Kaynağa Gözat

更新测试服代码

locky 1 yıl önce
ebeveyn
işleme
d9fb6d69b1

+ 4 - 1
AnsjerPush/Config/aiConfig.py

@@ -34,5 +34,8 @@ DEVICE_EVENT_TYPE = {
     512: 65,
     1024: 66,
     2048: 67,
-    4096: 68
+    4096: 68,
+    8192: 69
 }
+
+ALGORITHM_COMBO_TYPES = [51, 57, 58, 60, 59, 61, 62, 63, 64, 65, 66, 67, 68, 69]

+ 1 - 0
AnsjerPush/config.py

@@ -135,6 +135,7 @@ APP_BUNDLE_DICT = {
     'com.ansjer.customizedc_a': 'PatrolSecure',
 }
 
+SYS_EVENT_TYPE_LIST = [702, 703, 704]
 MULTI_CHANNEL_TYPE_LIST = [1, 2, 3, 4, 10001]
 
 # AI识别标签

+ 31 - 11
AnsjerPush/test_config/test_settings.py

@@ -53,15 +53,15 @@ WSGI_APPLICATION = 'AnsjerPush.wsgi.application'
 
 # 业务数据库
 DATABASE_DATA = 'ansjer_server_test'
-SERVER_HOST = 'server-test.cvp7gfpnmziz.rds.cn-northwest-1.amazonaws.com.cn'
+SERVER_HOST = 'server-cn.cvp7gfpnmziz.rds.cn-northwest-1.amazonaws.com.cn'
 DATABASES_USER = 'aws_rds'
-DATABASES_PASS = 'tIPV7Dhvi8LDageBIrsI'
+DATABASES_PASS = 'H84NQ8NARr9e39tn6aW5'
 
 # 推送数据库
 DATABASE_DATA2 = 'ansjer_push_test'
-SERVER_HOST2 = 'push-test.cvp7gfpnmziz.rds.cn-northwest-1.amazonaws.com.cn'
+SERVER_HOST2 = 'push-cn.cvp7gfpnmziz.rds.cn-northwest-1.amazonaws.com.cn'
 DATABASES_USER2 = 'aws_rds'
