import logging import os import time import apns2 import jpush as jpush import oss2 from django.http import JsonResponse from django.views.generic.base import View from pyfcm import FCMNotification from AnsjerPush.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, JPUSH_CONFIG, FCM_CONFIG, \ APNS_CONFIG, BASE_DIR, APNS_MODE from AnsjerPush.config import SERVER_TYPE from Model.models import UidPushModel, SysMsgModel from Object.ETkObject import ETkObject from Object.RedisObject import RedisObject from Object.UidTokenObject import UidTokenObject from Object.utils import LocalDateTimeUtil from Service.CommonService import CommonService from Service.EquipmentInfoService import EquipmentInfoService # 旧移动侦测接口 class NotificationView(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' return self.validation(request.GET) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' return self.validation(request.POST) def validation(self, request_dict): logger = logging.getLogger('info') logger.info("旧移动侦测接口参数:{}".format(request_dict)) uidToken = request_dict.get('uidToken', None) etk = request_dict.get('etk', None) channel = request_dict.get('channel', '1') n_time = request_dict.get('n_time', None) event_type = request_dict.get('event_type', None) is_st = request_dict.get('is_st', None) if not all([channel, n_time]): return JsonResponse(status=200, data={'code': 444, 'msg': 'error channel or n_time'}) if etk: eto = ETkObject(etk) uid = eto.uid if len(uid) != 20: return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'}) else: utko = UidTokenObject(uidToken) uid = utko.UID logger.info("旧移动侦测接口的uid:{}".format(uid)) pkey = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, event_type=event_type, channel=channel) ykey = '{uid}_redis_qs'.format(uid=uid) is_sys_msg = self.is_sys_msg(int(event_type)) if is_sys_msg is True: dkey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=event_type, channel=channel) else: dkey = '{uid}_{channel}_flag'.format(uid=uid, channel=channel) 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': 'Push it once a minute'} return JsonResponse(status=200, data=res_data) # 数据库读取数据 if have_ykey: uid_push_list = eval(redisObj.get_data(key=ykey)) else: # 从数据库查询出来 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__new_detect_interval') uid_push_list = [] for qs in uid_push_qs: uid_push_list.append(qs) # 修改redis数据,并设置过期时间为10分钟 redisObj.set_data(key=ykey, val=str(uid_push_list), expire=600) if not uid_push_list: res_data = {'code': 404, 'msg': 'error !'} return JsonResponse(status=200, data=res_data) if not uid_push_list: res_data = {'code': 0, 'msg': 'uid_push_list not exist'} return JsonResponse(status=200, data=res_data) nickname = uid_push_list[0]['uid_set__nickname'] detect_interval = uid_push_list[0]['uid_set__detect_interval'] detect_group = uid_push_list[0]['uid_set__detect_group'] now_time = int(time.time()) if not nickname: nickname = uid if detect_group is not None: if have_dkey: detect_med_type = 1 # 1为存库不推送 else: detect_med_type = 2 # 为2的话,既推送,又存库 if SERVER_TYPE != 'Ansjer.cn_config.cn_formal_settings': new_detect_interval = uid_push_list[0]['uid_set__new_detect_interval'] detect_interval = new_detect_interval if new_detect_interval > 0 else detect_interval detect_interval = 60 if detect_interval < 60 else detect_interval redisObj.set_data(key=dkey, val=1, expire=detect_interval - 5) redisObj.set_data(key=pkey, val=1, expire=60) logger.info('APP消息推送V1接口,是否进行APP推送:{},1为不推送,间隔:{}'.format(detect_med_type, detect_interval)) # 旧模式并且没有pkey,重新创建一个 if not detect_group and not have_pkey: 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, } sys_msg_list = [] userID_ids = [] do_apns_code = '' do_fcm_code = '' do_jpush_code = '' new_device_info_list = [] local_date_time = '' for up in uid_push_list: push_type = up['push_type'] appBundleId = up['appBundleId'] token_val = up['token_val'] lang = up['lang'] tz = up['tz'] if tz is None or tz == '': tz = 0 # 发送标题 msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname) # 发送内容 msg_text = self.get_msg_text(channel=channel, n_time=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 logger.info('推送要的数据:') logger.info(kwag_args) logger.info(detect_med_type) local_date_time = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang='cn') logger.info('<<<<<旧的接口,根据时区计算后日期={},时区={}'.format(local_date_time, tz)) local_date_time = local_date_time[0:10] logger.info('<<<<<旧的接口,日期={}'.format(local_date_time)) # 以下是存库 userID_id = up["userID_id"] if userID_id not in userID_ids: 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)) else: logger.info('----《旧接口》start------') new_device_info_list.append(EquipmentInfoService.get_equipment_info_obj( local_date_time, device_user_id=userID_id, event_time=n_time, event_type=event_type, device_uid=uid, device_nick_name=nickname, channel=channel, alarm='Motion \tChannel:{channel}'.format(channel=channel), is_st=is_st, receive_time=n_time, add_time=now_time, storage_location=1, border_coords='', )) userID_ids.append(userID_id) # 推送 if detect_med_type == 2 or detect_med_type == 0: logger.info('准备推送{}'.format(detect_med_type)) try: if push_type == 0: # ios apns do_apns_code = self.do_apns(**kwag_args) elif push_type == 1: # android gcm print('do_fcm') do_fcm_code = self.do_fcm(**kwag_args) elif push_type == 2: # android jpush print('do_jpush') do_jpush_code = self.do_jpush(**kwag_args) except Exception as e: logger.info("errLine={errLine}, errMsg={errMsg}".format(errLine=e.__traceback__.tb_lineno, errMsg=repr(e))) continue if detect_med_type == 1: do_apns_code = '只存库不推送' do_fcm_code = '只存库不推送' do_jpush_code = '只存库不推送' if is_sys_msg: SysMsgModel.objects.bulk_create(sys_msg_list) else: if new_device_info_list and len(new_device_info_list) > 0: # 根据日期获得星期几 week = LocalDateTimeUtil.date_to_week(local_date_time) EquipmentInfoService.equipment_info_bulk_create(week, new_device_info_list) logger.info('----《旧接口》设备信息分表批量保存end') if is_st == '0' or is_st == '2': print("is_st=0or2") for up in uid_push_list: if up['push_type'] == 0: # ios apns up['do_apns_code'] = do_apns_code elif up['push_type'] == 1: # android gcm up['do_fcm_code'] = do_fcm_code elif up['push_type'] == 2: # android jpush up['do_jpush_code'] = do_jpush_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'] return JsonResponse(status=200, data={'code': 0, 'msg': 'success 0 or 2'}) 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) for up in uid_push_list: up['do_apns_code'] = do_apns_code up['do_fcm_code'] = do_fcm_code up['do_jpush_code'] = do_jpush_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'] 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 = bucket.sign_url('PUT', obj, 7200) img_url_list.append(url) for up in uid_push_list: up['do_apns_code'] = do_apns_code up['do_fcm_code'] = do_fcm_code up['do_jpush_code'] = do_jpush_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'] res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success 3'} return JsonResponse(status=200, data=res_data) def get_msg_title(self, appBundleId, nickname): package_title_config = { 'com.ansjer.customizedd_a': 'DVS', 'com.ansjer.zccloud_a': 'ZosiSmart', 'com.ansjer.zccloud_ab': '周视', 'com.ansjer.adcloud_a': 'ADCloud', 'com.ansjer.adcloud_ab': 'ADCloud', 'com.ansjer.accloud_a': 'ACCloud', 'com.ansjer.loocamccloud_a': 'Loocam', 'com.ansjer.loocamdcloud_a': 'Anlapus', 'com.ansjer.customizedb_a': 'COCOONHD', 'com.ansjer.customizeda_a': 'Guardian365', 'com.ansjer.customizedc_a': 'PatrolSecure', } if appBundleId in package_title_config.keys(): return package_title_config[appBundleId] + '(' + nickname + ')' else: return nickname def is_sys_msg(self, event_type): 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, 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 = '电量过低' 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 = 'Low battery' 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_ try: res = push.send() print(res) status_code = res.status_code except Exception as e: logger = logging.getLogger('info') logger.info(e) status_code = 100 return status_code def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text): try: serverKey = FCM_CONFIG[appBundleId] except Exception as 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): 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") n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW) res = cli.push(n=n, device_token=token_val, topic=appBundleId) if res.status_code == 200: return res.status_code else: return res.status_code except (ValueError, ArithmeticError): return 'The program has a numeric format exception, one of the arithmetic exceptions' except Exception as e: return repr(e)