#!/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 json import os import time import urllib import apns2 import jpush as jpush import oss2 import requests from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from django.views.generic.base import View from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET from Ansjer.config import BASE_DIR from Ansjer.config import SERVER_DOMAIN from Model.models import Device_Info, VodHlsModel, Equipment_Info, UidSetModel, UidPushModel from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from Object.UidTokenObject import UidTokenObject from django.http import JsonResponse from Object.RedisObject import RedisObject from django.db import transaction # 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 class DetectControllerView(View): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(DetectControllerView, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.GET, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') 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, eventTime__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) # interval = request_dict.get('interval', None) print('status:' + status) 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') dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid) status = int(status) if dvqs.exists(): 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 } 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) utko = UidTokenObject() # right utko.generate(data={'uid': uid}) detectUrl = "{SERVER_DOMAIN}notify/push?uidToken={uidToken}". \ format(uidToken=utko.token, SERVER_DOMAIN=SERVER_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: 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 } # 绑定设备推送 UidPushModel.objects.create(**uid_push_create_dict) utko = UidTokenObject() utko.generate(data={'uid': uid}) detectUrl = "{SERVER_DOMAIN}notify/push?uidToken={uidToken}". \ format(uidToken=utko.token, SERVER_DOMAIN=SERVER_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', None) 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 uid_push_qs = UidPushModel.objects.filter(uid_set__id=uid_set_id). \ values('token_val', 'app_type', 'appBundleId', 'push_type', 'userID_id', 'userID__NickName') 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 if push_type == 0: self.do_apns(request_dict, up, response, uid) # android gcm elif push_type == 1: self.do_gmc(request_dict, up, response, uid) # android jpush elif push_type == 2: self.do_jpush(request_dict, up, response, uid) # 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): event_type = request_dict.get('event_type', None) jpush_config = { 'com.ansjer.accloud_ab': { 'Key': 'f0dc047e5e53fd14199de5b0', 'Secret': 'aa7f7db33e9f0a7f3871aa1c'}, 'com.ansjer.adcloud_ab': { 'Key': '76d97b535185114985608234', 'Secret': 'c9a92b301043cc9c52778692'}, 'com.ansjer.zccloud_ab': { 'Key': 'd9924f56d3cc7c6017965130', 'Secret': '869d832d126a232f158b5987'}, 'com.ansjer.loocamccloud_ab': { 'Key': 'd1cc44797b4642b0e05304fe', 'Secret': 'c3e8b4ca8c576de61401e56a'}, 'com.ansjer.loocamdcloud_ab': { 'Key': '76d97b535185114985608234', 'Secret': 'c9a92b301043cc9c52778692'}, 'com.ansjer.zccloud_a': { 'Key': '57de2a80d68bf270fd6bdf5a', 'Secret': '3d354eb6a0b49c2610decf42'}, 'com.ansjer.accloud_a': { 'Key': 'ff95ee685f49c0dc4013347b', 'Secret': 'de2c20959f5516fdeeafe78e'}, 'com.ansjer.adcloud_a': { 'Key': '2e47eb1aee9b164460df3668', 'Secret': 'b9137d8d684bc248f1809b6d'}, 'com.ansjer.loocamccloud_a': { 'Key': '23c9213215c7ca0ec945629b', 'Secret': '81e4b1e859cc8387e2e6c431'}, 'com.ansjer.loocamdcloud_a': { 'Key': '1dbdd60a16e9892d6f68a073', 'Secret': '80a97690e7e043109059b403'}, 'com.ansjer.customizedb_a': { 'Key': '9d79630aa49adfa291fe2568', 'Secret': '4d8ff52f88136561875a0212'}, } n_time = request_dict.get('n_time', None) appBundleId = uaql['appBundleId'] token_val = uaql['token_val'] 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"} push.message = jpush.message('Motion', extras=push_data, title='KPNS', content_type='text') 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 do_gmc(self, request_dict, uaql, response, uid): n_time = request_dict.get('n_time') appBundleId = uaql['appBundleId'] token_val = uaql['token_val'] gcm_config = { 'com.ansjer.zccloud_a': 'AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I', 'com.ansjer.loocamccloud_a': 'AAAAb9YP3rk:APA91bFCgd-kbVmpK4EVpfdHH_PJZQCYTkOGnTZdIuBWEz2r7aMRsJYHOH3sB-rwcbaRWgnufTyjX9nGQxb6KxQbWVk4ah_H-M3IqGh6Mb60WQQAuR33V6g_Jes5pGL6ViuIxGHqVMaR', 'com.ansjer.loocamdcloud_a': 'AAAAb9YP3rk:APA91bGw2I2KMD4i-5T7nZO_wB8kuAOuqgyqe5rxmY-W5qkpYEx9IL2IfmC_qf6B_xOyjIDDSjckvMo-RauN__SEoxvAkis7042GRkoKpw7cjZ_H8lC-d50PC0GclPzccrOGFusyKbFY', 'com.ansjer.customizedb_a': 'AAAAb9YP3rk:APA91bE7kI4vcm-9h_CJNFlOZfc-xwP4Btn6AnjOrwoKV6fgYN7fdarkO76sYxVZiAbDnxsFfOJyP7vQfwyan6mdjuyD5iHdt_XgO22VqniC0vA1V4GJiCS8Tp7LxIX8JVKZl9I_Powt', 'com.ansjer.customizeda_a': 'AAAAb9YP3rk:APA91bF0HzizVWDc6dKzobY9fsaKDK4veqkOZehDXshVXs8pEEvNWjR_YWbhP60wsRYCHCal8fWN5cECVOWNMMzDsfU88Ty2AUl8S5FtZsmeDTkoGntQOswBr8Ln7Fm_LAp1VqTf9CpM', } serverKey = gcm_config[appBundleId] event_type = request_dict.get('event_type', None) push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "", "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1"} json_data = { "collapse_key": "WhatYouWant", "data": push_data, "delay_while_idle": False, "time_to_live": 3600, "registration_ids": [token_val] } url = 'https://android.googleapis.com/gcm/send' # serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I" data = json.dumps(json_data).encode('utf-8') headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey} req = requests.post(url, data, headers=headers) return response.json(0) def do_apns(self, request_dict, uaql, response, uid): token_val = uaql['token_val'] n_time = request_dict.get('n_time') appBundleId = uaql['appBundleId'] apns_config = { 'appbundleId': {'pem_path': 'xxxx', 'topic': 'topic', 'password': 'password'} } try: # daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103)) # print(daytime) pem_path = os.path.join(BASE_DIR, apns_config[appBundleId]['topic']) # pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem') cli = apns2.APNSClient(mode="dev", client_cert=pem_path, password='111111') body = json.dumps({'uid': uid, 'n_time': n_time}) alert = apns2.PayloadAlert(body="body!", title="title!") payload = apns2.Payload(alert=alert) n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW) res = cli.push(n=n, device_token=token_val, topic=apns_config[appBundleId]['pem_path']) # assert res.status_code == 200, res.reason # assert res.apns_id if res.status_code == 200: return response.json(0) else: return response.json(404, res.reason) except Exception as 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()) # 设备昵称 for dv in uaqs: 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)) if qs_list: print(1) Equipment_Info.objects.bulk_create(qs_list) return True else: return False