-DATABASES_PASS2 = 'p7XolzlQoOJ2YqvBqogr'
+DATABASES_PASS2 = 'Dil02uKDyd5Mxv7fhhHJ'
 
 DATABASES = {
     'default': {
@@ -71,8 +71,12 @@ DATABASES = {
         'PASSWORD': DATABASES_PASS,
         'HOST': SERVER_HOST,
         'PORT': '3306',
-        'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
-        'AUTOCOMMIT': True
+        'AUTOCOMMIT': True,
+        'CONN_MAX_AGE': 60,
+        'OPTIONS': {'charset': 'utf8mb4',
+                    'use_unicode': True,
+                    'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
+                    }
     },
     'mysql02': {
         'ENGINE': 'django.db.backends.mysql',
@@ -81,8 +85,12 @@ DATABASES = {
         'PASSWORD': DATABASES_PASS2,
         'HOST': SERVER_HOST2,
         'PORT': '3306',
-        'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
-        'AUTOCOMMIT': True
+        'AUTOCOMMIT': True,
+        'CONN_MAX_AGE': 60,
+        'OPTIONS': {'charset': 'utf8mb4',
+                    'use_unicode': True,
+                    'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
+                    }
     }
 }
 DATABASE_ROUTERS = ['AnsjerPush.database_router.DatabaseAppsRouter']
@@ -162,7 +170,6 @@ LOGGING = {
     'disable_existing_loggers': True,
     'formatters': {
         'error_format': {
-            # 'format': '{"asctime":"%(asctime)s","thread":"%(threadName)s:%(thread)d","errorline":"%(lineno)d","errorlevel":"%(levelname)s","errorcontent":"%(message)s"}'
             'format': '%(asctime)s %(threadName)s %(thread)d %(lineno)d %(levelname)s %(message)s'
         },
         'standard': {
@@ -180,7 +187,7 @@ LOGGING = {
         'default': {
             'level': 'ERROR',
             'class': 'logging.handlers.RotatingFileHandler',
-            'filename': BASE_DIR + '/static/log/error.log',
+            'filename': BASE_DIR + '/static/log/error/error.log',
             'maxBytes': 1024 * 1024 * 5,  # 5 MB
             'backupCount': 5,
             'formatter': 'error_format',
@@ -199,11 +206,19 @@ LOGGING = {
             'formatter': 'standard',
             'encoding': 'utf-8',
         },
+        'time': {
+            'level': 'INFO',
+            'class': 'logging.handlers.TimedRotatingFileHandler',
+            'filename': BASE_DIR + '/static/log/time/info.log',
+            'backupCount': 30,
+            'when': 'D',
+            'formatter': 'standard',
+            'encoding': 'utf-8',
+        },
     },
     'loggers': {
         'django': {
             'handlers': ['default', 'console'],
-            # 'handlers': ['mail_admins','default','console'],
             'level': 'ERROR',
             'propagate': True
         },
@@ -212,6 +227,11 @@ LOGGING = {
             'handlers': ['info'],
             'level': 'INFO',
             'propagate': False
+        },
+        'time': {
+            'handlers': ['time'],
+            'level': 'INFO',
+            'propagate': False
         }
     }
 }

+ 4 - 5
Controller/DetectController.py

@@ -42,13 +42,11 @@ class NotificationView(View):
         n_time = request_dict.get('n_time', None)
         event_type = request_dict.get('event_type', None)
         is_st = request_dict.get('is_st', None)
-        uid = request_dict.get('uid', None)
         if not all([channel, n_time]):
             return JsonResponse(status=200, data={'code': 444, 'msg': 'error channel or n_time'})
         redisObj = RedisObject(db=6)
         try:
-            if not uid:
-                uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
+            uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
             if len(uid) != 20 and len(uid) != 14:
                 return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
             logger.info("旧移动侦测接口的uid:{}".format(uid))
@@ -83,7 +81,7 @@ class NotificationView(View):
                     logger.info('消息推送-uid_push 数据不存在')
                     return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
                 # 修改redis数据,并设置过期时间为10分钟
-                uid_push_list = DevicePushService.cache_uid_push(uid_push_qs)
+                uid_push_list = DevicePushService.qs_to_list(uid_push_qs)
                 redisObj.set_data(key=ykey, val=str(uid_push_list), expire=600)
                 if not uid_push_list:
                     res_data = {'code': 404, 'msg': 'error !'}
@@ -124,7 +122,8 @@ class NotificationView(View):
             params = {'nickname': nickname, 'uid': uid, 'kwag_args': kwag_args, 'is_st': is_st,
                       'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
                       'electricity': '', 'bucket': bucket, 'app_push': '', 'storage_location': 1, 'ai_type': 0,
-                      'dealings_type': 0, 'detection': 0, 'device_type': 1}
+                      'dealings_type': 0, 'detection': 0, 'device_type': 1,
+                      'app_push_config': ''}
             #  推送以及报警消息存库
             result = DevicePushService.save_msg_push(uid_set_push_list=uid_push_list, **params)
             if result['code_date'] is None:

+ 64 - 262
Controller/DetectControllerV2.py

@@ -1,22 +1,13 @@
 import json
 import logging
-import os
+import threading
 
-import apns2
-import boto3
-import botocore
-import jpush as jpush
-from botocore import client
 from django.http import JsonResponse
 from django.views.generic.base import View
-from pyfcm import FCMNotification
 
-from AnsjerPush.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
-from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE
 from Object.RedisObject import RedisObject
-from Service.CommonService import CommonService
 from Service.DevicePushService import DevicePushService
-from Service.PushService import PushObject
+TIME_LOGGER = logging.getLogger('time')
 
 
 # 移动侦测V2接口
@@ -30,7 +21,8 @@ class NotificationV2View(View):
         request.encoding = 'utf-8'
         return self.validation(request.POST)
 
-    def validation(self, request_dict):
+    @staticmethod
+    def validation(request_dict):
         """
         设备触发报警消息推送
         @param request_dict:uidToken 加密uid
@@ -39,11 +31,15 @@ class NotificationV2View(View):
         @param request_dict:n_time 设备触发报警时间
         @param request_dict:event_type 设备事件类型
         @param request_dict:is_st 文件类型(0:无,1:图片,2:视频)
-        @param request_dict:region 文件存储区域(1:国外,2国内)
+        @param request_dict:region 文件存储区域(1:国外, 2:国内)
         @param request_dict:electricity 电量值
+        @param request_dict:time_token 时间戳token
+        @param request_dict:uid uid
+        @param request_dict:dealings_type 往来检测 1:来,2:离开
+        @param request_dict:detection 检测类型 0:普通,1:算法
         """
         logger = logging.getLogger('info')
-        logger.info('移动侦测V2接口参数:{}'.format(request_dict))
+        logger.info('消息推送V2接口参数:{}'.format(request_dict))
         uidToken = request_dict.get('uidToken', None)
         etk = request_dict.get('etk', None)
         channel = request_dict.get('channel', '1')
@@ -52,74 +48,70 @@ class NotificationV2View(View):
         is_st = request_dict.get('is_st', None)
         region = request_dict.get('region', None)
         electricity = request_dict.get('electricity', '')
-        time_token = request_dict.get('time_token', None)
-        uid = request_dict.get('uid', None)
-        # 往来检测 1:来,2:离开
-        dealings_type = request_dict.get('dealingsType', 0)
-        # 检测类型 0:普通,1:算法
+        dealings_type = int(request_dict.get('dealingsType', 0))
         detection = int(request_dict.get('detection', 0))
 
+        # 参数校验
         if not all([channel, n_time]):
-            return JsonResponse(status=200, data={
-                'code': 444,
-                'msg': 'param is wrong'})
+            return JsonResponse(status=200, data={'code': 444, 'msg': 'param is wrong'})
         if not region or not is_st:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'no region or is_st'})
-        # 时间戳token校验
-        if time_token:
-            if not CommonService.check_time_stamp_token(time_token, n_time):
-                return JsonResponse(status=200, data={'code': 13, 'msg': 'Timestamp token verification failed'})
-        redisObj = RedisObject(db=6)
+
+        is_st = int(is_st)
+        region = int(region)
+        event_type = int(event_type)
+        redis_obj = RedisObject()
         try:
-            is_st = int(is_st)
-            region = int(region)
-            event_type = int(event_type)
-            if not uid:
-                uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
+            uid = DevicePushService.decode_uid(etk, uidToken)
             if len(uid) != 20 and len(uid) != 14:
                 return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
-            req_limiting = '{uid}_{channel}_{event_type}_ptl' \
-                .format(uid=uid, channel=channel, event_type=event_type)
-            is_sys_msg = self.is_sys_msg(int(event_type))  # 判断事件类型是否是系统消息
+            TIME_LOGGER.info('开始推送,uid:{},n_time:{},事件类型:{}'.format(uid, n_time, event_type))
+            # 判断是否为系统消息
+            is_sys_msg = DevicePushService.judge_sys_msg(int(event_type))
             if is_sys_msg:
-                push_interval = '{uid}_{channel}_{event_type}_flag' \
-                    .format(uid=uid, channel=channel, event_type=event_type)
+                push_interval = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, channel=channel,
+                                                                           event_type=event_type)
             else:
                 push_interval = '{uid}_{channel}_flag'.format(uid=uid, channel=channel)
 
-            cache_req_limiting = redisObj.get_data(key=req_limiting)  # 获取请求限流缓存数据
-            cache_app_push = redisObj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
-            logger.info('消息推送- 限流key: {}, 推送间隔key: {}'.
-                        format(cache_req_limiting, cache_app_push))
-            if event_type != 606:
+            req_limiting = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, channel=channel, event_type=event_type)
+            cache_req_limiting = redis_obj.get_data(key=req_limiting)  # 获取请求限流缓存数据
+            cache_app_push = redis_obj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
+            if event_type not in [606, 607]:
                 if cache_req_limiting:  # 限流存在则直接返回
                     return JsonResponse(status=200, data={'code': 0, 'msg': 'Push again in one minute'})
