Bläddra i källkod

基站推送使用通道名

locky 1 månad sedan
förälder
incheckning
7ae67d40a1
2 ändrade filer med 116 tillägg och 38 borttagningar
  1. 2 1
      AnsjerPush/config.py
  2. 114 37
      Service/DevicePushService.py

+ 2 - 1
AnsjerPush/config.py

@@ -143,7 +143,8 @@ APP_BUNDLE_DICT = {
     'com.ansjer.customizedc_a': 'PatrolSecure',
 }
 
-MULTI_CHANNEL_TYPE_LIST = [1, 2, 3, 4, 10001, 300]
+BASE_STATION_TYPE_LIST = [300]
+MULTI_CHANNEL_TYPE_LIST = [1, 2, 3, 4, 10001]
 # 事件类型,完整内容见企业微信的《开发类型定义》文档的推送类型表
 # 系统消息事件类型
 SYS_EVENT_TYPE_LIST = [702, 703, 704, 706]

+ 114 - 37
Service/DevicePushService.py

@@ -24,10 +24,10 @@ from AnsjerPush.MessageConfig import EVENT_CONFIGS, DEFAULT_TEXTS, MSG_CONFIG
 from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, MULTI_CHANNEL_TYPE_LIST, SYS_EVENT_TYPE_LIST, \
     EVENT_DICT, EVENT_DICT_CN, CONFIG_TEST, \
     HUAWEICLOUD_OBS_SERVER, HUAWEICLOUD_PUSH_BUKET, JPUSH_UID_LIST, \
-    DATA_PUSH_EVENT_TYPE_LIST, PRIMARY_USERS_PUSH_EVENT_TYPE_LIST
+    DATA_PUSH_EVENT_TYPE_LIST, PRIMARY_USERS_PUSH_EVENT_TYPE_LIST, BASE_STATION_TYPE_LIST
 from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG, XM_PUSH_CHANNEL_ID, XM_PUSH_CHANNEL_DICT
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
-    DeviceChannelUserPermission, UidSetModel, Device_Info, UserAudioVideoPush, PushLog
+    DeviceChannelUserPermission, UidSetModel, Device_Info, UserAudioVideoPush, PushLog, UidChannelSetModel
 from Object.ETkObject import ETkObject
 from Object.OCIObjectStorage import OCIObjectStorage
 from Object.RedisObject import RedisObject
@@ -217,6 +217,7 @@ class DevicePushService:
 
                 push_kwargs = params['push_kwargs']
                 push_token_list = []
+                msg_cache = {}
                 for up in params['uid_set_push_list']:
                     push_type = up['push_type']
                     lang = up['lang']
@@ -236,14 +237,32 @@ class DevicePushService:
                     if process_token in push_token_list:
                         LOGGING.info("uid:{}, 重复token_val{},".format(uid, token_val))
                         continue
-                    # 发送标题
-                    msg_title = cls.get_msg_title(nickname=params['nickname'])
-                    # 发送内容
-                    msg_text = cls.get_msg_text_v2(channel=params['channel'], n_time=params['n_time'], lang=lang, tz=tz,
-                                                   event_type=params['event_type'], ai_type=params['ai_type'],
-                                                   device_type=params['device_type'], electricity=params['electricity'],
-                                                   dealings_type=params['dealings_type'], event_tag=params['event_tag'],
-                                                   redis_obj=params['redis_obj'])
+                    # 如果当前语言下的消息标题和内容尚未计算,则调用函数生成并存入缓存
+                    if lang not in msg_cache:
+                        # 生成标题
+                        title = cls.get_msg_title(nickname=params['nickname'])
+                        # 生成消息内容
+                        text = cls.get_msg_text_v2(
+                            uid=uid,
+                            channel=params['channel'],
+                            n_time=params['n_time'],
+                            lang=lang,
+                            tz=tz,
+                            event_type=params['event_type'],
+                            ai_type=params['ai_type'],
+                            device_type=params['device_type'],
+                            electricity=params['electricity'],
+                            dealings_type=params['dealings_type'],
+                            event_tag=params['event_tag'],
+                            redis_obj=params['redis_obj']
+                        )
+                        msg_cache[lang] = (title, text)
+                    else:
+                        title, text = msg_cache[lang]
+
+                    # 使用缓存获取的消息标题和内容
+                    msg_title = title
+                    msg_text = text
 
                     # 补齐推送参数
                     push_kwargs['appBundleId'] = appBundleId
@@ -548,9 +567,10 @@ class DevicePushService:
                 msg_type = 'Unknown event type'
             return msg_type
 
