#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved. @AUTHOR: ASJRD018 @NAME: AnsjerFormal @software: PyCharm @DATE: 2019/1/14 15:57 @Version: python3.6 @MODIFY DECORD:ansjer dev @file: DetectController.py @Contact: chanjunkai@163.com """ import time import os import apns2 import jpush as jpush import oss2 from django.http import JsonResponse from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from django.views.generic.base import View from pyfcm import FCMNotification from Ansjer.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 Device_Info, VodHlsModel, Equipment_Info, UidSetModel, UidPushModel from Object.RedisObject import RedisObject from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from Object.UidTokenObject import UidTokenObject from Service.CommonService import CommonService # http://192.168.136.40:8077/detect/changeStatus?uid=JW3684H8BSHG9TTM111A&token_val=18071adc03536302f34&appBundleId=com.ansjer.zccloud_ab&push_type=2&token=local&status=1&app_type=1&m_code=12 class DetectControllerView(View): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(DetectControllerView, self).dispatch(*args, **kwargs) def __init__(self): self.ip = '' def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') self.ip = CommonService.get_ip_address(request) return self.validation(request.GET, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') self.ip = CommonService.get_ip_address(request) return self.validation(request.POST, operation) def validation(self, request_dict, operation): response = ResponseObject() if operation is None: return response.json(444, 'error path') token = request_dict.get('token', None) tko = TokenObject(token) if tko.code == 0: userID = tko.userID # 修改推送设置 if operation == 'changeStatus': return self.do_change_status(userID, request_dict, response) # 查询推送信息 elif operation == 'queryInfo': return self.do_query(request_dict, response, userID) # 更新推送延迟 elif operation == 'updateInterval': return self.do_update_interval(userID, request_dict, response) else: return response.json(414) else: return response.json(tko.code) def do_query(self, request_dict, response, userID): page = int(request_dict.get('page', None)) line = int(request_dict.get('line', None)) nowTime = int(time.time()) if not page or not line: return response.json(444, 'page,line') qs = Equipment_Info.objects.filter(userID_id=userID, addTime__gte=nowTime - 3600 * 24 * 27) uid = request_dict.get('uid', None) if uid: qs = qs.filter(devUid=uid) dvqs = Device_Info.objects.filter(UID=uid).values('Type', 'NickName') uid_type_dict = {uid: {'type': dvqs[0]['Type'], 'NickName': dvqs[0]['NickName']}} else: dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName') uid_type_dict = {} for dv in dvqs: uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']} print(uid_type_dict) if not qs.exists(): return response.json(0, {'datas': [], 'count': 0}) qs = qs.values('id', 'devUid', 'devNickName', 'Channel', 'eventType', 'status', 'alarm', 'eventTime', 'receiveTime', 'is_st') count = qs.count() qr = qs[(page - 1) * line:page * line] res = [] auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET) img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg') # vod_time_list = [] for p in qr: devUid = p['devUid'] eventTime = p['eventTime'] channel = p['Channel'] if p['is_st'] == 1: p['img'] = img_bucket.sign_url('GET', '{uid}/{channel}/{time}.jpeg'. format(uid=devUid, channel=p['Channel'], time=eventTime), 300) p['img_list'] = [img_bucket.sign_url('GET', '{uid}/{channel}/{time}.jpeg'. format(uid=devUid, channel=channel, time=eventTime), 300)] elif p['is_st'] == 2: # 列表装载回放时间戳标记 vodqs = VodHlsModel.objects.filter(uid=devUid, channel=channel, time=int(eventTime)) \ .values("bucket__bucket", "bucket__endpoint") print(vodqs) if vodqs.exists(): bucket_name = vodqs[0]['bucket__bucket'] endpoint = vodqs[0]['bucket__endpoint'] bucket = oss2.Bucket(auth, endpoint, bucket_name) ts = '{uid}/vod{channel}/{etime}/ts0.ts'.format(uid=devUid, channel=p['Channel'], etime=eventTime) thumb0 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_0000,w_700'}) thumb1 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_1000,w_700'}) thumb2 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_2000,w_700'}) # thumb3 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_3000,w_700'}) p['img_list'] = [thumb0, thumb1, thumb2] if devUid in uid_type_dict.keys(): p['uid_type'] = uid_type_dict[devUid]['type'] p['devNickName'] = uid_type_dict[devUid]['NickName'] else: p['uid_type'] = '' res.append(p) return response.json(0, {'datas': res, 'count': count}) def do_change_status(self, userID, request_dict, response): uid = request_dict.get('uid', None) token_val = request_dict.get('token_val', None) appBundleId = request_dict.get('appBundleId', None) app_type = request_dict.get('app_type', None) push_type = request_dict.get('push_type', None) status = request_dict.get('status', None) m_code = request_dict.get('m_code', None) # 设备语言 lang = request_dict.get('lang', 'en') tz = request_dict.get('tz', '0') # interval = request_dict.get('interval', None) if not status: return response.json(444, 'status') # 关闭推送 if not all([appBundleId, app_type, token_val, uid, m_code]): return response.json(444, 'appBundleId,app_type,token_val,uid,m_code') # 判断推送类型对应key是否存在 if push_type == '0': if appBundleId not in APNS_CONFIG.keys(): return response.json(904) elif push_type == '1': if appBundleId not in FCM_CONFIG.keys(): return response.json(904) elif push_type == '2': if appBundleId not in JPUSH_CONFIG.keys(): return response.json(904) else: return response.json(173) dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid) status = int(status) if dvqs.exists(): # 获取用户区域 # ip = self.ip # ipInfo = CommonService.getIpIpInfo(ip=ip, lang='EN') # area = ipInfo['country_name'] # if area == 'China': # DETECT_PUSH_DOMAIN = 'cn.push.dvema.com' # else: # DETECT_PUSH_DOMAIN = 'en.push.dvema.com' nowTime = int(time.time()) uid_set_qs = UidSetModel.objects. \ filter(uid=uid, uidpushmodel__userID_id=userID, uidpushmodel__m_code=m_code) # 判断是否有曾经开启过 if uid_set_qs.exists(): uid_push_update_dict = { 'appBundleId': appBundleId, 'app_type': app_type, 'push_type': push_type, 'token_val': token_val, 'updTime': nowTime, 'lang': lang, 'tz': tz } uid_set_qs.update(detect_status=status, updTime=nowTime) UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid). \ update(**uid_push_update_dict) if status == 0: # 关闭成功 return response.json(0) utko = UidTokenObject() # right utko.generate(data={'uid': uid}) detectUrl = "{DETECT_PUSH_DOMAIN}notify/push?uidToken={uidToken}". \ format(uidToken=utko.token, DETECT_PUSH_DOMAIN=DETECT_PUSH_DOMAIN) return response.json(0, {'detectUrl': detectUrl}) else: uid_set_qs = UidSetModel.objects.filter(uid=uid) # 判断uid push是否绑定 if not uid_set_qs.exists(): uid_set_create_dict = { 'uid': uid, 'addTime': nowTime, 'updTime': nowTime, 'detect_status': status, } # 添加设备配置 uid_set_qs = UidSetModel.objects.create(**uid_set_create_dict) uid_set_id = uid_set_qs.id else: update_dict = { 'updTime': nowTime, 'detect_status': status, } uid_set_qs.update(**update_dict) uid_set_id = uid_set_qs[0].id uid_push_create_dict = { 'uid_set_id': uid_set_id, 'userID_id': userID, 'appBundleId': appBundleId, 'app_type': app_type, 'push_type': push_type, 'token_val': token_val, 'm_code': m_code, 'addTime': nowTime, 'updTime': nowTime, 'lang': lang, 'tz': tz } # 绑定设备推送 UidPushModel.objects.create(**uid_push_create_dict) if status == 0: return response.json(0) utko = UidTokenObject() utko.generate(data={'uid': uid}) detectUrl = "{DETECT_PUSH_DOMAIN}notify/push?uidToken={uidToken}". \ format(uidToken=utko.token, DETECT_PUSH_DOMAIN=DETECT_PUSH_DOMAIN) return response.json(0, {'detectUrl': detectUrl}) else: return response.json(14) def do_update_interval(self, userID, request_dict, response): uid = request_dict.get('uid', None) interval = request_dict.get('interval', None) dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid) if dvqs.exists(): uid_set_qs = UidSetModel.objects. \ filter(uid=uid, uidpushmodel__userID_id=userID) if uid_set_qs.exists(): uid_set_qs.update(interval=int(interval)) else: return response.json(173) else: return response.json(0) # http://192.168.136.40:8077/notify/push?uidToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJUTjdNUEUzMjExVUU3NkFQMTExQSJ9.k501567VdnhFpn_ygzGRDat3Kqlz5CsEA9jAC2dDk_g&obj=12341234&n_time=1234561234 # http://test.dvema.com/notify/push?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0 # 移动侦测接口 class NotificationView(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' # operation = kwargs.get('operation') return self.validation(request.GET) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' # operation = kwargs.get('operation') return self.validation(request.POST) def validation(self, request_dict): response = ResponseObject() uidToken = request_dict.get('uidToken', 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([uidToken, channel, n_time]): return JsonResponse(status=200, data={ 'code': 444, 'msg': 'param is wrong'}) # return response.json(444) utko = UidTokenObject(uidToken) uid = utko.UID uid_set_qs = UidSetModel.objects.filter(uid=uid, detect_status=1) if uid_set_qs.exists(): uid_set_id = uid_set_qs[0].id nickname = uid_set_qs[0].nickname if not nickname: nickname = uid uid_push_qs = UidPushModel.objects.filter(uid_set__id=uid_set_id). \ values('token_val', 'app_type', 'appBundleId', 'push_type', 'userID_id', 'userID__NickName', 'lang', 'tz') if uid_set_qs.exists(): redisObj = RedisObject(db=6) pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel) if redisObj.get_data(key=pkey): res_data = {'code': 0, 'msg': 'success,!'} return JsonResponse(status=200, data=res_data) else: detect_interval = uid_set_qs[0].detect_interval if detect_interval: redisObj.set_data(key=pkey, val=1, expire=detect_interval) auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET) bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg') for up in uid_push_qs: push_type = up['push_type'] # ios apns print(push_type) if push_type == 0: self.do_apns(request_dict, up, response, uid, channel, nickname) # android gcm elif push_type == 1: self.do_fcm(request_dict, up, response, uid, channel, nickname) # self.do_gmc(request_dict, up, response, uid, channel,nickname) # android jpush elif push_type == 2: self.do_jpush(request_dict, up, response, uid, channel, nickname) # self.do_save_equipment_info(ua, n_time, channel, event_type, is_st) # 需求不一样,所以这么做的 self.do_bulk_create_info(uid_push_qs, n_time, channel, event_type, is_st, uid) if is_st == '0' or is_st == '2': return JsonResponse(status=200, data={'code': 0, 'msg': 'success'}) else: # 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) else: return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'}) else: return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'}) def do_jpush(self, request_dict, uaql, response, uid, channel, nickname): event_type = request_dict.get('event_type', None) n_time = request_dict.get('n_time', None) appBundleId = uaql['appBundleId'] token_val = uaql['token_val'] lang = uaql['lang'] tz = uaql['tz'] response = ResponseObject() app_key = JPUSH_CONFIG[appBundleId]['Key'] master_secret = JPUSH_CONFIG[appBundleId]['Secret'] # 此处换成各自的app_key和master_secre _jpush = jpush.JPush(app_key, master_secret) push = _jpush.create_push() # if you set the logging level to "DEBUG",it will show the debug logging. _jpush.set_logging("DEBUG") # push.audience = jpush.all_ 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} message_title = self.get_message_title(appBundleId=appBundleId, nickname=nickname) send_text = self.get_send_text(channel=channel, n_time=n_time, lang=lang, tz=tz) android = jpush.android(alert=send_text, priority=1, style=1, alert_type=7, big_text=send_text, title=message_title, extras=push_data) push.notification = jpush.notification(android=android) push.platform = jpush.all_ try: res = push.send() print(res) except Exception as e: print("Exception") print(repr(e)) return response.json(10, repr(e)) else: return response.json(0) def get_message_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 get_send_text(self, channel, n_time, lang, tz): n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz) send_text = 'channel:{channel} date:{date}'.format(channel=channel, date=n_date) if lang == 'cn': send_text = '通道:{channel} 日期:{date}'.format(channel=channel, date=n_date) return send_text def do_fcm(self, request_dict, uaql, response, uid, channel, nickname): n_time = request_dict.get('n_time') appBundleId = uaql['appBundleId'] token_val = uaql['token_val'] lang = uaql['lang'] tz = uaql['tz'] try: serverKey = FCM_CONFIG[appBundleId] except Exception as e: return response.json(404) event_type = request_dict.get('event_type', None) push_service = FCMNotification(api_key=serverKey) registration_id = token_val message_title = self.get_message_title(appBundleId=appBundleId, nickname=nickname) send_text = self.get_send_text(channel=channel, n_time=n_time, lang=lang, tz=tz) 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=registration_id, message_title=message_title, message_body=send_text, data_message=data, extra_kwargs={ 'default_vibrate_timings': True, 'default_sound': True, 'default_light_settings': True }) response = ResponseObject() return response.json(0, result) def do_apns(self, request_dict, uaql, response, uid, channel, nickname): event_type = request_dict.get('event_type', None) token_val = uaql['token_val'] lang = uaql['lang'] n_time = request_dict.get('n_time') appBundleId = uaql['appBundleId'] tz = uaql['tz'] message_title = self.get_message_title(appBundleId=appBundleId, nickname=nickname) send_text = self.get_send_text(channel=channel, n_time=n_time, lang=lang, tz=tz) try: print('---') cli = apns2.APNSClient(mode=APNS_MODE, client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path'])) # password=APNS_CONFIG[appBundleId]['password']) 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} # body = json.dumps(push_data) alert = apns2.PayloadAlert(body=send_text, title=message_title) payload = apns2.Payload(alert=alert, custom=push_data) n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW) res = cli.push(n=n, device_token=token_val, topic=appBundleId) # assert res.status_code == 200, res.reason # assert res.apns_id print('========') print(res.status_code) if res.status_code == 200: return response.json(0) else: return response.json(404, res.reason) except Exception as e: print(repr(e)) return response.json(10, repr(e)) def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid): # qs_list = [] nowTime = int(time.time()) # 设备昵称 userID_ids = [] for dv in uaqs: userID_id = dv["userID_id"] if userID_id not in userID_ids: add_data = { 'userID_id': dv["userID_id"], 'eventTime': n_time, 'eventType': event_type, 'devUid': uid, 'devNickName': uid, 'Channel': channel, 'alarm': 'Motion \tChannel:{channel}'.format(channel=channel), 'is_st': int(is_st), 'receiveTime': n_time, 'addTime': nowTime } qs_list.append(Equipment_Info(**add_data)) userID_ids.append(userID_id) if qs_list: print(1) Equipment_Info.objects.bulk_create(qs_list) return True else: return False # http://192.168.136.40:8077/notify/push?uidToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJUTjdNUEUzMjExVUU3NkFQMTExQSJ9.k501567VdnhFpn_ygzGRDat3Kqlz5CsEA9jAC2dDk_g&obj=12341234&n_time=1234561234 # http://test.dvema.com/notify/push?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0 # 移动侦测接口 class NotificationViewXX(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' # operation = kwargs.get('operation') return self.validation(request.GET) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' # operation = kwargs.get('operation') return self.validation(request.POST) def validation(self, request_dict): response = ResponseObject() uidToken = request_dict.get('uidToken', None) channel = request_dict.get('channel', '1') n_time = request_dict.get('n_time', None) return