-            redisObj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
-            uid_push_qs = DevicePushService.query_uid_push(uid, event_type)  # 查询uid_set与push数据列表
+            redis_obj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
+
+            # 查询uid_push和uid_set数据
+            uid_push_qs = DevicePushService.query_uid_push(uid, event_type)
             if not uid_push_qs.exists():
                 logger.info('消息推送-{}uid_push数据不存在'.format(uid))
                 return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
+
             ai_type = uid_push_qs.first()['uid_set__ai_type']
             device_type = uid_push_qs.first()['uid_set__device_type']
-            logger.info('ai_type: {}, device_type: {}'.format(ai_type, device_type))
-            # 将uid_set以及uid_push 转数组列表
-            uid_set_push_list = DevicePushService.cache_uid_push(uid_push_qs)
+
+            # uid_push_qs转存列表
+            uid_set_push_list = DevicePushService.qs_to_list(uid_push_qs)
             nickname = uid_set_push_list[0]['uid_set__nickname']
             nickname = uid if not nickname else nickname
+
             # APP消息提醒推送间隔
             detect_interval = uid_set_push_list[0]['uid_set__detect_interval']
-            if event_type != 606:
+            if event_type not in [606, 607]:
                 if not cache_app_push:
                     # 缓存APP提醒推送间隔 默认1分钟提醒一次
