|
@@ -53,6 +53,8 @@ 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)
|
|
|
+ # print("aaa")
|
|
|
+ # return JsonResponse(0,safe=False)
|
|
|
if not all([channel, n_time]):
|
|
|
return JsonResponse(status=200, data={
|
|
|
'code': 444,
|
|
@@ -70,34 +72,148 @@ class NotificationView(View):
|
|
|
redisObj = RedisObject(db=6)
|
|
|
pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
|
|
|
ykey = '{uid}_redis_qs'.format(uid=uid)
|
|
|
- # 判断redisObj.get_data(key=pkey):不为空
|
|
|
+ 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!'}
|
|
|
+ # 1分钟之内不再推送,不存数据库了
|
|
|
return JsonResponse(status=200, data=res_data)
|
|
|
- else:
|
|
|
+ # 第一次进来推送,存库
|
|
|
+ # 修改redis数据,并设置过期时间为10分钟
|
|
|
+ # 查数据库,存入redis得到redis_list
|
|
|
+ # 创建dkey,有效期为detect_interval
|
|
|
+ # 创建ykey600秒
|
|
|
+ # 推送,存数据库代码
|
|
|
+ if not redisObj.get_data(key=dkey):
|
|
|
redis_data = redisObj.get_data(key=ykey)
|
|
|
if redis_data:
|
|
|
print("从缓存查到数据")
|
|
|
redis_list = eval(redis_data)
|
|
|
else:
|
|
|
# 设置推送时间为60秒一次
|
|
|
- redisObj.set_data(key=pkey, val=1, expire=time_interval)
|
|
|
- # redisObj.set_data(key=pkey, val=1, expire=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')
|
|
|
+ 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group')
|
|
|
# 新建一个list接收数据
|
|
|
redis_list = []
|
|
|
# 把数据库数据追加进redis_list
|
|
|
for qs in uid_push_qs:
|
|
|
redis_list.append(qs)
|
|
|
- # 修改redis数据,并设置过期时间为10分钟
|
|
|
- if redis_list:
|
|
|
- redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
|
|
|
- nickname = redis_list[0]['uid_set__nickname']
|
|
|
+ if redis_list:
|
|
|
+ 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 redisObj.get_data(key=dkey):
|
|
|
+ # res_data = {'code': 0, 'msg': 'success!'}
|
|
|
+ # 把ykey的时间也设置成跟dkey一样
|
|
|
+ redisObj.set_data(key=ykey, val=str(redis_list), expire=detect_interval)
|
|
|
+ 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)
|
|
|
+
|
|
|
+ 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)
|
|
|
+ # 1分钟之外,dkey 分钟之内,存数据库,不推送
|
|
|
+ if not redisObj.get_data(key=pkey) and redisObj.get_data(key=dkey):
|
|
|
+ redis_data = redisObj.get_data(key=ykey)
|
|
|
+ if redis_data:
|
|
|
+ print("从缓存查到数据")
|
|
|
+ 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
|
|
@@ -132,12 +248,12 @@ class NotificationView(View):
|
|
|
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)
|
|
|
+ # 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:
|
|
@@ -189,8 +305,6 @@ class NotificationView(View):
|
|
|
img_url_list.append(url)
|
|
|
res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
|
|
|
return JsonResponse(status=200, data=res_data)
|
|
|
- else:
|
|
|
- return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
|
|
|
|
|
|
def get_msg_title(self, appBundleId, nickname):
|
|
|
package_title_config = {
|