瀏覽代碼

优化低电量推送接口

locky 2 年之前
父節點
當前提交
ca42c53c65
共有 3 個文件被更改,包括 128 次插入187 次删除
  1. 4 4
      Controller/AiController.py
  2. 57 177
      Controller/PowerWarningController.py
  3. 67 6
      Service/PushService.py

+ 4 - 4
Controller/AiController.py

@@ -78,7 +78,7 @@ class AiView(View):
 
         # 查询推送相关数据
         uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
-            values('push_type', 'appBundleId', 'token_val', 'm_code', 'lang', 'tz', 'userID_id')
+            values('push_type', 'appBundleId', 'token_val', 'lang', 'tz', 'userID_id')
         if not uid_push_qs.exists():
             return response.json(173)
 
@@ -180,16 +180,16 @@ class AiView(View):
 
                 # 推送
                 push_type = up['push_type']
-                appBundleId = up['appBundleId']
+                app_bundle_id = up['appBundleId']
                 token_val = up['token_val']
                 lang = up['lang']
                 # 推送标题和推送内容
-                msg_title = PushObject.get_msg_title(app_bundle_id=appBundleId, nickname=nickname)
+                msg_title = PushObject.get_msg_title(app_bundle_id=app_bundle_id, nickname=nickname)
                 msg_text = PushObject.get_ai_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz, label=label_str)
 
                 kwargs = {
                     'nickname': nickname,
-                    'app_bundle_id': appBundleId,
+                    'app_bundle_id': app_bundle_id,
                     'token_val': token_val,
                     'n_time': n_time,
                     'event_type': event_type,

+ 57 - 177
Controller/PowerWarningController.py

@@ -5,24 +5,18 @@
 @File :PowerWarningController.py
 """
 import logging
-import os
 import time
 
-import apns2
-import jpush as jpush
 from django.http import JsonResponse
 from django.views.generic.base import View
-from pyfcm import FCMNotification
 
-from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE
-from Model.models import UidPushModel, SysMsgModel
+from Model.models import UidPushModel, SysMsgModel, Device_Info
 from Object.RedisObject import RedisObject
-from Service.CommonService import CommonService
+from Service.PushService import PushObject
 
 
-# 低电量推送接口
 class PowerWarningView(View):
-
+    # 低电量推送接口
     def get(self, request, *args, **kwargs):
         request.encoding = 'utf-8'
         return self.validation(request.GET)
@@ -31,14 +25,15 @@ class PowerWarningView(View):
         request.encoding = 'utf-8'
         return self.validation(request.POST)
 
-    def validation(self, request_dict):
+    @staticmethod
+    def validation(request_dict):
         logger = logging.getLogger('info')
         uid = request_dict.get('uid', None)
 
         # 限制每6小时推一次
-        redisObj = RedisObject()
-        is_limit = redisObj.CONN.setnx(uid+'limit_power_warning', 1)
-        redisObj.CONN.expire(uid+'limit_power_warning', 6*60*60)
+        redis_obj = RedisObject()
+        is_limit = redis_obj.CONN.setnx(uid + 'limit_power_warning', 1)
+        redis_obj.CONN.expire(uid + 'limit_power_warning', 6 * 60 * 60)
         if not is_limit:
             return JsonResponse(status=200, data={'code': 0, 'msg': 'push limited!'})
 
@@ -47,190 +42,75 @@ class PowerWarningView(View):
         logger.info('调用低电量推送接口的uid: {},electricity: {}'.format(uid, electricity))
         try:
             uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
-                values('token_val', 'app_type', 'appBundleId', 'm_code',
-                       'push_type', 'userID_id', 'userID__NickName',
-                       'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
-                       'uid_set__channel')
+                values('push_type', 'appBundleId', 'token_val', 'lang', 'tz', 'userID_id')
             if not uid_push_qs.exists():
                 res_data = {'code': 173, 'msg': 'uid push data not exit!'}
                 return JsonResponse(status=200, data=res_data)
 
-            # 新建一个list接收数据
-            redis_list = []
-            # 把数据库数据追加进redis_list
-            for qs in uid_push_qs:
-                redis_list.append(qs)
-
-            if not redis_list:
-                res_data = {'code': 0, 'msg': 'no redis_list success!'}
-                return JsonResponse(status=200, data=res_data)
-
-            nickname = redis_list[0]['uid_set__nickname']
-            if not nickname:
-                nickname = uid
+            uid_push_list = [uid_push for uid_push in uid_push_qs]
+            # 查询设备数据
+            device_info_qs = Device_Info.objects.filter(UID=uid).first()
+            nickname = uid if device_info_qs is None else device_info_qs.NickName
 
             now_time = int(time.time())
-            channel = channel
             event_type = 704
             sys_msg_list = []
-            userID_ids = []
-            kwag_args = {
-                'uid': uid,
-                'channel': channel,
-                'event_type': event_type,
-                'n_time': now_time,
-            }
-
-            for up in redis_list:
-                push_type = up['push_type']
-                appBundleId = up['appBundleId']
-                token_val = up['token_val']
-                lang = up['lang']
-                tz = up['tz']
+            user_id_list = []
+
+            for uid_push in uid_push_list:
+                push_type = uid_push['push_type']
+                app_bundle_id = uid_push['appBundleId']
+                token_val = uid_push['token_val']
+                lang = uid_push['lang']
+                tz = uid_push['tz']
                 if tz is None or tz == '':
                     tz = 0
-                # 发送标题
-                msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
-                # 发送内容
-                msg_text = self.get_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
-                                             event_type=event_type, electricity=electricity)
-                kwag_args['appBundleId'] = appBundleId
-                kwag_args['token_val'] = token_val
-                kwag_args['msg_title'] = msg_title
-                kwag_args['msg_text'] = msg_text
-
-                if push_type == 0:  # ios apns
-                    self.do_apns(**kwag_args)
-                elif push_type == 1:  # android gcm
-                    self.do_fcm(**kwag_args)
-                elif push_type == 2:  # android jpush
-                    self.do_jpush(**kwag_args)
 
-                # 以下是存库
-                userID_id = up["userID_id"]
-                if userID_id not in userID_ids:
-                    sys_msg_text = self.get_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
-                                                     event_type=event_type, is_sys=1, electricity=electricity)
+                # 推送标题和推送内容
+                msg_title = PushObject.get_msg_title(app_bundle_id=app_bundle_id, nickname=nickname)
+                msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
+                                                             electricity=electricity)
+
+                kwargs = {
+                    'nickname': nickname,
+                    'app_bundle_id': app_bundle_id,
+                    'token_val': token_val,
+                    'n_time': now_time,
+                    'event_type': event_type,
+                    'msg_title': msg_title,
+                    'msg_text': msg_text,
+                    'uid': uid,
+                    'channel': channel,
+                }
+
+                try:
+                    # 推送消息
+                    if push_type == 0:  # ios apns
+                        PushObject.ios_apns_push(**kwargs)
+                    elif push_type == 1:  # android gcm
+                        PushObject.android_fcm_push(**kwargs)
+                    elif push_type == 2:  # android jpush
+                        PushObject.android_jpush(**kwargs)
+                except Exception as e:
+                    logger.info('低电量推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+                    continue
+
+                # 保存系统消息
+                user_id = uid_push['userID_id']
+                if user_id not in user_id_list:
+                    sys_msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
+                                                                     electricity=electricity, is_sys=1)
                     sys_msg_list.append(SysMsgModel(
-                        userID_id=userID_id,
+                        userID_id=user_id,
                         msg=sys_msg_text,
                         addTime=now_time,
                         updTime=now_time,
                         uid=uid,
                         eventType=event_type,
                     ))
-                    userID_ids.append(userID_id)
+                    user_id_list.append(user_id)
             SysMsgModel.objects.bulk_create(sys_msg_list)
             return JsonResponse(status=200, data={'code': 0})
         except Exception as e:
             logger.info('低电量推送接口异常: {}'.format(e))
             return JsonResponse(status=500, data={'msg': 'power warning error'})
-
-    def get_msg_title(self, appBundleId, nickname):
-        package_title_config = {
-            'com.ansjer.customizedd_a': 'DVS',
-            'com.ansjer.zccloud_a': 'ZosiSmart',
-            'com.ansjer.zccloud_ab': '周视',
-            'com.ansjer.adcloud_a': 'ADCloud',
-            'com.ansjer.adcloud_ab': 'ADCloud',
-            'com.ansjer.accloud_a': 'ACCloud',
-            'com.ansjer.loocamccloud_a': 'Loocam',
-            'com.ansjer.loocamdcloud_a': 'Anlapus',
-            'com.ansjer.customizedb_a': 'COCOONHD',
-            'com.ansjer.customizeda_a': 'Guardian365',
-            'com.ansjer.customizedc_a': 'PatrolSecure',
-        }
-        if appBundleId in package_title_config.keys():
-            return package_title_config[appBundleId] + '(' + nickname + ')'
-        else:
-            return nickname
-
-    def is_sys_msg(self, event_type):
-        event_type_list = [702, 703, 704]
-        if event_type in event_type_list:
-            return True
-        return False
-
-    def get_msg_text(self, channel, n_time, lang, tz, event_type, electricity, is_sys=0):
-        n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
-        etype = int(event_type)
-        if lang == 'cn':
-            if etype == 704:
-                msg_type = '剩余电量:' + electricity
-            elif etype == 702:
-                msg_type = '摄像头休眠'
-            elif etype == 703:
-                msg_type = '摄像头唤醒'
-            else:
-                msg_type = ''
-            if is_sys:
-                send_text = '{msg_type} 通道:{channel}'.format(msg_type=msg_type, channel=channel)
-            else:
-                send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
-        else:
-            if etype == 704:
-                msg_type = 'Battery remaining:' + electricity
-            elif etype == 702:
-                msg_type = 'Camera sleep'
-            elif etype == 703:
-                msg_type = 'Camera wake'
-            else:
-                msg_type = ''
-            if is_sys:
-                send_text = '{msg_type} channel:{channel}'. \
-                    format(msg_type=msg_type, channel=channel)
-            else:
-                send_text = '{msg_type} channel:{channel} date:{date}'. \
-                    format(msg_type=msg_type, channel=channel, date=n_date)
-        return send_text
-
-    def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
-        app_key = JPUSH_CONFIG[appBundleId]['Key']
-        master_secret = JPUSH_CONFIG[appBundleId]['Secret']
-        _jpush = jpush.JPush(app_key, master_secret)
-        push = _jpush.create_push()
-        push.audience = jpush.registration_id(token_val)
-        push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
-                     "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
-        android = jpush.android(alert=msg_text, priority=1, style=1, alert_type=7,
-                                big_text=msg_text, title=msg_title,
-                                extras=push_data)
-        push.notification = jpush.notification(android=android)
-        push.platform = jpush.all_
-        res = push.send()
-        return res.status_code
-
-    def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
-        try:
-            serverKey = FCM_CONFIG[appBundleId]
-        except Exception as e:
-            return 'serverKey abnormal'
-        push_service = FCMNotification(api_key=serverKey)
-        data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
-                "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
-        result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
-                                                   message_body=msg_text, data_message=data,
-                                                   extra_kwargs={
-                                                       'default_vibrate_timings': True,
-                                                       'default_sound': True,
-                                                       'default_light_settings': True
-                                                   })
-        return result
-
-    def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
-        try:
-            cli = apns2.APNSClient(mode=APNS_MODE, client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
-            push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
-                         "received_at": n_time, "sound": "", "uid": uid, "zpush": "1", "channel": channel}
-            alert = apns2.PayloadAlert(body=msg_text, title=msg_title)
-            payload = apns2.Payload(alert=alert, custom=push_data, sound="default")
-            n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
-            res = cli.push(n=n, device_token=token_val, topic=appBundleId)
-            if res.status_code == 200:
-                return res.status_code
-            else:
-                return res.status_code
-        except (ValueError, ArithmeticError):
-            return 'The program has a numeric format exception, one of the arithmetic exceptions'
-        except Exception as e:
-            return repr(e)

+ 67 - 6
Service/PushService.py

@@ -16,9 +16,8 @@ from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG,
 from Service.CommonService import CommonService
 
 
-# 推送对象
 class PushObject:
-
+    # 推送对象
     @staticmethod
     def get_msg_title(app_bundle_id, nickname):
         """
@@ -70,10 +69,50 @@ class PushObject:
             ai_msg_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
         return ai_msg_text
 
-    # ios apns 推送
+    @staticmethod
+    def get_low_power_msg_text(channel, n_time, lang, tz, electricity, is_sys=0):
+        """
+        获取低电量推送内容
+        @param channel: 通道
+        @param n_time: 当前时间
+        @param lang: 语言
+        @param tz: 时区
+        @param electricity: 电量
+        @param is_sys: 是否为系统消息
+        @return: low_power_msg_text
+        """
+        n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
+        if lang == 'cn':
+            alarm = '剩余电量:' + electricity
+            if is_sys:
+                low_power_msg_text = '{} 通道:{}'.format(alarm, channel)
+            else:
+                low_power_msg_text = '{} 通道:{} 日期:{}'.format(alarm, channel, n_date)
+        else:
+            alarm = 'Battery remaining:' + electricity
+            if is_sys:
+                low_power_msg_text = '{} channel:{}'.format(alarm, channel)
+            else:
+                low_power_msg_text = '{} channel:{} date:{}'.format(alarm, channel, n_date)
+        return low_power_msg_text
+
     @staticmethod
     def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
                       uid='', channel='1', launch_image=None):