-                    DevicePushService.cache_push_detect_interval(redisObj, push_interval, detect_interval,
+                    DevicePushService.cache_push_detect_interval(redis_obj, push_interval, detect_interval,
                                                                  uid_set_push_list[0]['uid_set__new_detect_interval'])
             else:
                 cache_app_push = ''
+
             bucket = ''
             aws_s3_client = ''
             if is_st == 1 or is_st == 3:  # 使用aws s3
-                aws_s3_client = s3_client(region=region)
+                aws_s3_client = DevicePushService.get_s3_client(region=region)
                 bucket = 'foreignpush' if region == 1 else 'push'
+
             kwag_args = {
                 'uid': uid,
                 'channel': channel,
@@ -130,18 +122,22 @@ class NotificationV2View(View):
                       'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
                       'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
                       'app_push': cache_app_push, 'storage_location': 2, 'ai_type': ai_type, 'device_type': device_type,
-                      'dealings_type': int(dealings_type), 'detection': detection}
-            logger.info('推送数据参数:{}'.format(params))
-            # 推送消息,生成推送数据列表
-            result = DevicePushService.save_msg_push(uid_set_push_list, **params)
-            # 保存推送数据
-            DevicePushService.save_sys_msg(is_sys_msg, result['local_date_time'],
-                                           result['sys_msg_list'], result['new_device_info_list'])
+                      'dealings_type': dealings_type, 'detection': detection,
+                      'app_push_config': uid_set_push_list[0]['uid_set__msg_notify']}
+
+            # 异步推送消息和保存数据
+            push_thread = threading.Thread(
+                target=push_and_save_data,
+                args=(uid_set_push_list, is_sys_msg),
+                kwargs=params)
+            push_thread.start()
+
             params['aws_s3_client'] = aws_s3_client
             params['uid_set_push_list'] = uid_set_push_list
-            params['code_dict'] = result
+
             result_dict = DevicePushService.get_push_url(**params)  # 获取S3对象上传链接
-            logger.info('{}触发时间{},推送URL{}'.format(uid, n_time, json.dumps(result_dict)))
+            TIME_LOGGER.info('推送响应,uid:{},n_time:{},事件类型:{},响应:{}'.format(
+                uid, n_time, event_type, json.dumps(result_dict)))
             return JsonResponse(status=200, data=result_dict)
         except Exception as e:
             logger.info('V2推送接口异常, errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
@@ -151,208 +147,14 @@ class NotificationV2View(View):
             }
             return JsonResponse(status=200, data=json.dumps(data), safe=False)
 
