Forráskód Böngészése

异步推送消息

locky 1 éve
szülő
commit
c740f775c7

+ 7 - 2
Controller/DetectController.py

@@ -169,6 +169,11 @@ class NotificationView(View):
 def push_and_save_data(**params):
     uid = params['uid']
     V1_PUSH_LOGGER.info('{}开始异步存表和推送'.format(uid))
-    # 保存推送数据和推送消息
+    # 异步推送消息
+    push_thread = threading.Thread(
+        target=DevicePushService.push_msg,
+        kwargs=params)
+    push_thread.start()
+    # 保存推送数据
     result = DevicePushService.save_msg_push(**params)
-    V1_PUSH_LOGGER.info('{}存表和推送结果:{}'.format(uid, result))
+    V1_PUSH_LOGGER.info('{}存表结果:{}'.format(uid, result))

+ 7 - 2
Controller/DetectControllerV2.py

@@ -162,6 +162,11 @@ class NotificationV2View(View):
 def push_and_save_data(**params):
     uid = params['uid']
     TIME_LOGGER.info('{}开始异步存表和推送'.format(uid))
-    # 保存推送数据和推送消息
+    # 异步推送消息
+    push_thread = threading.Thread(
+        target=DevicePushService.push_msg,
+        kwargs=params)
+    push_thread.start()
+    # 保存推送数据
     result = DevicePushService.save_msg_push(**params)
-    TIME_LOGGER.info('{}存表和推送结果:{}'.format(uid, result))
+    TIME_LOGGER.info('{}存表结果:{}'.format(uid, result))

+ 59 - 41
Service/DevicePushService.py

@@ -140,6 +140,65 @@ class DevicePushService:
             detect_interval = new_detect_interval if new_detect_interval > detect_interval else detect_interval
         redis_obj.set_data(key=name, val=1, expire=detect_interval - 5)
 
+    @classmethod
+    def push_msg(cls, **params):
+        """
+        推送消息
+        @param params: 推送参数
+        @return: bool
+        """
+        uid = params['uid']
+        params['event_tag'] = cls.get_event_tag(params['ai_type'], params['event_type'], params['detection'])
+        is_app_push = True if params['event_type'] in [606, 607] else \
+            cls.is_send_app_push(
+                params['event_type'], params['event_tag'], params['app_push_config'], params['app_push'], uid)
+
+        # 低功耗产品推送,休眠702、低电量704提醒,并且detection=0,0标识单事件类型,1标识多事件类型
+        is_app_push = True if params['event_type'] in [702, 704] and params['detection'] == 0 else is_app_push
+
+        # 推送
+        if is_app_push:
+            push_kwargs = params['push_kwargs']
+            for up in params['uid_set_push_list']:
+                push_type = up['push_type']
+                lang = up['lang']
+                tz = up['tz']
+                if tz is None or tz == '':
+                    tz = 0
+                if params['event_type'] in [606, 607] and push_type in [5, 6]:
+                    push_kwargs['jg_token_val'] = up['jg_token_val']
+                else:
+                    if 'jg_token_val' in push_kwargs:
+                        push_kwargs.pop('jg_token_val')
+
+                appBundleId = up['appBundleId']
+                token_val = up['token_val']
+                # 发送标题
+                msg_title = cls.get_msg_title(nickname=params['nickname'])
+                # 发送内容
+                msg_text = cls.get_msg_text(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']
+                                            )
+
+                # 补齐推送参数
+                push_kwargs['appBundleId'] = appBundleId
+                push_kwargs['token_val'] = token_val
+                push_kwargs['msg_title'] = msg_title
+                push_kwargs['msg_text'] = msg_text
+                params['push_kwargs'] = push_kwargs
+                params['appBundleId'] = appBundleId
+                params['token_val'] = token_val
+                params['lang'] = lang
+                params['tz'] = tz
+
+                push_thread = threading.Thread(
+                    target=cls.send_app_msg_push,
+                    args=(push_type, params)
+                )
+                push_thread.start()
+
     @classmethod
     def save_msg_push(cls, **params):
         """
@@ -150,17 +209,10 @@ class DevicePushService:
         sys_msg_list = []
         saved_user_id_list = []
         uid = params['uid']
-        push_kwargs = params['push_kwargs']
         now_time = int(time.time())
         redis_obj = RedisObject()
         try:
             params['event_tag'] = cls.get_event_tag(params['ai_type'], params['event_type'], params['detection'])
-            is_app_push = True if params['event_type'] in [606, 607] else \
-                cls.is_send_app_push(
-                    params['event_type'], params['event_tag'], params['app_push_config'], params['app_push'], uid)
-
-            # 低功耗产品推送,休眠702、低电量704提醒,并且detection=0,0标识单事件类型,1标识多事件类型
-            is_app_push = True if params['event_type'] in [702, 704] and params['detection'] == 0 else is_app_push
 
             save_equipment_info = False
             equipment_info_key = EquipmentInfoService.randoms_choice_equipment_info_key()
@@ -208,40 +260,6 @@ class DevicePushService:
                         LOGGING.info('***保存推送消息uid:{},time:{},user_id:{}'.format(uid, params['n_time'], user_id))
                     saved_user_id_list.append(user_id)
 
-                # 推送
-                if is_app_push:
-                    push_type = up['push_type']
-                    if params['event_type'] in [606, 607] and push_type in [5, 6]:
-                        push_kwargs['jg_token_val'] = up['jg_token_val']
-                    else:
-                        if 'jg_token_val' in push_kwargs:
-                            push_kwargs.pop('jg_token_val')
-
-                    appBundleId = up['appBundleId']
-                    token_val = up['token_val']
-                    # 发送标题
-                    msg_title = cls.get_msg_title(nickname=params['nickname'])
-                    # 发送内容
-                    msg_text = cls.get_msg_text(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']
-                                                )
-
-                    # 补齐推送参数
-                    push_kwargs['appBundleId'] = appBundleId
-                    push_kwargs['token_val'] = token_val
-                    push_kwargs['msg_title'] = msg_title
-                    push_kwargs['msg_text'] = msg_text
-                    params['push_kwargs'] = push_kwargs
-                    params['appBundleId'] = appBundleId
-                    params['token_val'] = token_val
-                    params['lang'] = lang
-                    params['tz'] = tz
-
-                    push_result = cls.send_app_msg_push(push_type, **params)
-                    LOGGING.info('{}推送类型:{},推送结果:{}'.format(uid, push_type, push_result))
-
             # 写入系统消息
             if sys_msg_list:
                 SysMsgModel.objects.bulk_create(sys_msg_list)