소스 검색

更新NVR消息提醒

zhangdongming 10 달 전
부모
커밋
12ec47d389
2개의 변경된 파일71개의 추가작업 그리고 6개의 파일을 삭제
  1. 47 2
      Controller/DetectControllerV2.py
  2. 24 4
      Controller/EquipmentManager.py

+ 47 - 2
Controller/DetectControllerV2.py

@@ -87,6 +87,9 @@ class DetectControllerViewV2(View):
         try:
             msg_data = request_dict.get('msgData', None)
             uid = request_dict.get('uid', None)
+            channel = int(request_dict.get('channel', 0))
+            is_nvr = int(request_dict.get('isNVR', 0))
+            event_types = request_dict.get('eventTypes', None)
             LOGGER.info('*****DetectControllerViewV2.message_notification_set*****api_version:{},uid:{}'
                         .format(api_version, uid))
             if not all([msg_data, uid]):
@@ -95,13 +98,42 @@ class DetectControllerViewV2(View):
             uid_set_qs = UidSetModel.objects.filter(uid=uid)
             if not uid_set_qs.exists():
                 return response.json(173)
+            data = cls.update_nvr_app_msg(channel, is_nvr, uid_set_qs[0].msg_notify, data, event_types)
+
             uid_set_qs.update(msg_notify=data, updTime=int(time.time()))
             return response.json(0)
         except Exception as e:
-            LOGGER.info('*****DetectControllerViewV2.message_notification_set:errLine:{}, errMsg:{}'
-                        .format(e.__traceback__.tb_lineno, repr(e)))
+            LOGGER.error('*****DetectControllerViewV2.message_notification_set:errLine:{}, errMsg:{}'
+                         .format(e.__traceback__.tb_lineno, repr(e)))
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
+    @classmethod
+    def update_nvr_app_msg(cls, channel, is_nvr, msg_notify, data, event_types):
+        # 检查是否是 NVR 且通道大于 0
+        if is_nvr == 1 and channel > 0:
+            # 构建目标键,例如 'channel1' 或 'channel2'
+            target_key = f'channel{channel}'
+            # 初始化存在标志
+            channel_found = False
+            new_data = {target_key: list(map(int, event_types.split(',')))}
+            # 检查 msg_notify 是否存在且包含 'nvr'
+            if msg_notify and 'nvr' in msg_notify:
+
+                # 遍历 nvr 列表中的每个字典
+                for item in msg_notify['nvr']:
+                    # 检查当前通道的键是否在 item 中
+                    if target_key in item:
+                        # 替换对应通道的值为 data 中的值
+                        item[target_key] = new_data[target_key]
+                        # 由于目标键是唯一的,可以找到后退出循环
+                        channel_found = True
+                        break
+
+                if not channel_found:
+                    msg_notify['nvr'].append(new_data)
+            return msg_notify
+        return data
+
     def do_change_status(self, userID, request_dict, response):
         token_val = request_dict.get('token_val', None)
         jg_token_val = request_dict.get('jg_token_val', '')
@@ -117,6 +149,7 @@ class DetectControllerViewV2(View):
         region = request_dict.get('region', None)  # app必须传:1:国外,2:国内
         electricity_status = request_dict.get('electricity_status', None)
         domain_name = request_dict.get('domain_name', None)
+        is_nvr = int(request_dict.get('isNVR', 0))
         if not region:
             return response.json(444, 'region')
         region = int(region)
@@ -195,7 +228,19 @@ class DetectControllerViewV2(View):
                                              'startTime': 0},
                                 'eventTypes': {'device': types, 'aiCloud': 1}
                                 }
+
+                    if is_nvr == 1:  # 检查NVR版本是否存在
+
+                        # 确定通道数量,如果channel大于0则使用其值,否则默认使用4
+                        channel_count = uid_set_qs[0].channel if uid_set_qs[0].channel > 0 else 4
+                        # 生成通道的列表,每个频道对应一个字典,格式为 "channelX": [types]
+                        channel_list = [{f'channel{index + 1}': types} for index in range(channel_count)]
+                        # 将NVR字典赋值给msg_data中的'nvr'键
+                        msg_data['nvr'] = channel_list
+                        msg_data['eventTypes']['device'] = []  # 将IPC允许推送类型设为空
+
                     uid_set_data['msg_notify'] = msg_data
+
                 uid_set_id = uid_set_qs[0].id
                 uid_set_data['updTime'] = nowTime
                 uid_set_qs.update(**uid_set_data)

+ 24 - 4
Controller/EquipmentManager.py

@@ -11,7 +11,7 @@ from django.db import transaction
 from django.db.models import Q
 from django.utils import timezone
 
-from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY, BASE_DIR
+from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY, BASE_DIR, ALGORITHM_COMBO_TYPES
 from Controller.DetectController import DetectControllerView
 from Controller.DeviceShare import DeviceShareView
 from Model.models import Device_User, Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidPushModel, \
@@ -1096,6 +1096,8 @@ def uid_status(request):
         return response.json(444)
     token = request_dict.get('token', None)
     uid = request_dict.get('uid', None)
+    channel = int(request_dict.get('channel', 0))
+    is_nvr = int(request_dict.get('isNVR', 0))
     tko = TokenObject(token)
     response.lang = tko.lang
     if tko.code != 0:
@@ -1114,7 +1116,7 @@ def uid_status(request):
 
     # 调试
     debugOnes = int(time.time())
-    logger.error('————————debugOne_sec:', debugOnes - debugOne)
+    logger.error('————————debugOne_sec:{}'.format(debugOnes - debugOne))
 
     # 判断uid配置表信息是否存在
     if qs.exists():
@@ -1146,9 +1148,27 @@ def uid_status(request):
             'msgData': qs[0]['msg_notify']
         }
 
+        # 检查 channel 有效性和 msg_notify 是否存在
+        if channel > 0 and is_nvr == 1 and qs[0].get('msg_notify'):
+            # 获取 msg_notify 数据
+            msg_notify = qs[0]['msg_notify']
+            nvr_entries = msg_notify.get('nvr', [])
+            channel_key = f'channel{channel}'
+
+            # 查找匹配的 channel_entry
+            matching_entry = next((entry for entry in nvr_entries if channel_key in entry), None)
+
+            if matching_entry:
+                # 更新 nvr 值
+                data['msgData']['nvr'] = matching_entry[channel_key]
+            else:
+                # 没有找到匹配的 channel,使用默认值替换 nvr
+                available_types = [t for t in ALGORITHM_COMBO_TYPES if t != 51]
+                data['msgData']['nvr'] = available_types
+
         # 调试
         debugTwos = int(time.time())
-        logger.error('————————debugTwo_sec:', debugTwos - debugTwo)
+        logger.error('————————debugTwo_sec:{}'.format(debugTwos - debugTwo))
 
         # 调试
         debugThere = int(time.time())
@@ -1159,7 +1179,7 @@ def uid_status(request):
 
         # 调试
         debugTheres = int(time.time())
-        logger.error('————————debugTheres_sec:', debugTheres - debugThere)
+        logger.error('————————debugTheres_sec:{}'.format(debugTheres - debugThere))
 
         nowTime = int(time.time())
         if ubqs.exists():