-    @classmethod
-    def get_combo_msg_type(cls, ai_type, event_type):
-        """
-        获取组合类型,需判断组合类型
-        """
-        logger = logging.getLogger('info')
-        try:
-            if ai_type == 0:
-                return event_type
-            logger.info('LOG------算法小店组合类型十进制值:{}'.format(event_type))
-            # 设备十进制算法说明:1:移动侦测,2:人形,4:车型,8:人脸,16:宠物,32:异响,64:区域闯入,
-            # 128:区域闯出,256:徘徊检测,512:长时间无人检测
-            event_dict = {
-                1: 51,
-                2: 57,
-                4: 58,
-                16: 59,
-                8: 60,
-                32: 61,
-                64: 62,
-                128: 63,
-                256: 64,
-                512: 65
-            }
-            event_val = event_dict.get(event_type, 0)
-            # event_val == 0 没有匹配到单个值则认为组合类型
-            # 如是3,则转为二进制11,代表(1+2)触发了移动侦测+人形侦测
-            if event_val == 0:
-                val = cls.dec_to_bin(event_type)
-                return int(val)
-            else:
-                return int(event_val)
-        except Exception as e:
-            logger.info('推送错误异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
-            return event_type
-
-    @staticmethod
-    def dec_to_bin(num):
-        """
-        十进制转二进制
-        """
-        result = ""
-        while num != 0:
-            ret = num % 2
-            num //= 2
-            result = str(ret) + result
-        return result
-
-    def push_thread_test(self, push_type, aws_s3_client, bucket, key, uid, appBundleId, token_val, event_type, n_time,
-                         msg_title, msg_text, channel):
-        logger = logging.getLogger('info')
-        logger.info('推送图片测试:{} {} {} {} {} {} {} {}'.format(push_type, uid, appBundleId, token_val, event_type, n_time,
-                                                            msg_title, msg_text))
-        try:
-            image_url = aws_s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket, 'Key': key},
-                                                             ExpiresIn=300)
-            logger.info('推送图片url:{}'.format(image_url))
-            if push_type == 0:
-                PushObject.ios_apns_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text,
-                                         uid, channel, image_url)
-            elif push_type == 1:
-                PushObject.android_fcm_push(uid, appBundleId, token_val, n_time, event_type, msg_title,
-                                            msg_text, uid, channel, image_url)
-        except Exception as e:
-            logger.info('推送图片测试异常:{}'.format(e))
-
-    @staticmethod
-    def is_sys_msg(event_type):
-        """
-        判断是否属于系统消息
-        @return: True | False
-        """
-        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()
-        print(res)
-        return res.status_code
-
-    def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
-        logger = logging.getLogger('info')
-        try:
-            serverKey = FCM_CONFIG[appBundleId]
-        except Exception as e:
-            logger.info('------fcm_error:{}'.format(repr(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):
-        logger = logging.getLogger('info')
-        logger.info("进来do_apns函数了")
-        logger.info(token_val)
-        logger.info(APNS_MODE)
-        logger.info(os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
-        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")
-
-            # return uid, channel, appBundleId, str(token_val), event_type, n_time, msg_title,msg_text
-            n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
-            res = cli.push(n=n, device_token=token_val, topic=appBundleId)
-            print(res.status_code)
-            logger.info("apns_推送状态:")
-            logger.info(res.status_code)
-
-            if res.status_code == 200:
-                return res.status_code
-            else:
-                print('apns push fail')
-                print(res.reason)
-                logger.info('apns push fail')
-                logger.info(res.reason)
-                return res.status_code
-        except (ValueError, ArithmeticError):
-            return 'The program has a numeric format exception, one of the arithmetic exceptions'
-        except Exception as e:
-            print(repr(e))
-            print('do_apns函数错误行号', e.__traceback__.tb_lineno)
-            logger.info('do_apns错误:{}'.format(repr(e)))
-            return repr(e)
 
+def push_and_save_data(uid_set_push_list, is_sys_msg, **params):
+    # 推送消息,生成推送数据列表
+    result = DevicePushService.save_msg_push(uid_set_push_list, **params)
+    # 保存推送数据
+    DevicePushService.save_sys_msg(
+        is_sys_msg,
+        result['local_date_time'],
+        result['sys_msg_list'],
+        result['new_device_info_list'])
 
-def s3_client(region):
-    if region == 2:  # 国内
-        aws_s3_client = boto3.client(
-            's3',
-            aws_access_key_id=AWS_ACCESS_KEY_ID[0],
-            aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
-            config=botocore.client.Config(signature_version='s3v4'),
-            region_name='cn-northwest-1'
-        )
-    else:  # 国外
-        aws_s3_client = boto3.client(
-            's3',
-            aws_access_key_id=AWS_ACCESS_KEY_ID[1],
-            aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
-            config=botocore.client.Config(signature_version='s3v4'),
-            region_name='us-east-1'
-        )
-    return aws_s3_client

+ 1 - 0
Model/models.py

@@ -1178,6 +1178,7 @@ class UidSetModel(models.Model):
     tb_country = models.IntegerField(blank=True, default=0, verbose_name='国家')
     device_type = models.SmallIntegerField(default=0, verbose_name='设备类型')
     ai_type = models.SmallIntegerField(default=0, verbose_name='检测类型')
+    msg_notify = models.JSONField(null=True, verbose_name='消息通知Json')
 
     class Meta:
         db_table = 'uid_set'

+ 15 - 1
Object/utils/LocalDateTimeUtil.py

@@ -8,9 +8,9 @@
 # @File    : LocalDateTimeUtil.py
 # @Software: PyCharm
 import datetime
-import logging
 import time
 import pendulum
+from datetime import datetime as dt2
 
 
 def get_last_first_date_and_last_date(n):
@@ -71,3 +71,17 @@ def date_to_week(str_date):
     return datetime.datetime.now().weekday() + 1
 
 
+def convert_time_to_seconds(date_time):
+    # 将时间字符串转换为datetime对象
+    dt_obj = dt2.strptime(date_time, '%Y-%m-%d %H:%M:%S')
+
+    # 提取小时、分钟和秒数
+    hours = dt_obj.hour
+    minutes = dt_obj.minute
+    seconds = dt_obj.second
+
+    # 计算总秒数
+    total_seconds = hours * 3600 + minutes * 60 + seconds
+
+    return total_seconds
+

+ 161 - 48
Service/DevicePushService.py

@@ -9,19 +9,17 @@
 import hashlib
 import json
 import logging
-import os
 import threading
 import time
 
-import apns2
-import jpush as jpush
+import boto3
+import botocore
 import requests
-from pyfcm import FCMNotification
 
-from AnsjerPush.Config.aiConfig import DEVICE_EVENT_TYPE
-from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, MULTI_CHANNEL_TYPE_LIST
-from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE, XMPUSH_CONFIG, OPPOPUSH_CONFIG, \
-    EVENT_DICT, EVENT_DICT_CN
+from AnsjerPush.Config.aiConfig import DEVICE_EVENT_TYPE, ALGORITHM_COMBO_TYPES
+from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, MULTI_CHANNEL_TYPE_LIST, SYS_EVENT_TYPE_LIST, AWS_ACCESS_KEY_ID, \
+    AWS_SECRET_ACCESS_KEY, EVENT_DICT, EVENT_DICT_CN
+from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
     DeviceChannelUserPermission, UidSetModel, Device_Info
 from Object.ETkObject import ETkObject
@@ -52,6 +50,42 @@ class DevicePushService:
         LOGGING.info('消息推送-当前UID:{}'.format(uid))
         return uid
 
+    @staticmethod
+    def judge_sys_msg(event_type):
+        """
+        判断是否属于系统消息
+        @param event_type: 事件类型
+        @return: bool
+        """
+        if event_type in SYS_EVENT_TYPE_LIST:
+            return True
+        return False
+
+    @staticmethod
+    def get_s3_client(region):
+        """
+        根据地区获取S3 client
+        @param region: 地区,1:国外, 2:国内
+        @return: aws_s3_client
+        """
+        if int(region) == 1:
+            aws_s3_client = boto3.client(
+                's3',
+                aws_access_key_id=AWS_ACCESS_KEY_ID[1],
+                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
+                config=botocore.client.Config(signature_version='s3v4'),
+                region_name='us-east-1'
+            )
+        else:
+            aws_s3_client = boto3.client(
+                's3',
+                aws_access_key_id=AWS_ACCESS_KEY_ID[0],
+                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
+                config=botocore.client.Config(signature_version='s3v4'),
+                region_name='cn-northwest-1'
+            )
+        return aws_s3_client
+
     @classmethod
     def query_uid_push(cls, uid, event_type):
         """
@@ -60,11 +94,12 @@ class DevicePushService:
         @param event_type: 事件类型
         @return: uid_push_qs
         """
-        if event_type != 606:
+        if event_type not in [606, 607]:
             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', 'uid_set__ai_type', 'uid_set__device_type', 'uid_set__new_detect_interval')
+                       'uid_set__channel', 'uid_set__ai_type', 'uid_set__device_type', 'uid_set__new_detect_interval',
+                       'uid_set__msg_notify')
         else:
             # 一键通话只推主用户
             device_info_qs = Device_Info.objects.filter(UID=uid).values('vodPrimaryUserID')
@@ -73,21 +108,20 @@ class DevicePushService:
                 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', 'uid_set__ai_type', 'uid_set__device_type', 'uid_set__new_detect_interval',
-                       'jg_token_val')
+                       'uid_set__msg_notify', 'jg_token_val')
         return uid_push_qs
 
     @staticmethod