-    @staticmethod
-    def get_msg_text(channel, n_time, lang, tz, event_type, electricity='', is_sys=0, dealings_type=0, ai_type=0,
-                     device_type=0, event_tag='', is_save_msg=0):
+    @classmethod
+    def get_msg_text(
+            cls, uid, channel, n_time, lang, tz, event_type, electricity='', is_sys=0, dealings_type=0, ai_type=0,
+            device_type=0, event_tag='', is_save_msg=0):
         """
         获取消息文本
         @param: channel 通道号
@@ -634,11 +654,19 @@ class DevicePushService:
             if is_sys:
                 if device_type in MULTI_CHANNEL_TYPE_LIST:
                     send_text = '{} 通道:{}'.format(msg_type, channel)
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = '{} 通道:{}'.format(msg_type, channel_name)
                 else:
                     send_text = msg_type
             else:
                 if device_type in MULTI_CHANNEL_TYPE_LIST:
                     send_text = '{} 通道:{}'.format(msg_type, channel)
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = '{} 通道:{}'.format(msg_type, channel_name)
                 else:
                     send_text = '{}'.format(msg_type)
         else:
@@ -698,18 +726,27 @@ class DevicePushService:
             if is_sys:
                 if device_type in MULTI_CHANNEL_TYPE_LIST:
                     send_text = '{} channel:{}'.format(msg_type, channel)
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = '{} channel:{}'.format(msg_type, channel_name)
                 else:
                     send_text = msg_type
             else:
                 if device_type in MULTI_CHANNEL_TYPE_LIST:
                     send_text = '{} channel:{}'.format(msg_type, channel)
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = '{} channel:{}'.format(msg_type, channel_name)
                 else:
                     send_text = '{}'.format(msg_type)
         return send_text
 
-    @staticmethod
-    def get_msg_text_v2(channel, n_time, lang, tz, event_type, electricity='', is_sys=0, dealings_type=0, ai_type=0,
-                        device_type=0, event_tag='', is_save_msg=0, redis_obj=None):
+    @classmethod
+    def get_msg_text_v2(
+            cls, uid, channel, n_time, lang, tz, event_type, electricity='', is_sys=0, dealings_type=0, ai_type=0,
+            device_type=0, event_tag='', is_save_msg=0, redis_obj=None):
         """
         获取消息文案V2
         """
@@ -764,30 +801,23 @@ class DevicePushService:
             if is_sys:
                 if device_type in MULTI_CHANNEL_TYPE_LIST:
                     send_text = f'{msg_type} 通道:{channel}' if lang == 'cn' else f'{msg_type} channel:{channel}'
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = f'{msg_type} 通道:{channel_name}' if lang == 'cn' else\
+                        f'{msg_type} channel:{channel_name}'
                 else:
                     send_text = msg_type
             else:
-                if device_type in MULTI_CHANNEL_TYPE_LIST:  # NVR多通道文案
-                    channel_dict = {
-                        'cn': '通道',  # 简体中文
-                        'en': 'Channel',  # 英语
-                        'es': 'Canal',  # 西班牙语
-                        'fr': 'Canal',  # 法语
-                        'de': 'Kanal ',  # 德语
-                        'cn_tw': '通道',  # 繁体中文
-                        'pt': 'Canal',  # 葡萄牙语
-                        'ru': 'Канал',  # 俄语
-                        'ja': 'チャンネル',  # 日语
-                        'it': 'Canale',  # 意大利语
-                        'pl': 'Kanał',  # 波兰语
-                        'nl': 'Kanaal',  # 荷兰语
-                        'ko': '채널',  # 韩语
-                        'ar': 'قناة',  # 阿拉伯语
-                        'vi': 'kênh',  # 越南语
-                        'th': 'ช่องเสียง'  # 泰语
-                    }
-                    prefix = channel_dict.get(lang, channel_dict['en'])
-                    send_text = f'{msg_type} {prefix}:{channel}'
+                # NVR多通道文案
+                if device_type in MULTI_CHANNEL_TYPE_LIST:
+                    channel_prefix = cls.get_get_channel_prefix(lang)
+                    send_text = f'{msg_type} {channel_prefix}:{channel}'
+                elif device_type in BASE_STATION_TYPE_LIST:
+                    # 基站使用通道名
+                    channel_prefix = cls.get_get_channel_prefix(lang)
+                    channel_name = cls.get_channel_name(uid, channel)
+                    send_text = f'{msg_type} {channel_prefix}:{channel_name}'
                 else:
                     send_text = f'{msg_type}'
             return send_text
@@ -796,6 +826,53 @@ class DevicePushService:
                                     .format(n_time, event_tag, e.__traceback__.tb_lineno, repr(e)))
             return msg_type
 
+    @staticmethod
+    def get_get_channel_prefix(lang: str) -> str:
+        """
+        获取通道
+        @param lang: 语言
+        @return: channel_prefix
+        """
+        channel_dict = {
+            'cn': '通道',  # 简体中文
+            'en': 'Channel',  # 英语
+            'es': 'Canal',  # 西班牙语
+            'fr': 'Canal',  # 法语
+            'de': 'Kanal ',  # 德语
+            'cn_tw': '通道',  # 繁体中文
+            'pt': 'Canal',  # 葡萄牙语
+            'ru': 'Канал',  # 俄语
+            'ja': 'チャンネル',  # 日语
+            'it': 'Canale',  # 意大利语
+            'pl': 'Kanał',  # 波兰语
+            'nl': 'Kanaal',  # 荷兰语
+            'ko': '채널',  # 韩语
+            'ar': 'قناة',  # 阿拉伯语
+            'vi': 'kênh',  # 越南语
+            'th': 'ช่องเสียง'  # 泰语
+        }
+        channel_prefix = channel_dict.get(lang, channel_dict['en'])
+        return channel_prefix
+
+    @staticmethod
+    def get_channel_name(uid: str, channel: int) -> str:
+        """
+        根据uid和channel查询并返回UidChannelSetModel的channel_name
+        @param uid: 设备uid
+        @param channel: 通道号
+        @return: channel_name或channel
+        """
+        try:
+            uid_channel = UidChannelSetModel.objects.filter(
+                uid__uid=uid, channel=channel).values('channel_name').first()
+            if uid_channel and uid_channel.get('channel_name'):
+                return uid_channel['channel_name']
+            return str(channel)
+        except Exception as e:
+            ERROR_INFO_LOGGER.error('获取通道名异常uid:{},channel:{},error行数:{},内容:{}'.format(
+                uid, channel, e.__traceback__.tb_lineno, repr(e)))
+            return str(channel)
+
     @staticmethod
     def get_event_type_text_v2(lang, event_type, dealings_type, redis_obj):
         """ 改造后:优先读缓存,无缓存时读配置并写入 """