Sfoglia il codice sorgente

新增NVR支持根据通道设置APP通知提醒,IPC默认关闭移动侦测APP通知提醒

zhangdongming 9 mesi fa
parent
commit
83d722f68a
2 ha cambiato i file con 78 aggiunte e 7 eliminazioni
  1. 51 3
      Controller/DetectControllerV2.py
  2. 27 4
      Controller/EquipmentManager.py

+ 51 - 3
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}'
+
+            # 使用条件表达式构建 new_data
+            new_data = {target_key: list(map(int, event_types.split(','))) if event_types else []}
+
+            # 获取 nvr 列表并准备标志
+            nvr_list = msg_notify.get('nvr', [])
+
+            # 尝试在 nvr 列表中找到匹配的通道
+            for item in nvr_list:
+                if target_key in item:
+                    item[target_key] = new_data[target_key]
+                    return msg_notify  # 找到匹配后直接返回
+
+            # 如果没有找到匹配,添加 new_data
+            nvr_list.append(new_data)
+            msg_notify['nvr'] = nvr_list
+
+            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)
@@ -188,12 +221,26 @@ class DetectControllerViewV2(View):
                     uid_set_data['msg_notify'] = msg_data
                 elif status == 1 and uid_set_qs.first().detect_status == 0:
                     uid_set_data['detect_interval'] = 60
+                    types = ALGORITHM_COMBO_TYPES
+                    types = [x for x in types if x != 51]
                     msg_data = {'appPush': 1,
                                 'pushTime': {'allDay': 1, 'repeat': 127, 'endTime': 0, 'timeZone': '+08.00',
                                              'startTime': 0},
-                                'eventTypes': {'device': ALGORITHM_COMBO_TYPES, 'aiCloud': 1}
+                                '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}': ALGORITHM_COMBO_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)
@@ -302,6 +349,7 @@ class DetectControllerViewV2(View):
             else:
                 return response.json(173)
         except Exception as e:
+            LOGGER.info('消息推送设置error,uid:{},line:{},msg:{}'.format(uid, e.__traceback__.tb_lineno, repr(e)))
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     def do_delete_redis(self, uid, detect_interval=0):

+ 27 - 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 Ansjer.config import PUSH_REDIS_ADDRESS
 from Controller.DetectController import DetectControllerView
 from Controller.DeviceShare import DeviceShareView
@@ -1097,6 +1097,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:
@@ -1115,7 +1117,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():
@@ -1147,9 +1149,30 @@ 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
+        else:
+            if 'nvr' in qs[0].get('msg_notify'):
+                data['msgData']['nvr'] = []
+
         # 调试
         debugTwos = int(time.time())
-        logger.error('————————debugTwo_sec:', debugTwos - debugTwo)
+        logger.error('————————debugTwo_sec:{}'.format(debugTwos - debugTwo))
 
         # 调试
         debugThere = int(time.time())
@@ -1160,7 +1183,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():