-    def cache_uid_push(uid_push_qs):
+    def qs_to_list(qs):
         """
-        将uid_push 信息进行缓存
-        @param uid_push_qs: uid_set & uid_push 列表对象
-        @return: uid_set_list
+        qs对象转存列表
+        @param qs: query set对象
+        @return: qs_list
         """
-        uid_set_list = []
-        for qs in uid_push_qs:
-            uid_set_list.append(qs)
-        # redis_obj.set_data(key=name, val=str(redis_list), expire=expire)
-        return uid_set_list
+        qs_list = []
+        for i in qs:
+            qs_list.append(i)
+        return qs_list
 
     @staticmethod
     def cache_push_detect_interval(redis_obj, name, detect_interval, new_detect_interval):
@@ -121,6 +155,8 @@ class DevicePushService:
         # push_permission = True
         try:
             params['event_tag'] = cls.get_event_tag(params['ai_type'], params['event_type'], params['detection'])
+            is_app_push = True if params['event_tag'] in [606, 607] else \
+                cls.is_send_app_push(params['event_type'], params['event_tag'], params['app_push_config'])
             for up in uid_set_push_list:
                 appBundleId = up['appBundleId']
                 token_val = up['token_val']
@@ -140,7 +176,7 @@ class DevicePushService:
                 kwag_args['token_val'] = token_val
                 kwag_args['msg_title'] = msg_title
                 kwag_args['msg_text'] = msg_text
