Browse Source

app间隔推送优化

WuYiPei 5 years ago
parent
commit
6e7bbb4f90
1 changed files with 385 additions and 383 deletions
  1. 385 383
      Controller/DetectController.py

+ 385 - 383
Controller/DetectController.py

@@ -73,142 +73,58 @@ class NotificationView(View):
         redisObj = RedisObject(db=6)
         pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
         ykey = '{uid}_redis_qs'.format(uid=uid)
+        ckey = '{uid}_interval_group'.format(uid=uid)
         dkey = '{uid}_{event_type}_redis_qs'.format(uid=uid, event_type=event_type)
-        # 第二次,一分钟内不推,不存
-        if redisObj.get_data(key=pkey):
-            res_data = {'code': 0, 'msg': 'success!'}
-            return JsonResponse(status=200, data=res_data)
-
-        # 第一次,没有,没有dkey,也没有ykey
-        if not redisObj.get_data(key=dkey) and not redisObj.get_data(key=ykey):
-                print("第一次进来,设置dkey和pkey为interval秒")
-                # 设置推送时间为60秒一次
-                redisObj.set_data(key=pkey, val=1, expire=60)
-                print("从数据库查到数据")
-                # 从数据库查询出来
-                uid_push_qs = UidPushModel.objects.filter(uid_set__uid='HLK7EJ2VYLNHHUMG111A'). \
-                    values('token_val', 'app_type', 'appBundleId',
-                           'push_type', 'userID_id', 'lang',
-                           'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
-                # 用来判断是否推送
-                first_redis_list = 1
-                redis_list = []
-                # 把数据库数据追加进redis_list
-                for qs in uid_push_qs:
-                    redis_list.append(qs)
-                detect_interval = redis_list[0]['uid_set__detect_interval']
-                detect_group = redis_list[0]['uid_set__detect_group']
-                if detect_group:
-                    detect_group_list = detect_group.split(',')
-                    if event_type in detect_group_list:
-                        redisObj.set_data(key=dkey, val=1, expire=detect_interval)
-                # 把ykey的时间也设置成跟dkey一样
-                redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
-                if redis_list:
-                    nickname = redis_list[0]['uid_set__nickname']
-                    now_time = int(time.time())
-                    if not nickname:
-                        nickname = uid
-                    # 判断是否为系统类通知
-                    is_sys_msg = self.is_sys_msg(int(event_type))
-                    auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
-                    bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
-                    kwag_args = {
-                        'uid': uid,
-                        'channel': channel,
-                        'event_type': event_type,
-                        'n_time': n_time,
-                        # 'appBundleId': appBundleId,
-                        # 'token_val': token_val,
-                        # 'msg_title': msg_title,
-                        # 'msg_text': msg_text
-                    }
-                    eq_list = []
-                    sys_msg_list = []
-                    userID_ids = []
-                    for up in redis_list:
-                        push_type = up['push_type']
-                        appBundleId = up['appBundleId']
-                        token_val = up['token_val']
-                        lang = up['lang']
-                        tz = up['tz']
-                        # 发送标题
-                        msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
-                        # 发送内容
-                        msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                     event_type=event_type)
-                        kwag_args['appBundleId'] = appBundleId
-                        kwag_args['token_val'] = token_val
-                        kwag_args['msg_title'] = msg_title
-                        kwag_args['msg_text'] = msg_text
-                        #第一次进来,才推送
-                        if first_redis_list:
-                            print("第一次进来推送")
-                            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"]
-                        int_is_st = int(is_st)
-                        if userID_id not in userID_ids:
-                            eq_list.append(Equipment_Info(
-                                userID_id=userID_id,
-                                eventTime=n_time,
-                                eventType=event_type,
-                                devUid=uid,
-                                devNickName=nickname,
-                                Channel=channel,
-                                alarm='Motion \tChannel:{channel}'.format(channel=channel),
-                                is_st=int_is_st,
-                                receiveTime=n_time,
-                                addTime=now_time
-                            ))
-                            if is_sys_msg:
-                                sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                                 event_type=event_type, is_sys=1)
-                                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)
-
-                    if is_sys_msg:
-                        SysMsgModel.objects.bulk_create(sys_msg_list)
-                    Equipment_Info.objects.bulk_create(eq_list)
-                    # 以下是存库
-                    if is_st == '0' or is_st == '2':
-                        return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
-                    elif is_st == '1':
-                        # Endpoint以杭州为例,其它Region请按实际情况填写。
-                        obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
-                        # 设置此签名URL在60秒内有效。
-                        url = bucket.sign_url('PUT', obj, 7200)
-                        res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
-                        return JsonResponse(status=200, data=res_data)
-                    elif is_st == '3':
-                        # 人形检测带动图
-                        # Endpoint以杭州为例,其它Region请按实际情况填写。
-                        img_url_list = []
-                        for i in range(int(is_st)):
-                            obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
-                                format(uid=uid, channel=channel, filename=n_time, st=i)
-                            # 设置此签名URL在60秒内有效。
-                            url = bucket.sign_url('PUT', obj, 7200)
-                            img_url_list.append(url)
-                        res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
-                        return JsonResponse(status=200, data=res_data)
-
-        # 第三次,有dkey,也有ykey(一分钟外,10分钟内)
-        if redisObj.get_data(key=dkey) and redisObj.get_data(key=ykey):
-            redis_data = redisObj.get_data(key=ykey)
-            if redis_data:
-                first_redis_list = 0
-                redis_list = eval(redis_data)
+        have_push_pkey = redisObj.get_data(key=pkey)
+        have_push_dkey = redisObj.get_data(key=dkey)
+        have_push_ykey = redisObj.get_data(key=ykey)
+        have_push_ckey = redisObj.get_data(key=ckey)
+        #--------------------兼容旧代码--------------------
+        # 用缓存ckey判断兼容旧代码还是新代码的情况
+        if have_push_ckey:
+            print("从缓存ckey中获取detect_interval和detect_group")
+            check_list=eval(have_push_ckey)
+            detect_interval = check_list[0]['uid_set__detect_interval']
+            detect_group = check_list[0]['uid_set__detect_group']
+        else:
+            uid_push_cs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                values('token_val', 'app_type', 'appBundleId',
+                       'push_type', 'userID_id', 'lang',
+                       'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
+            check_list = []
+            # 把数据库数据追加进redis_list
+            for cs in uid_push_cs:
+                check_list.append(cs)
+            detect_interval = check_list[0]['uid_set__detect_interval']
+            detect_group = check_list[0]['uid_set__detect_group']
+            print(detect_group)
+            print(detect_interval)
+            redisObj.set_data(key=ckey, val=str(check_list), expire=3600)
+        # 走旧代码
+        if detect_interval and detect_group:
+            if redisObj.get_data(key=pkey):
+                res_data = {'code': 0, 'msg': 'success!'}
+                return JsonResponse(status=200, data=res_data)
+            else:
+                redis_data = redisObj.get_data(key=ykey)
+                if redis_data:
+                    redis_list = eval(redis_data)
+                else:
+                    # 设置推送时间为60秒一次
+                    redisObj.set_data(key=pkey, val=1, expire=60)
+                    print("从数据库查到数据")
+                    # 从数据库查询出来
+                    uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                        values('token_val', 'app_type', 'appBundleId',
+                               'push_type', 'userID_id', 'lang',
+                               'tz', 'uid_set__nickname')
+                    # 新建一个list接收数据
+                    redis_list = []
+                    # 把数据库数据追加进redis_list
+                    for qs in uid_push_qs:
+                        redis_list.append(qs)
+                    # 修改redis数据,并设置过期时间为10分钟
+                    redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
             if redis_list:
                 nickname = redis_list[0]['uid_set__nickname']
                 now_time = int(time.time())
@@ -246,15 +162,12 @@ class NotificationView(View):
                     kwag_args['token_val'] = token_val
                     kwag_args['msg_title'] = msg_title
                     kwag_args['msg_text'] = msg_text
-                    # 第一次进来,才推送
-                    if first_redis_list:
-                        print("第一次进来推送")
-                        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)
+                    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"]
                     int_is_st = int(is_st)
                     if userID_id not in userID_ids:
