|
|
|
|
@ -16,86 +16,110 @@ from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
|
|
from .models import OwnTrackLog
|
|
|
|
|
|
|
|
|
|
# 初始化日志记录器,用于记录视图中的操作和错误信息
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 处理位置日志数据提交的视图函数,禁用CSRF保护(方便外部设备提交数据)
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def manage_owntrack_log(request):
|
|
|
|
|
try:
|
|
|
|
|
# 解析请求体中的JSON数据
|
|
|
|
|
s = json.loads(request.read().decode('utf-8'))
|
|
|
|
|
# 提取必要的字段(用户标识、纬度、经度)
|
|
|
|
|
tid = s['tid']
|
|
|
|
|
lat = s['lat']
|
|
|
|
|
lon = s['lon']
|
|
|
|
|
|
|
|
|
|
# 记录日志信息
|
|
|
|
|
logger.info(
|
|
|
|
|
'tid:{tid}.lat:{lat}.lon:{lon}'.format(
|
|
|
|
|
tid=tid, lat=lat, lon=lon))
|
|
|
|
|
# 验证字段不为空
|
|
|
|
|
if tid and lat and lon:
|
|
|
|
|
# 创建并保存位置记录
|
|
|
|
|
m = OwnTrackLog()
|
|
|
|
|
m.tid = tid
|
|
|
|
|
m.lat = lat
|
|
|
|
|
m.lon = lon
|
|
|
|
|
m.save()
|
|
|
|
|
return HttpResponse('ok')
|
|
|
|
|
return HttpResponse('ok') # 成功响应
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse('data error')
|
|
|
|
|
return HttpResponse('data error') # 数据不完整错误
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 记录异常信息
|
|
|
|
|
logger.error(e)
|
|
|
|
|
return HttpResponse('error')
|
|
|
|
|
return HttpResponse('error') # 异常响应
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 显示地图页面的视图函数,要求用户登录且为超级用户
|
|
|
|
|
@login_required
|
|
|
|
|
def show_maps(request):
|
|
|
|
|
if request.user.is_superuser:
|
|
|
|
|
# 获取默认日期(当前UTC日期)或请求中的日期参数
|
|
|
|
|
defaultdate = str(datetime.datetime.now(timezone.utc).date())
|
|
|
|
|
date = request.GET.get('date', defaultdate)
|
|
|
|
|
# 传递日期参数到模板
|
|
|
|
|
context = {
|
|
|
|
|
'date': date
|
|
|
|
|
}
|
|
|
|
|
return render(request, 'owntracks/show_maps.html', context)
|
|
|
|
|
else:
|
|
|
|
|
# 非超级用户拒绝访问
|
|
|
|
|
from django.http import HttpResponseForbidden
|
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 显示日志日期列表的视图函数,要求用户登录
|
|
|
|
|
@login_required
|
|
|
|
|
def show_log_dates(request):
|
|
|
|
|
# 获取所有记录的创建时间,提取日期并去重排序
|
|
|
|
|
dates = OwnTrackLog.objects.values_list('creation_time', flat=True)
|
|
|
|
|
results = list(sorted(set(map(lambda x: x.strftime('%Y-%m-%d'), dates))))
|
|
|
|
|
|
|
|
|
|
# 传递日期列表到模板
|
|
|
|
|
context = {
|
|
|
|
|
'results': results
|
|
|
|
|
}
|
|
|
|
|
return render(request, 'owntracks/show_log_dates.html', context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 将GPS坐标转换为高德地图坐标的工具函数
|
|
|
|
|
def convert_to_amap(locations):
|
|
|
|
|
convert_result = []
|
|
|
|
|
it = iter(locations)
|
|
|
|
|
it = iter(locations) # 创建迭代器
|
|
|
|
|
|
|
|
|
|
# 每次处理30个坐标(高德API限制)
|
|
|
|
|
item = list(itertools.islice(it, 30))
|
|
|
|
|
while item:
|
|
|
|
|
# 拼接坐标字符串(格式:"lon1,lat1;lon2,lat2")
|
|
|
|
|
datas = ';'.join(
|
|
|
|
|
set(map(lambda x: str(x.lon) + ',' + str(x.lat), item)))
|
|
|
|
|
|
|
|
|
|
key = '8440a376dfc9743d8924bf0ad141f28e'
|
|
|
|
|
# 高德坐标转换API参数
|
|
|
|
|
key = '8440a376dfc9743d8924bf0ad141f28e' # 高德API密钥
|
|
|
|
|
api = 'http://restapi.amap.com/v3/assistant/coordinate/convert'
|
|
|
|
|
query = {
|
|
|
|
|
'key': key,
|
|
|
|
|
'locations': datas,
|
|
|
|
|
'coordsys': 'gps'
|
|
|
|
|
'coordsys': 'gps' # 源坐标系统为GPS
|
|
|
|
|
}
|
|
|
|
|
# 调用高德API进行坐标转换
|
|
|
|
|
rsp = requests.get(url=api, params=query)
|
|
|
|
|
result = json.loads(rsp.text)
|
|
|
|
|
if "locations" in result:
|
|
|
|
|
convert_result.append(result['locations'])
|
|
|
|
|
# 处理下一批坐标
|
|
|
|
|
item = list(itertools.islice(it, 30))
|
|
|
|
|
|
|
|
|
|
# 拼接所有转换结果
|
|
|
|
|
return ";".join(convert_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 获取位置数据的视图函数,要求用户登录
|
|
|
|
|
@login_required
|
|
|
|
|
def get_datas(request):
|
|
|
|
|
# 确定查询日期(默认今天,可通过date参数指定)
|
|
|
|
|
now = django.utils.timezone.now().replace(tzinfo=timezone.utc)
|
|
|
|
|
querydate = django.utils.timezone.datetime(
|
|
|
|
|
now.year, now.month, now.day, 0, 0, 0)
|
|
|
|
|
@ -103,25 +127,32 @@ def get_datas(request):
|
|
|
|
|
date = list(map(lambda x: int(x), request.GET.get('date').split('-')))
|
|
|
|
|
querydate = django.utils.timezone.datetime(
|
|
|
|
|
date[0], date[1], date[2], 0, 0, 0)
|
|
|
|
|
# 计算查询日期的结束时间(次日0点)
|
|
|
|
|
nextdate = querydate + datetime.timedelta(days=1)
|
|
|
|
|
# 查询该日期范围内的所有位置记录
|
|
|
|
|
models = OwnTrackLog.objects.filter(
|
|
|
|
|
creation_time__range=(querydate, nextdate))
|
|
|
|
|
|
|
|
|
|
result = list()
|
|
|
|
|
if models and len(models):
|
|
|
|
|
# 按用户标识(tid)分组
|
|
|
|
|
for tid, item in groupby(
|
|
|
|
|
sorted(models, key=lambda k: k.tid), key=lambda k: k.tid):
|
|
|
|
|
|
|
|
|
|
d = dict()
|
|
|
|
|
d["name"] = tid
|
|
|
|
|
d["name"] = tid # 用户名
|
|
|
|
|
paths = list()
|
|
|
|
|
# 使用高德转换后的经纬度
|
|
|
|
|
# 注释掉的代码:使用高德转换后的坐标
|
|
|
|
|
# locations = convert_to_amap(
|
|
|
|
|
# sorted(item, key=lambda x: x.creation_time))
|
|
|
|
|
# for i in locations.split(';'):
|
|
|
|
|
# paths.append(i.split(','))
|
|
|
|
|
# 使用GPS原始经纬度
|
|
|
|
|
|
|
|
|
|
# 当前使用:GPS原始坐标,按创建时间排序
|
|
|
|
|
for location in sorted(item, key=lambda x: x.creation_time):
|
|
|
|
|
paths.append([str(location.lon), str(location.lat)])
|
|
|
|
|
d["path"] = paths
|
|
|
|
|
d["path"] = paths # 位置路径
|
|
|
|
|
result.append(d)
|
|
|
|
|
|
|
|
|
|
# 返回JSON格式的位置数据
|
|
|
|
|
return JsonResponse(result, safe=False)
|
|
|
|
|
|