-                if params['event_type'] == 606 and up['push_type'] in [5, 6]:
+                if params['event_type'] in [606, 607] and up['push_type'] in [5, 6]:
                     kwag_args['jg_token_val'] = up['jg_token_val']
                 else:
                     if 'jg_token_val' in kwag_args:
@@ -171,7 +207,7 @@ class DevicePushService:
                 params['lang'] = lang
                 params['tz'] = tz
                 params['kwag_args'] = kwag_args
-                code_data = cls.send_app_msg_push(up['push_type'], **params)
+                code_data = cls.send_app_msg_push(up['push_type'], **params) if is_app_push else code_data
             return {'code_date': code_data, 'new_device_info_list': new_device_info_list, 'sys_msg_list': sys_msg_list,
                     'local_date_time': local_date_time}
         except Exception as e:
@@ -202,21 +238,19 @@ class DevicePushService:
         """
         获取设备算法组合类型
         51:移动侦测,52:传感器报警,53:影像遗失,54:PIR,55:门磁报警,56:外部发报,57:人型报警(提示:有人出现),58:车型,59:宠物,60:人脸,61:异响,
-        62:区域闯入,63:区域闯出,64:长时间无人检测,65:长时间无人检测,66:往来检测,67:哭声检测,68:手势检测
+        62:区域闯入,63:区域闯出,64:长时间无人检测,65:长时间无人检测,66:往来检测,67:哭声检测,68:手势检测,69:火焰检测
         0:代表空字符,702:摄像头休眠,703:摄像头唤醒,704:电量过低
         AWS AI识别 1:人形,2:车型,3:宠物,4:包裹。云端AI类型
         """
         try:
             types = []
-            combo_types = [51, 57, 58, 60, 59, 61,
-                           62, 63, 64, 65, 66, 67, 68]
             event_type = str(event_type)
             len_type = len(event_type)
             for i in range(len_type):
                 e_type = event_type[len_type - 1 - i]
                 if e_type == '1':
