|
@@ -21,8 +21,7 @@ from django.http import JsonResponse
|
|
from django.views.generic.base import View
|
|
from django.views.generic.base import View
|
|
from pyfcm import FCMNotification
|
|
from pyfcm import FCMNotification
|
|
|
|
|
|
-from AnsjerPush.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, DETECT_PUSH_DOMAIN, JPUSH_CONFIG, \
|
|
|
|
- FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE
|
|
|
|
|
|
+from AnsjerPush.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, DETECT_PUSH_DOMAIN, JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE
|
|
from Model.models import Equipment_Info, UidPushModel, SysMsgModel
|
|
from Model.models import Equipment_Info, UidPushModel, SysMsgModel
|
|
from Object.ETkObject import ETkObject
|
|
from Object.ETkObject import ETkObject
|
|
from Object.RedisObject import RedisObject
|
|
from Object.RedisObject import RedisObject
|
|
@@ -53,10 +52,8 @@ class NotificationView(View):
|
|
n_time = request_dict.get('n_time', None)
|
|
n_time = request_dict.get('n_time', None)
|
|
event_type = request_dict.get('event_type', None)
|
|
event_type = request_dict.get('event_type', None)
|
|
is_st = request_dict.get('is_st', None)
|
|
is_st = request_dict.get('is_st', None)
|
|
- #--------------test-------------
|
|
|
|
- print("aaaaa")
|
|
|
|
- return JsonResponse(status=200, data={'code': 404, 'msg': 'testing'})
|
|
|
|
- #--------------/test------------
|
|
|
|
|
|
+ # print("aaa")
|
|
|
|
+ # return JsonResponse(0,safe=False)
|
|
if not all([channel, n_time]):
|
|
if not all([channel, n_time]):
|
|
return JsonResponse(status=200, data={
|
|
return JsonResponse(status=200, data={
|
|
'code': 444,
|
|
'code': 444,
|
|
@@ -69,128 +66,163 @@ class NotificationView(View):
|
|
else:
|
|
else:
|
|
utko = UidTokenObject(uidToken)
|
|
utko = UidTokenObject(uidToken)
|
|
uid = utko.UID
|
|
uid = utko.UID
|
|
- redisObj = RedisObject(db=6)
|
|
|
|
- pkey = '{uid}_ptl'.format(uid=uid)
|
|
|
|
|
|
+ pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
|
|
|
|
+ # ykey = 'MUJ887NLR8K8GBM9111A_redis_qs'.format(uid=uid)
|
|
ykey = '{uid}_redis_qs'.format(uid=uid)
|
|
ykey = '{uid}_redis_qs'.format(uid=uid)
|
|
- if redisObj.get_data(key=pkey):
|
|
|
|
|
|
+ dkey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=event_type, channel=channel)
|
|
|
|
+ # 判断redisObj.get_data(key=pkey):不为空
|
|
|
|
+ redisObj = RedisObject(db=6)
|
|
|
|
+ have_ykey = redisObj.get_data(key=ykey) # uid_set 数据库缓存
|
|
|
|
+ have_pkey = redisObj.get_data(key=pkey) # 一分钟限制key
|
|
|
|
+ have_dkey = redisObj.get_data(key=dkey) # 推送类型限制
|
|
|
|
+ # 一分钟外,推送开启状态
|
|
|
|
+ detect_med_type = 0 # 0推送旧机制 1存库不推送,2推送存库
|
|
|
|
+ if have_pkey:
|
|
res_data = {'code': 0, 'msg': 'success!'}
|
|
res_data = {'code': 0, 'msg': 'success!'}
|
|
return JsonResponse(status=200, data=res_data)
|
|
return JsonResponse(status=200, data=res_data)
|
|
|
|
+ # 数据库读取数据
|
|
|
|
+ if have_ykey:
|
|
|
|
+ redis_list = eval(redisObj.get_data(key=ykey))
|
|
else:
|
|
else:
|
|
- redis_data = redisObj.get_data(key=ykey)
|
|
|
|
- if redis_data:
|
|
|
|
- print("从缓存查到数据")
|
|
|
|
- redis_list = eval(redis_data)
|
|
|
|
|
|
+ # 从数据库查询出来
|
|
|
|
+ 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', 'userID__NickName',
|
|
|
|
+ 'lang', '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分钟
|
|
|
|
+ redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
|
|
|
|
+ if not redis_list:
|
|
|
|
+ res_data = {'code': 404, 'msg': 'error !'}
|
|
|
|
+ return JsonResponse(status=200, data=res_data)
|
|
|
|
+
|
|
|
|
+ if not redis_list:
|
|
|
|
+ print("没有redi_list")
|
|
|
|
+ res_data = {'code': 0, 'msg': 'success!'}
|
|
|
|
+ return JsonResponse(status=200, data=res_data)
|
|
|
|
+ is_sys_msg = self.is_sys_msg(int(event_type))
|
|
|
|
+ nickname = redis_list[0]['uid_set__nickname']
|
|
|
|
+ detect_interval = redis_list[0]['uid_set__detect_interval']
|
|
|
|
+ detect_group = redis_list[0]['uid_set__detect_group']
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ if not nickname:
|
|
|
|
+ nickname = uid
|
|
|
|
+ if detect_group:
|
|
|
|
+ if have_dkey:
|
|
|
|
+ detect_med_type = 1
|
|
else:
|
|
else:
|
|
- # 设置推送时间为60秒一次
|
|
|
|
|
|
+ detect_med_type = 2
|
|
|
|
+ # detect_group=0允许全部推送的时候
|
|
|
|
+ if detect_group == '0':
|
|
|
|
+ redisObj.set_data(key=dkey, val=1, expire=detect_interval)
|
|
|
|
+ else:
|
|
|
|
+ detect_group_list = detect_group.split(',')
|
|
|
|
+ if event_type in detect_group_list:
|
|
|
|
+ if detect_interval < 60:
|
|
|
|
+ detect_interval = 60
|
|
|
|
+ redisObj.set_data(key=dkey, val=1, expire=detect_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')
|
|
|
|
- # 新建一个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']
|
|
|
|
- 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
|
|
|
|
|
|
+
|
|
|
|
+ # 旧模式并且没有pkey,重新创建一个
|
|
|
|
+ if not detect_group and not have_pkey:
|
|
|
|
+ # 设置推送时间为60秒一次
|
|
|
|
+ redisObj.set_data(key=pkey, val=1, expire=60)
|
|
|
|
+ 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 detect_med_type == 2 or detect_med_type == 0:
|
|
if push_type == 0: # ios apns
|
|
if push_type == 0: # ios apns
|
|
self.do_apns(**kwag_args)
|
|
self.do_apns(**kwag_args)
|
|
elif push_type == 1: # android gcm
|
|
elif push_type == 1: # android gcm
|
|
self.do_fcm(**kwag_args)
|
|
self.do_fcm(**kwag_args)
|
|
elif push_type == 2: # android jpush
|
|
elif push_type == 2: # android jpush
|
|
self.do_jpush(**kwag_args)
|
|
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 = 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,
|
|
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)
|
|
|
|
|
|
+ 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':
|
|
|
|
+ print("is_st=0or2")
|
|
|
|
+ return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
|
|
|
|
+ elif is_st == '1':
|
|
|
|
+ print("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':
|
|
|
|
+ print("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在60秒内有效。
|
|
url = bucket.sign_url('PUT', obj, 7200)
|
|
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)
|
|
|
|
- else:
|
|
|
|
- return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
|
|
|
|
|
|
+ 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):
|
|
def get_msg_title(self, appBundleId, nickname):
|
|
package_title_config = {
|
|
package_title_config = {
|
|
@@ -218,7 +250,7 @@ class NotificationView(View):
|
|
return False
|
|
return False
|
|
|
|
|
|
def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
|
|
def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
|
|
- n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
|
|
|
|
|
|
+ n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz,lang=lang)
|
|
etype = int(event_type)
|
|
etype = int(event_type)
|
|
if lang == 'cn':
|
|
if lang == 'cn':
|
|
if etype == 704:
|
|
if etype == 704:
|