瀏覽代碼

低电量推送接口添加打印

locky 3 年之前
父節點
當前提交
5c0fe2480d
共有 1 個文件被更改,包括 77 次插入83 次删除
  1. 77 83
      Controller/DetectControllerV2.py

+ 77 - 83
Controller/DetectControllerV2.py

@@ -698,7 +698,7 @@ class PushNotificationView(View):
 
 
 
-# 移动侦测接口
+# 低电量推送接口
 class PWnotificationView(View):
 
     def get(self, request, *args, **kwargs):
@@ -710,97 +710,91 @@ class PWnotificationView(View):
         return self.validation(request.POST)
 
     def validation(self, request_dict):
-
+        logger = logging.getLogger('info')
         uid = request_dict.get('uid', None)
         channel = request_dict.get('channel', None)
         electricity = request_dict.get('electricity', None)
-        uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
-            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')
-        print(uid_push_qs)
-        # 新建一个list接收数据
-        redis_list = []
-        # 把数据库数据追加进redis_list
-        for qs in uid_push_qs:
-            redis_list.append(qs)
-
-        if not redis_list:
-            print("没有redi_list")
-            res_data = {'code': 0, 'msg': 'no redi_list success!'}
-            return JsonResponse(status=200, data=res_data)
-
-        nickname = redis_list[0]['uid_set__nickname']
-        if not nickname:
-            nickname = uid
-
-        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,
-            # 'appBundleId': appBundleId,
-            # 'token_val': token_val,
-            # 'msg_title': msg_title,
-            # 'msg_text': msg_text
-        }
-
-
-        for up in redis_list:
-            push_type = up['push_type']
-            appBundleId = up['appBundleId']
-            token_val = up['token_val']
-            lang = up['lang']
-            tz = up['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
-                do_apns_code = self.do_apns(**kwag_args)
-            elif push_type == 1:  # android gcm
-                print('do_fcm')
-                do_fcm_code = self.do_fcm(**kwag_args)
-            elif push_type == 2:  # android jpush
-                print('do_jpush')
-                do_jpush_code = 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)
-                sys_msg_list.append(SysMsgModel(
-                    userID_id=userID_id,
-                    msg=sys_msg_text,
-                    addTime=now_time,
-                    updTime=now_time,
-                    uid=uid,
-                    eventType=event_type))
-
-                userID_ids.append(userID_id)
+        logger.info('调用低电量推送接口的uid: {},electricity: {}'.format(uid, electricity))
+        try:
+            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                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')
+            print(uid_push_qs)
+            # 新建一个list接收数据
+            redis_list = []
+            # 把数据库数据追加进redis_list
+            for qs in uid_push_qs:
+                redis_list.append(qs)
 
-        SysMsgModel.objects.bulk_create(sys_msg_list)
+            if not redis_list:
+                res_data = {'code': 0, 'msg': 'test: no redis_list success!'}
+                return JsonResponse(status=200, data=res_data)
 
+            nickname = redis_list[0]['uid_set__nickname']
+            if not nickname:
+                nickname = uid
 
-        return JsonResponse(status=200, data={'code': 0})
+            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']
+                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
+                    do_apns_code = self.do_apns(**kwag_args)
+                elif push_type == 1:  # android gcm
+                    print('do_fcm')
+                    do_fcm_code = self.do_fcm(**kwag_args)
+                elif push_type == 2:  # android jpush
+                    print('do_jpush')
+                    do_jpush_code = 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)
+                    sys_msg_list.append(SysMsgModel(
+                        userID_id=userID_id,
+                        msg=sys_msg_text,
+                        addTime=now_time,
+                        updTime=now_time,
+                        uid=uid,
+                        eventType=event_type,
+                        electricity=electricity,
+                    ))
+                    userID_ids.append(userID_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)
 
     def get_msg_title(self, appBundleId, nickname):
         package_title_config = {