@@ -281,254 +194,343 @@ class NotificationView(View):
                                 uid=uid,
                                 eventType=event_type))
                         userID_ids.append(userID_id)
-
-                if is_sys_msg:
-                    SysMsgModel.objects.bulk_create(sys_msg_list)
-                Equipment_Info.objects.bulk_create(eq_list)
-                # 以下是存库
-                if is_st == '0' or is_st == '2':
-                    return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
-                elif is_st == '1':
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
-                    # 设置此签名URL在60秒内有效。
-                    url = bucket.sign_url('PUT', obj, 7200)
-                    res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
-                elif is_st == '3':
-                    # 人形检测带动图
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    img_url_list = []
-                    for i in range(int(is_st)):
-                        obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
-                            format(uid=uid, channel=channel, filename=n_time, st=i)
-                        # 设置此签名URL在60秒内有效。
-                        url = bucket.sign_url('PUT', obj, 7200)
-                        img_url_list.append(url)
-                    res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
-
-        #第四次,没有dkey,有ykey(1分钟外,10分钟内   )
-        if not redisObj.get_data(key=dkey) and  redisObj.get_data(key=ykey):
-            redis_data = redisObj.get_data(key=ykey)
-            if redis_data:
-                first_redis_list = 1
-                redis_list = eval(redis_data)
-                # 再次设置推送时间为60秒一次
-                redisObj.set_data(key=pkey, val=1, expire=60)
-                detect_interval = redis_list[0]['uid_set__detect_interval']
-                detect_group = redis_list[0]['uid_set__detect_group']
-                if detect_group:
-                    detect_group_list = detect_group.split(',')
-                    if event_type in detect_group_list:
-                        redisObj.set_data(key=dkey, val=1, expire=detect_interval)
-            if redis_list:
-                nickname = redis_list[0]['uid_set__nickname']
-                now_time = int(time.time())
-                if not nickname:
-                    nickname = uid
-                # 判断是否为系统类通知
-                is_sys_msg = self.is_sys_msg(int(event_type))
-                auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
-                bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
-                kwag_args = {
-                    'uid': uid,
-                    'channel': channel,
-                    'event_type': event_type,
-                    'n_time': n_time,
-                    # 'appBundleId': appBundleId,
-                    # 'token_val': token_val,
-                    # 'msg_title': msg_title,
-                    # 'msg_text': msg_text
-                }
-                eq_list = []
-                sys_msg_list = []
-                userID_ids = []
-                for up in redis_list:
-                    push_type = up['push_type']
-                    appBundleId = up['appBundleId']
-                    token_val = up['token_val']
-                    lang = up['lang']
-                    tz = up['tz']
-                    # 发送标题
-                    msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
-                    # 发送内容
-                    msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                 event_type=event_type)
-                    kwag_args['appBundleId'] = appBundleId
-                    kwag_args['token_val'] = token_val
-                    kwag_args['msg_title'] = msg_title
-                    kwag_args['msg_text'] = msg_text
-                    if first_redis_list:
-                        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"]
-                    int_is_st = int(is_st)
-                    if userID_id not in userID_ids:
-                        eq_list.append(Equipment_Info(
-                            userID_id=userID_id,
-                            eventTime=n_time,
-                            eventType=event_type,
-                            devUid=uid,
-                            devNickName=nickname,
-                            Channel=channel,
-                            alarm='Motion \tChannel:{channel}'.format(channel=channel),
-                            is_st=int_is_st,
-                            receiveTime=n_time,
-                            addTime=now_time
-                        ))
-                        if is_sys_msg:
-                            sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                             event_type=event_type, is_sys=1)
-                            sys_msg_list.append(SysMsgModel(
+                return self.save_data(uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list)
+            else:
+                return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
+        #--------------------/兼容旧代码--------------------
+        #新代码
+        else:
+            # 限定一分钟内不推,不存
+            if have_push_pkey:
+                res_data = {'code': 0, 'msg': 'success!'}
+                return JsonResponse(status=200, data=res_data)
+            # 没有,没有dkey,也没有ykey
+            if not have_push_dkey and not have_push_ykey:
+                    # 设置推送时间为60秒一次
+                    redisObj.set_data(key=pkey, val=1, expire=60)
+                    #------------测试
+                    # 从数据库查询出来
+                    uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                        values('token_val', 'app_type', 'appBundleId',
+                               'push_type', 'userID_id', 'lang',
+                               'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
+                    # 用来判断是否推送
+                    redis_list = []
+                    # 把数据库数据追加进redis_list
+                    for qs in uid_push_qs:
+                        redis_list.append(qs)
+                    detect_interval = redis_list[0]['uid_set__detect_interval']
+                    detect_group = redis_list[0]['uid_set__detect_group']
+
+                    if detect_group:
+                        detect_group_list = detect_group.split(',')
+                        if event_type in detect_group_list:
+                            redisObj.set_data(key=dkey, val=1, expire=detect_interval)
+                    redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
+                    if redis_list:
+                        nickname = redis_list[0]['uid_set__nickname']
+                        now_time = int(time.time())
+                        if not nickname:
+                            nickname = uid
+                        # 判断是否为系统类通知
+                        is_sys_msg = self.is_sys_msg(int(event_type))
+                        auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+                        bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
+                        kwag_args = {
+                            'uid': uid,
+                            'channel': channel,
+                            'event_type': event_type,
+                            'n_time': n_time,
+                            # 'appBundleId': appBundleId,
+                            # 'token_val': token_val,
+                            # 'msg_title': msg_title,
+                            # 'msg_text': msg_text
+                        }
+                        eq_list = []
+                        sys_msg_list = []
+                        userID_ids = []
+                        for up in redis_list:
+                            push_type = up['push_type']
+                            appBundleId = up['appBundleId']
+                            token_val = up['token_val']
+                            lang = up['lang']
+                            tz = up['tz']
+                            # 发送标题
+                            msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
+                            # 发送内容
+                            msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                         event_type=event_type)
+                            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"]
+                            int_is_st = int(is_st)
+                            if userID_id not in userID_ids:
+                                eq_list.append(Equipment_Info(
+                                    userID_id=userID_id,
+                                    eventTime=n_time,
+                                    eventType=event_type,
+                                    devUid=uid,
+                                    devNickName=nickname,
+                                    Channel=channel,
+                                    alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                                    is_st=int_is_st,
+                                    receiveTime=n_time,
+                                    addTime=now_time
+                                ))
+                                if is_sys_msg:
+                                    sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                                     event_type=event_type, is_sys=1)
+                                    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)
+                        return self.save_data(uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list)
+            # 有dkey,也有ykey(一分钟外,10分钟内)
+            if have_push_dkey and have_push_ykey:
+                redis_data = redisObj.get_data(key=ykey)
+                if redis_data:
+                    redis_list = eval(redis_data)
+                if redis_list:
+                    nickname = redis_list[0]['uid_set__nickname']
+                    now_time = int(time.time())
+                    if not nickname:
+                        nickname = uid
+                        # 判断是否为系统类通知
+                        is_sys_msg = self.is_sys_msg(int(event_type))
+                        auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+                        bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
+                        kwag_args = {
+                            'uid': uid,
+                            'channel': channel,
+                            'event_type': event_type,
+                            'n_time': n_time
+                        }
+                        eq_list = []
+                        sys_msg_list = []
+                        userID_ids = []
+                    for up in redis_list:
+                        lang = up['lang']
+                        tz = up['tz']
+                        userID_id = up["userID_id"]
+                        int_is_st = int(is_st)
+                        if userID_id not in userID_ids:
+                            eq_list.append(Equipment_Info(
                                 userID_id=userID_id,
-                                msg=sys_msg_text,
-                                addTime=now_time,
-                                updTime=now_time,
-                                uid=uid,
-                                eventType=event_type))
-                        userID_ids.append(userID_id)
-
-                if is_sys_msg:
-                    SysMsgModel.objects.bulk_create(sys_msg_list)
-                Equipment_Info.objects.bulk_create(eq_list)
-                # 以下是存库
-                if is_st == '0' or is_st == '2':
-                    return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
-                elif is_st == '1':
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
-                    # 设置此签名URL在60秒内有效。
-                    url = bucket.sign_url('PUT', obj, 7200)
-                    res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
-                elif is_st == '3':
-                    # 人形检测带动图
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    img_url_list = []
-                    for i in range(int(is_st)):
-                        obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
-                            format(uid=uid, channel=channel, filename=n_time, st=i)
-                        # 设置此签名URL在60秒内有效。
-                        url = bucket.sign_url('PUT', obj, 7200)
-                        img_url_list.append(url)
-                    res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
-
-        # 第五次,有dkey,没有ykey
-        if redisObj.get_data(key=dkey) and not redisObj.get_data(key=ykey):
-            uid_push_qs = UidPushModel.objects.filter(uid_set__uid='HLK7EJ2VYLNHHUMG111A'). \
-                values('token_val', 'app_type', 'appBundleId',
-                       'push_type', 'userID_id', 'lang',
-                       'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
-            redis_list = []
-            # 把数据库数据追加进redis_list
-            for qs in uid_push_qs:
-                redis_list.append(qs)
-            redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
-            first_redis_list = 0
-            if redis_list:
-                nickname = redis_list[0]['uid_set__nickname']
-                now_time = int(time.time())
-                if not nickname:
-                    nickname = uid
-                # 判断是否为系统类通知
-                is_sys_msg = self.is_sys_msg(int(event_type))
-                auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
-                bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
-                kwag_args = {
-                    'uid': uid,
-                    'channel': channel,
-                    'event_type': event_type,
-                    'n_time': n_time,
-                    # 'appBundleId': appBundleId,
-                    # 'token_val': token_val,
-                    # 'msg_title': msg_title,
-                    # 'msg_text': msg_text
-                }
-                eq_list = []
-                sys_msg_list = []
-                userID_ids = []
-                for up in redis_list:
-                    push_type = up['push_type']
-                    appBundleId = up['appBundleId']
-                    token_val = up['token_val']
-                    lang = up['lang']
-                    tz = up['tz']
-                    # 发送标题
-                    msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
-                    # 发送内容
-                    msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                 event_type=event_type)
-                    kwag_args['appBundleId'] = appBundleId
-                    kwag_args['token_val'] = token_val
-                    kwag_args['msg_title'] = msg_title
-                    kwag_args['msg_text'] = msg_text
-                    # 第一次进来,才推送
-                    if first_redis_list:
+                                eventTime=n_time,
+                                eventType=event_type,
+                                devUid=uid,
+                                devNickName=nickname,
+                                Channel=channel,
+                                alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                                is_st=int_is_st,
+                                receiveTime=n_time,
+                                addTime=now_time
+                            ))
+                            if is_sys_msg:
+                                sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                                 event_type=event_type, is_sys=1)
+                                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)
+                    return self.save_data(uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list)
+            #没有dkey,有ykey(interval分钟外,10分钟内),创建pkey,dkey,推送,存库
+            if not have_push_dkey and  have_push_ykey:
+                #-----------测试------------
+                print("aaaaaaa")
+                # -----------测试------------
+                redis_data = have_push_ykey
+
+                if redis_data:
+                    redis_list = eval(redis_data)
+                    # -----------测试------------
+                    print("输出redis_list")
+                    print(redis_list)
+                    # -----------/测试------------
+                    # 再次设置推送时间为60秒一次
+                    redisObj.set_data(key=pkey, val=1, expire=60)
+                    # -----------测试------------
+                    redisObj.del_data(pkey)
+                    # -----------/测试------------
+                    detect_interval = redis_list[0]['uid_set__detect_interval']
+                    detect_group = redis_list[0]['uid_set__detect_group']
+                    if detect_group:
+                        detect_group_list = detect_group.split(',')
+                        if event_type in detect_group_list:
+                            redisObj.set_data(key=dkey, val=1, expire=detect_interval)
+                if redis_list:
+                    nickname = redis_list[0]['uid_set__nickname']
+                    now_time = int(time.time())
+                    if not nickname:
+                        nickname = uid
+                    # 判断是否为系统类通知
+                    is_sys_msg = self.is_sys_msg(int(event_type))
+                    auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+                    bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
+                    kwag_args = {
+                        'uid': uid,
+                        'channel': channel,
+                        'event_type': event_type,
+                        'n_time': n_time,
+                    }
+                    eq_list = []
+                    sys_msg_list = []
+                    userID_ids = []
+                    for up in redis_list:
+                        push_type = up['push_type']
+                        appBundleId = up['appBundleId']
+                        token_val = up['token_val']
+                        lang = up['lang']
+                        tz = up['tz']
+                        print(lang)
+                        # 发送标题
+                        msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
+                        # 发送内容
+                        msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                     event_type=event_type)
+                        # -----------测试------------
+                        return JsonResponse(status=200, data={'code': 0, 'msg': 'have dkey,no ykey'})
+                        # -----------/测试------------
+                        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"]
-                    int_is_st = int(is_st)
-                    if userID_id not in userID_ids:
-                        eq_list.append(Equipment_Info(
-                            userID_id=userID_id,
-                            eventTime=n_time,
-                            eventType=event_type,
-                            devUid=uid,
-                            devNickName=nickname,
-                            Channel=channel,
-                            alarm='Motion \tChannel:{channel}'.format(channel=channel),
-                            is_st=int_is_st,
-                            receiveTime=n_time,
-                            addTime=now_time
-                        ))
-                        if is_sys_msg:
-                            sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                             event_type=event_type, is_sys=1)
-                            sys_msg_list.append(SysMsgModel(
+                        userID_id = up["userID_id"]
+                        int_is_st = int(is_st)
+                        if userID_id not in userID_ids:
+                            eq_list.append(Equipment_Info(
                                 userID_id=userID_id,
-                                msg=sys_msg_text,
-                                addTime=now_time,
-                                updTime=now_time,
-                                uid=uid,
-                                eventType=event_type))
-                        userID_ids.append(userID_id)
-
-                if is_sys_msg:
-                    SysMsgModel.objects.bulk_create(sys_msg_list)
-                Equipment_Info.objects.bulk_create(eq_list)
-                # 以下是存库
-                if is_st == '0' or is_st == '2':
-                    return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
-                elif is_st == '1':
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
-                    # 设置此签名URL在60秒内有效。
-                    url = bucket.sign_url('PUT', obj, 7200)
-                    res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
-                elif is_st == '3':
-                    # 人形检测带动图
-                    # Endpoint以杭州为例,其它Region请按实际情况填写。
-                    img_url_list = []
-                    for i in range(int(is_st)):
-                        obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
-                            format(uid=uid, channel=channel, filename=n_time, st=i)
-                        # 设置此签名URL在60秒内有效。
-                        url = bucket.sign_url('PUT', obj, 7200)
-                        img_url_list.append(url)
-                    res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
-                    return JsonResponse(status=200, data=res_data)
+                                eventTime=n_time,
+                                eventType=event_type,
+                                devUid=uid,
+                                devNickName=nickname,
+                                Channel=channel,
+                                alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                                is_st=int_is_st,
+                                receiveTime=n_time,
+                                addTime=now_time
+                            ))
+                            if is_sys_msg:
+                                sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                                 event_type=event_type, is_sys=1)
+                                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)
+                    return self.save_data(uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list)
+            #有dkey,没有ykey的情况
+            if have_push_dkey and not have_push_ykey:
+                uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                    values('token_val', 'app_type', 'appBundleId',
+                           'push_type', 'userID_id', 'lang',
+                           'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
+                redis_list = []
+                # 把数据库数据追加进redis_list
+                for qs in uid_push_qs:
+                    redis_list.append(qs)
+                redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
+                if redis_list:
+                    nickname = redis_list[0]['uid_set__nickname']
+                    now_time = int(time.time())
+                    if not nickname:
+                        nickname = uid
+                    # 判断是否为系统类通知
+                    is_sys_msg = self.is_sys_msg(int(event_type))
+                    auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+                    bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
+                    kwag_args = {
+                        'uid': uid,
+                        'channel': channel,
+                        'event_type': event_type,
+                        'n_time': n_time,
+                    }
+                    eq_list = []
+                    sys_msg_list = []
+                    userID_ids = []
+                    for up in redis_list:
+                        lang = up['lang']
+                        tz = up['tz']
+                        userID_id = up["userID_id"]
+                        int_is_st = int(is_st)
+                        if userID_id not in userID_ids:
+                            eq_list.append(Equipment_Info(
+                                userID_id=userID_id,
+                                eventTime=n_time,
+                                eventType=event_type,
+                                devUid=uid,
+                                devNickName=nickname,
+                                Channel=channel,
+                                alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                                is_st=int_is_st,
+                                receiveTime=n_time,
+                                addTime=now_time
+                            ))
+                            if is_sys_msg:
+                                sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                                 event_type=event_type, is_sys=1)
+                                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)
+                    return self.save_data(uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list)
 
+    # 封装存库部分代码
+    def save_data(self, uid, channel, n_time, is_sys_msg, eq_list, is_st, bucket, sys_msg_list):
+        if is_sys_msg:
+            SysMsgModel.objects.bulk_create(sys_msg_list)
+        Equipment_Info.objects.bulk_create(eq_list)
+        if is_st == '0' or is_st == '2':
+            return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
+        elif is_st == '1':
+            # Endpoint以杭州为例,其它Region请按实际情况填写。
+            obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
+            # 设置此签名URL在60秒内有效。
+            url = bucket.sign_url('PUT', obj, 7200)
+            res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
+            return JsonResponse(status=200, data=res_data)
+        elif is_st == '3':
+            # 人形检测带动图
+            # Endpoint以杭州为例,其它Region请按实际情况填写。
+            img_url_list = []
+            for i in range(int(is_st)):
+                obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
+                    format(uid=uid, channel=channel, filename=n_time, st=i)
+                # 设置此签名URL在60秒内有效。
+                url = bucket.sign_url('PUT', obj, 7200)
+                img_url_list.append(url)
+            res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
+            return JsonResponse(status=200, data=res_data)
 
 
     def get_msg_title(self, appBundleId, nickname):