+        """
+        ios apns 推送
+        @param nickname: 设备昵称
+        @param app_bundle_id: app包id
+        @param token_val: 推送token
+        @param n_time: 当前时间
+        @param event_type: 事件类型
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @param uid: uid
+        @param channel: 通道
+        @param launch_image: 推送图片链接
+        @return: None
+        """
         logger = logging.getLogger('info')
         try:
             pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
@@ -93,10 +132,23 @@ class PushObject:
             logger.info('--->IOS推送异常{}'.format(repr(e)))
             return repr(e)
 
-    # android fcm 推送
     @staticmethod
     def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
                          uid='', channel='1', image=''):
+        """
+        android fcm 推送
+        @param nickname: 设备昵称
+        @param app_bundle_id: app包id
+        @param token_val: 推送token
+        @param n_time: 当前时间
+        @param event_type: 事件类型
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @param uid: uid
+        @param channel: 通道
+        @param image: 推送图片链接
+        @return: None
+        """
         logger = logging.getLogger('info')
         try:
             serverKey = FCM_CONFIG[app_bundle_id]
@@ -113,13 +165,22 @@ class PushObject:
                                                                      }
                                                        )
             logger.info('fcm推送结果:{}'.format(result))
-            return result
         except Exception as e:
             return repr(e)
 
-    # android 极光 推送
     @staticmethod
     def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
+        """
+        android 极光 推送
+        @param nickname: 设备昵称
+        @param app_bundle_id: app包id
+        @param token_val: 推送token
+        @param n_time: 当前时间
+        @param event_type: 事件类型
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @return: None
+        """
         try:
             app_key = JPUSH_CONFIG[app_bundle_id]['Key']
             master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']