|
@@ -7,6 +7,7 @@
|
|
|
@Software: PyCharm
|
|
|
"""
|
|
|
import json
|
|
|
+import time
|
|
|
|
|
|
from django.http import QueryDict
|
|
|
from django.views import View
|
|
@@ -47,6 +48,8 @@ class DeviceVersionInfoView(View):
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
response = ResponseObject('cn')
|
|
|
+ if operation == 'syncVerConfig':
|
|
|
+ return self.sync_ver_config(request, request_dict, response)
|
|
|
tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
if tko.code != 0:
|
|
|
return response.json(tko.code)
|
|
@@ -206,3 +209,82 @@ class DeviceVersionInfoView(View):
|
|
|
error_line = e.__traceback__.tb_lineno
|
|
|
LOGGER.error(f'验证用户uid异常 user:{user_id}, uid:{uid}, error_line:{error_line}, error_msg:{repr(e)}')
|
|
|
return response.json(500, f'error_line:{error_line}, error_msg:{repr(e)}')
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def sync_ver_config(cls, request, request_dict, response):
|
|
|
+ # 定义必要字段列表
|
|
|
+ required_fields = ['dCode', 'softwareVer', 'videoCode', 'deviceType', 'supportsAlarm', 'screenChannels',
|
|
|
+ 'networkType']
|
|
|
+
|
|
|
+ # 验证必要参数是否存在
|
|
|
+ if not all(request_dict.get(field) for field in required_fields):
|
|
|
+ LOGGER.error(f'缺少必要参数: {request_dict}')
|
|
|
+ return response.json(444)
|
|
|
+ LOGGER.info(f'同步设备版本信息 params: {request_dict}')
|
|
|
+ try:
|
|
|
+ d_code = request_dict.get('dCode', '')
|
|
|
+ ver = request_dict.get('softwareVer', '')
|
|
|
+ # 提取参数并设置默认值(添加必要字段的类型转换)
|
|
|
+ params = {
|
|
|
+ 'd_code': d_code, # 字符串类型,添加默认空字符串
|
|
|
+ 'software_ver': ver, # 字符串类型,添加默认空字符串
|
|
|
+ 'firmware_ver': request_dict.get('firmwareVer', ''), # 已有默认空字符串,保持不变
|
|
|
+ 'video_code': int(request_dict.get('videoCode', 0)), # 使用get()添加默认值0,避免KeyError
|
|
|
+ 'region_alexa': request_dict.get('regionAlexa', 'ALL'), # 匹配模型默认值'ALL'
|
|
|
+ 'supports_human_tracking': bool(int(request_dict.get('supportsHumanTracking', 0))), # 转为布尔值
|
|
|
+ 'supports_custom_voice': bool(int(request_dict.get('supportsCustomVoice', 0))), # 转为布尔值
|
|
|
+ 'supports_dual_band_wifi': bool(int(request_dict.get('supportsDualBandWifi', 0))), # 转为布尔值
|
|
|
+ 'supports_four_point': bool(int(request_dict.get('supportsFourPoint', 0))), # 转为布尔值
|
|
|
+ 'supports_4g': bool(int(request_dict.get('supports4g', 0))), # 转为布尔值
|
|
|
+ 'supports_ptz': bool(int(request_dict.get('supportsPTZ', 0))), # 转为布尔值
|
|
|
+ 'supports_ai': bool(int(request_dict.get('supportsAi', 0))), # 转为布尔值
|
|
|
+ 'supports_cloud_storage': bool(int(request_dict.get('supportsCloudStorage', 0))), # 转为布尔值
|
|
|
+ 'supports_alexa': bool(int(request_dict.get('supportsAlexa', 0))), # 转为布尔值
|
|
|
+ 'device_type': int(request_dict.get('deviceType', 0)), # 使用get()添加默认值0
|
|
|
+ 'resolution': request_dict.get('resolution', ''), # 字符串类型,添加默认空字符串
|
|
|
+ 'ai_type': int(request_dict.get('aiType', 0)), # 使用get()添加默认值0
|
|
|
+ 'supports_alarm': int(request_dict.get('supportsAlarm', -1)), # 使用get()添加默认值-1
|
|
|
+ 'supports_night_vision': int(request_dict.get('supportsNightVision', -1)), # 使用get()添加默认值-1
|
|
|
+ 'screen_channels': int(request_dict.get('screenChannels', 1)), # 匹配模型默认值1
|
|
|
+ 'network_type': int(request_dict.get('networkType', 1)), # 匹配模型默认值1
|
|
|
+ 'other_features': json.loads(request_dict['otherFeatures']) if request_dict.get(
|
|
|
+ 'otherFeatures') else None, # 保持不变
|
|
|
+ 'electricity_statistics': int(request_dict.get('electricityStatistics', 0)), # 使用get()添加默认值0
|
|
|
+ 'supports_pet_tracking': int(request_dict.get('supportsPetTracking', 0)), # 使用get()添加默认值0
|
|
|
+ }
|
|
|
+
|
|
|
+ now_time = int(time.time())
|
|
|
+
|
|
|
+ # 使用 update_or_create 合并更新和创建操作
|
|
|
+ version_config_qs = DeviceVersionInfo.objects.filter(
|
|
|
+ d_code=params['d_code'],
|
|
|
+ software_ver=params['software_ver']
|
|
|
+ )
|
|
|
+ if version_config_qs.exists():
|
|
|
+ version_config_qs.update(**params, updated_time=now_time)
|
|
|
+ # 创建Redis对象
|
|
|
+ redis = RedisObject()
|
|
|
+ # 构建Redis键
|
|
|
+ version_key = RedisKeyConstant.ZOSI_DEVICE_VERSION_INFO.value + ver + d_code
|
|
|
+ redis.del_data(version_key)
|
|
|
+ else:
|
|
|
+ DeviceVersionInfo.objects.create(**params, created_time=now_time, updated_time=now_time)
|
|
|
+
|
|
|
+ except json.JSONDecodeError as e:
|
|
|
+ # 专门捕获 JSON 解析错误
|
|
|
+ LOGGER.error(f"JSON 解析失败: {str(e)}")
|
|
|
+ return response.json(500, f'Invalid JSON format: {str(e)}')
|
|
|
+ except KeyError as e:
|
|
|
+ # 处理字段缺失(理论上 required_fields 已校验,此处作为备用)
|
|
|
+ LOGGER.error(f"参数缺失: {str(e)}")
|
|
|
+ return response.json(444, f'Missing parameter: {str(e)}')
|
|
|
+ except ValueError as e:
|
|
|
+ # 处理整型转换失败
|
|
|
+ LOGGER.error(f"参数类型错误: {str(e)}")
|
|
|
+ return response.json(400, f'Invalid parameter type: {str(e)}')
|
|
|
+ except Exception as e:
|
|
|
+ # 通用异常处理(建议替换为具体异常类型)
|
|
|
+ LOGGER.exception("同步设备配置失败")
|
|
|
+ return response.json(500, f'Server error: {str(e)}')
|
|
|
+
|
|
|
+ return response.json(0)
|