-                    types.append(str(combo_types[i]))
-            LOGGING.info('算法对照打印:{}'.format(combo_types))
+                    types.append(str(ALGORITHM_COMBO_TYPES[i]))
+            LOGGING.info('算法对照打印:{}'.format(ALGORITHM_COMBO_TYPES))
             return types
         except Exception as e:
             print('推送错误异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
@@ -606,27 +640,6 @@ class DevicePushService:
         re_data = {'code': 0, 'msg': 'success'}
         if params['is_st'] == 0 or params['is_st'] == 2:
             re_data['msg'] = 'success 0 or 2'
-            for up in params['uid_set_push_list']:
-                if up['push_type'] == 0:  # ios apns
-                    up['do_apns_code'] = params['code_dict']['code_date']['do_apns_code']
-                elif up['push_type'] == 1:  # android gcm
-                    up['do_fcm_code'] = params['code_dict']['code_date']['do_fcm_code']
-                elif up['push_type'] == 2:  # android jpush
-                    up['do_jpush_code'] = params['code_dict']['code_date']['do_jpush_code']
-                elif up['push_type'] == 4:  # android jpush
-                    up['do_xmpush_code'] = params['code_dict']['code_date']['do_xmpush_code']
-                elif up['push_type'] == 5:  # android jpush
-                    up['do_vivopush_code'] = params['code_dict']['code_date']['do_vivopush_code']
-                elif up['push_type'] == 7:  # android jpush
-                    up['do_meizupush_code'] = params['code_dict']['code_date']['do_meizupush_code']
-                del up['push_type']
-                del up['userID_id']
-                del up['userID__NickName']
-                del up['lang']
-                del up['tz']
-                del up['uid_set__nickname']
-                del up['uid_set__detect_interval']
-                del up['uid_set__detect_group']
             re_data['re_list'] = params['uid_set_push_list']
         elif params['is_st'] == 1:
             key_name = '{uid}/{channel}/{filename}.jpeg' \
@@ -713,3 +726,103 @@ class DevicePushService:
             return False
         c = [x for x in event_types if x in event_types2]
         return True if c else False
+
+    @staticmethod
+    def is_send_app_push(event_type, event_tag, app_push_config):
+        """
+        是否进行APP消息提醒
+        @return: True|False
+        """
+        try:
+            if not app_push_config:
+                return True
+
+            is_push = app_push_config['appPush']
+            if is_push != 1:  # 1:进行APP提醒,其它则不执行APP提醒
+                return False
+
+            all_day = app_push_config['pushTime']['allDay']
+            # 允许设备类型APP提醒列表
+            app_event_types = app_push_config['eventTypes']['device']
+
+            if all_day == 0:  # 1:全天提醒,0:自定义时间提醒
+                push_time_config = app_push_config['pushTime']
+                # 计算当前时间是否在自定义消息提醒范围内
+                if not DevicePushService.is_push_notify_allowed_now(push_time_config):
+                    LOGGING.info('APP推送提醒不在自定义时间内:{}'.format(push_time_config))
+                    return False
+            # APP接收提醒,判断识别类型是否勾选提醒
+            push_result = DevicePushService.is_type_push(event_type, event_tag, app_event_types)
+            LOGGING.info('APP推送提醒是否执行:{}'.format(push_result))
+            return push_result
+        except Exception as e:
+            LOGGING.info('判断是否执行APP推送异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return True
+
+    @staticmethod
+    def is_type_push(event_type, event_tag, app_event_types):
+        # 检查事件标签和应用事件类型是否都存在
+        if event_tag and app_event_types:
+            # 将事件标签按逗号分割成列表,并转换为整数类型
+            tag_list = [int(event) for event in event_tag.split(',') if event]
+            # 判断是否有任一标签允许应用提醒
+            return any(item in app_event_types for item in tag_list)
+
+        # 检查事件类型和用户所选事件类型是否都存在,并判断事件类型在用户所选事件类型列表中
+        return event_type and app_event_types and event_type in app_event_types
+
+    @staticmethod
+    def is_push_notify_allowed_now(push_time_config):
+        """
+        判断当前时间是否在允许APP推送提醒
+        """
+        now_time = int(time.time())
+        start_time = push_time_config['startTime']
+        end_time = push_time_config['endTime']
+        repeat = push_time_config['repeat']
+        tz = push_time_config['timeZone']
+
+        # 获取当前日期和周几
+        now_date, week = DevicePushService.get_now_date_and_week(now_time, tz)
+
+        # 判断是否在重复日范围内
+        if not DevicePushService.is_repeated(week, repeat):
+            return False
+
+        # 计算当前日期在一天中的秒数
+        seconds = LocalDateTimeUtil.convert_time_to_seconds(now_date)
+
+        # 判断是否在APP推送提醒范围内
+        return DevicePushService.is_in_effect(start_time, end_time, seconds)
+
+    @staticmethod
+    def is_in_effect(start, end, now_seconds):
+        """
+        判断是否在提醒时间范围内
+        @params: 开始时间秒
+        @params: 结束时间秒
+        @params: 当前时间秒
+        @return: 当前时间是在范围内返回True 否则False
+        """
+        if start < end:
+            return start <= now_seconds <= end
+        else:
+            return start <= now_seconds or now_seconds <= end
+
+    @staticmethod
+    def is_repeated(week_day, repeat_day):
+        """
+        判断是否重复日
+        @params: week_day 周几
+        @params: 重复日1-127
+        @return: 如果当前日期在重复日则返回True否则False
+        """
+        # 判断对应位置上的值是否为 1
+        is_repeat = (repeat_day >> (week_day - 1)) & 1 == 1
+        return is_repeat
+
+    @staticmethod
+    def get_now_date_and_week(now_time, tz):
+        now_data = CommonService.get_now_time_str(now_time, tz, 'cn')
+        week = LocalDateTimeUtil.date_to_week(now_data)
+        return now_data, week