|
@@ -11,25 +11,30 @@
|
|
|
@file: DetectController.py
|
|
|
@Contact: chanjunkai@163.com
|
|
|
"""
|
|
|
+import json
|
|
|
+import os
|
|
|
+import time
|
|
|
+
|
|
|
+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
|
|
|
-import time
|
|
|
-import apns2
|
|
|
-from Object.ResponseObject import ResponseObject
|
|
|
-import os
|
|
|
+from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
|
|
|
from Ansjer.config import BASE_DIR
|
|
|
-from Object.TokenObject import TokenObject
|
|
|
-import jpush as jpush
|
|
|
-from Model.models import Device_User, Device_Info, Equipment_Info, App_Info, UID_App
|
|
|
-from Object.UidTokenObject import UidTokenObject
|
|
|
from Ansjer.config import SERVER_DOMAIN
|
|
|
-import json
|
|
|
-import requests
|
|
|
+from Model.models import Device_Info, App_Info, UID_App
|
|
|
from Model.models import Equipment_Info
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
+from Object.TokenObject import TokenObject
|
|
|
+from Object.UidTokenObject import UidTokenObject
|
|
|
+from django.http import JsonResponse
|
|
|
+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
|
|
|
+# 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):
|
|
@@ -53,46 +58,92 @@ class DetectControllerView(View):
|
|
|
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)
|
|
|
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))
|
|
|
+ if not page or not line:
|
|
|
+ return response.json(444, 'page,line')
|
|
|
+ qs = Equipment_Info.objects.filter(userID_id=userID)
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ if uid:
|
|
|
+ qs = qs.filter(devUid=uid)
|
|
|
+ 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]
|
|
|
+ # send_json = CommonService.qs_to_dict(res)
|
|
|
+ res = []
|
|
|
+ import oss2
|
|
|
+ auth = oss2.Auth('LTAIyMkGfEdogyL9', '71uIjpsqVOmF7DAITRyRuc259jHOjO')
|
|
|
+ bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
|
|
|
+ for p in qr:
|
|
|
+ if p['is_st'] == '1':
|
|
|
+ p['img'] = bucket.sign_url('GET',
|
|
|
+ '{uid}/{channel}/{time}.jpeg'.format(uid=p['devUid'], channel=p['Channel'],
|
|
|
+ time=p['eventTime']), 300)
|
|
|
+ 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)
|
|
|
print('status')
|
|
|
print(status)
|
|
|
if status == '0':
|
|
|
- UID_App.objects.filter(appBundleId=appBundleId, userID_id=userID, token_val=token_val, uid=uid).delete()
|
|
|
- return response.json(0)
|
|
|
+ uid_app_qs = UID_App.objects.filter(appBundleId=appBundleId, userID_id=userID, token_val=token_val,
|
|
|
+ uid=uid,app_type=app_type)
|
|
|
+ if uid_app_qs.exists():
|
|
|
+ uid_app_qs.update(status=0)
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(173)
|
|
|
elif status == '1':
|
|
|
# return response.json(10)
|
|
|
|
|
|
dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
|
|
|
- aiqs = App_Info.objects.filter(appBundleId=appBundleId).values('app_type')
|
|
|
- print(dvqs)
|
|
|
- print(aiqs)
|
|
|
- if dvqs.exists() and aiqs.exists():
|
|
|
+ # aiqs = App_Info.objects.filter(appBundleId=appBundleId).values('app_type')
|
|
|
+ # print(dvqs)
|
|
|
+ # print(aiqs)
|
|
|
+ # if dvqs.exists() and aiqs.exists():
|
|
|
+ if dvqs.exists():
|
|
|
now_time = int(time.time())
|
|
|
try:
|
|
|
- UID_App.objects.create(
|
|
|
- uid=uid,
|
|
|
- userID_id=userID,
|
|
|
- appBundleId=appBundleId,
|
|
|
- app_type=aiqs[0]['app_type'],
|
|
|
- push_type=push_type,
|
|
|
- token_val=token_val,
|
|
|
- addTime=now_time,
|
|
|
- updTime=now_time)
|
|
|
+ uid_app_qs = UID_App.objects.filter(appBundleId=appBundleId, userID_id=userID, token_val=token_val,
|
|
|
+ uid=uid)
|
|
|
+ print(uid_app_qs)
|
|
|
+ if uid_app_qs.exists():
|
|
|
+ uid_app_qs.update(status=1)
|
|
|
+ else:
|
|
|
+ UID_App.objects.create(
|
|
|
+ uid=uid,
|
|
|
+ userID_id=userID,
|
|
|
+ appBundleId=appBundleId,
|
|
|
+ app_type=app_type,
|
|
|
+ push_type=push_type,
|
|
|
+ token_val=token_val,
|
|
|
+ status=1,
|
|
|
+ addTime=now_time,
|
|
|
+ updTime=now_time)
|
|
|
except Exception as e:
|
|
|
print(repr(e))
|
|
|
- return response.json(10,repr(e))
|
|
|
+ return response.json(10, repr(e))
|
|
|
else:
|
|
|
utko = UidTokenObject()
|
|
|
utko.generate(data={'uid': uid})
|
|
@@ -102,6 +153,9 @@ class DetectControllerView(View):
|
|
|
else:
|
|
|
return response.json(173)
|
|
|
|
|
|
+
|
|
|
+# 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):
|
|
|
|
|
@@ -118,6 +172,13 @@ class NotificationView(View):
|
|
|
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=404,data={})
|
|
|
+ # return response.json(444)
|
|
|
utko = UidTokenObject(uidToken)
|
|
|
uid = utko.UID
|
|
|
uaqs = UID_App.objects.filter(uid=uid). \
|
|
@@ -134,13 +195,29 @@ class NotificationView(View):
|
|
|
# android jpush
|
|
|
elif push_type == 2:
|
|
|
self.do_jpush(request_dict, ua, response)
|
|
|
- n_time = request_dict.get('n_time')
|
|
|
- self.do_save_equipment_info(ua, n_time)
|
|
|
- return response.json(0)
|
|
|
+ self.do_save_equipment_info(ua, n_time, channel, event_type,is_st)
|
|
|
+ #############
|
|
|
+ # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
|
|
|
+
|
|
|
+ if is_st == '0':
|
|
|
+ return JsonResponse(status=200, data={'code': 0, 'msg': 'success'})
|
|
|
+ else:
|
|
|
+ auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
|
|
|
+ # Endpoint以杭州为例,其它Region请按实际情况填写。
|
|
|
+ bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
|
|
|
+ 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 response.json(173)
|
|
|
+ return JsonResponse(status=200, data={
|
|
|
+ 'code': 173,
|
|
|
+ 'msg': 'data is not exist'})
|
|
|
|
|
|
def do_jpush(self, request_dict, uaql, response):
|
|
|
+ event_type = request_dict.get('event_type', None)
|
|
|
jpush_config = {
|
|
|
'com.ansjer.accloud_ab': {
|
|
|
'Key': 'f0dc047e5e53fd14199de5b0',
|
|
@@ -191,7 +268,7 @@ class NotificationView(View):
|
|
|
_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": "51", "msg": "",
|
|
|
+ push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
|
|
|
"received_at": n_time, "sound": "sound.aif", "uid": uid}
|
|
|
push_msg = json.dumps(push_data)
|
|
|
# push.notification = jpush.notification(alert="hello jpush api")
|
|
@@ -226,7 +303,8 @@ class NotificationView(View):
|
|
|
'com.ansjer.customizeda_a': 'AAAAb9YP3rk:APA91bF0HzizVWDc6dKzobY9fsaKDK4veqkOZehDXshVXs8pEEvNWjR_YWbhP60wsRYCHCal8fWN5cECVOWNMMzDsfU88Ty2AUl8S5FtZsmeDTkoGntQOswBr8Ln7Fm_LAp1VqTf9CpM',
|
|
|
}
|
|
|
serverKey = gcm_config[appBundleId]
|
|
|
- push_data = {"alert": "Motion ", "event_time": n_time, "event_type": "51", "msg": "",
|
|
|
+ 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}
|
|
|
json_data = {
|
|
|
"collapse_key": "WhatYouWant",
|
|
@@ -266,20 +344,20 @@ class NotificationView(View):
|
|
|
# assert res.status_code == 200, res.reason
|
|
|
# assert res.apns_id
|
|
|
if res.status_code == 200:
|
|
|
- # self.do_save_equipment_info(uaql, n_time)
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
return response.json(404, res.reason)
|
|
|
except Exception as e:
|
|
|
return response.json(10, repr(e))
|
|
|
|
|
|
- def do_save_equipment_info(self, uaql, n_time):
|
|
|
+ def do_save_equipment_info(self, uaql, n_time, channel, event_type,is_st):
|
|
|
Equipment_Info.objects.create(
|
|
|
userID_id=uaql['userID_id'],
|
|
|
eventTime=n_time,
|
|
|
- eventType=1,
|
|
|
+ eventType=event_type,
|
|
|
devUid=uaql['uid'],
|
|
|
devNickName=uaql['userID__NickName'],
|
|
|
- Channel='0',
|
|
|
+ Channel=channel,
|
|
|
alarm='0',
|
|
|
+ is_st=int(is_st),
|
|
|
receiveTime=n_time)
|