|
@@ -9,6 +9,7 @@
|
|
|
import logging
|
|
|
import time
|
|
|
from datetime import datetime, timezone
|
|
|
+from http.client import responses
|
|
|
|
|
|
from django.http import JsonResponse
|
|
|
from django.views import View
|
|
@@ -43,6 +44,9 @@ class DeviceReportView(View):
|
|
|
if len(uid) not in (14, 20):
|
|
|
raise ValidationError('Invalid device UID format')
|
|
|
|
|
|
+ # 数据库操作
|
|
|
+ n_time = int(time.time())
|
|
|
+ TIME_LOGGER.info(f'电池电量上报uid:{uid}{data}')
|
|
|
# 参数提取和类型转换
|
|
|
wake_sleep = self.parse_int_param(data, 'pirWakeupCount') + self.parse_int_param(data, 'mqttWakeupCount')
|
|
|
report_params = {
|
|
@@ -52,15 +56,23 @@ class DeviceReportView(View):
|
|
|
'wake_sleep': wake_sleep,
|
|
|
'working_hours': self.parse_int_param(data, 'workingHours'),
|
|
|
'battery_level': self.parse_int_param(data, 'batteryLevel'),
|
|
|
- 'report_time': self.parse_int_param(data, 'reportTime') - 3600,
|
|
|
+ 'report_time': n_time - 43200,
|
|
|
'channel': int(data.get('channel', 1))
|
|
|
}
|
|
|
-
|
|
|
+ # 若人体检测、PIR唤醒、MQTT唤醒、工作时长均为0,判定为无效数据,返回失败
|
|
|
+ if (report_params['human_detection'] == 0
|
|
|
+ and report_params['pir_wakeup_count'] == 0
|
|
|
+ and report_params['mqtt_wakeup_count'] == 0
|
|
|
+ and report_params['working_hours'] == 0
|
|
|
+ ):
|
|
|
+ return JsonResponse({
|
|
|
+ 'code': 0,
|
|
|
+ 'msg': 'Reporting failed',
|
|
|
+ 'channel': report_params['channel']
|
|
|
+ })
|
|
|
# 时间戳验证
|
|
|
report_date = datetime.fromtimestamp(report_params['report_time'], tz=timezone.utc)
|
|
|
|
|
|
- # 数据库操作
|
|
|
- n_time = int(time.time())
|
|
|
DeviceDailyReport.objects.create(
|
|
|
device_id=uid,
|
|
|
type=1,
|
|
@@ -71,7 +83,7 @@ class DeviceReportView(View):
|
|
|
)
|
|
|
|
|
|
TIME_LOGGER.info(f'设备保存电池报告uid: {uid}')
|
|
|
- return JsonResponse({'code': 0, 'msg': 'Report saved successfully'})
|
|
|
+ return JsonResponse({'code': 0, 'msg': 'Report saved successfully', 'channel': report_params['channel']})
|
|
|
|
|
|
except ValidationError as e:
|
|
|
ERROR_INFO_LOGGER.warning(f'Validation error: {e.message}', exc_info=True)
|