|
@@ -26,6 +26,7 @@ from Object.UidTokenObject import UidTokenObject
|
|
|
from Ansjer.config import SERVER_DOMAIN
|
|
|
import json
|
|
|
import requests
|
|
|
+from Model.models import Equipment_Info
|
|
|
|
|
|
|
|
|
class DetectControllerView(View):
|
|
@@ -108,33 +109,24 @@ class NotificationView(View):
|
|
|
uidToken = request_dict.get('uidToken', None)
|
|
|
utko = UidTokenObject(uidToken)
|
|
|
uid = utko.UID
|
|
|
- uaqs = UID_App.objects.filter(uid=uid).values('token_val', 'app_type', 'appBundleId', 'push_type','uid')
|
|
|
+ uaqs = UID_App.objects.filter(uid=uid). \
|
|
|
+ values('token_val', 'app_type', 'appBundleId', 'push_type', 'uid')
|
|
|
if uaqs.exists():
|
|
|
- push_type = uaqs[0]['push_type']
|
|
|
- # token_val = uaqs[0]['token_val']
|
|
|
- # appBundleId = uaqs[0]['appBundleId']
|
|
|
- # app_type = uaqs[0]['app_type']
|
|
|
-
|
|
|
- # ios apns
|
|
|
- if push_type == 0:
|
|
|
- return self.do_apns(request_dict, uaqs[0], response)
|
|
|
- # android gcm
|
|
|
- elif push_type == 1:
|
|
|
- return self.do_gmc(request_dict, uaqs[0], response)
|
|
|
- # android jpush
|
|
|
- elif push_type == 2:
|
|
|
- return self.do_apns(request_dict, uaqs[0], response)
|
|
|
+ for ua in uaqs:
|
|
|
+ push_type = ua['push_type']
|
|
|
+ # ios apns
|
|
|
+ if push_type == 0:
|
|
|
+ return self.do_apns(request_dict, ua, response)
|
|
|
+ # android gcm
|
|
|
+ elif push_type == 1:
|
|
|
+ return self.do_gmc(request_dict, ua, response)
|
|
|
+ # android jpush
|
|
|
+ elif push_type == 2:
|
|
|
+ return self.do_jpush(request_dict, ua, response)
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
else:
|
|
|
return response.json(173)
|
|
|
- # # 设备主键uid
|
|
|
- # if pushType == 'jpush':
|
|
|
- # return self.do_jpush(request_dict)
|
|
|
- # elif pushType == 'gcm':
|
|
|
- # return
|
|
|
- # elif pushType == 'apns':
|
|
|
- # return
|
|
|
- # else:
|
|
|
- # return response.json(414)
|
|
|
|
|
|
def do_jpush(self, request_dict, uaql, response):
|
|
|
jpush_config = {
|
|
@@ -199,40 +191,49 @@ class NotificationView(View):
|
|
|
else:
|
|
|
return response.json(0)
|
|
|
|
|
|
- def do_gmc(self, request_dict, response):
|
|
|
+ def do_gmc(self, request_dict, uaql, response):
|
|
|
|
|
|
n_time = request_dict.get('n_time')
|
|
|
- n_type = request_dict.get('n_type')
|
|
|
- msg = {'n_time': n_time, 'n_type': n_type}
|
|
|
+ appBundleId = uaql['appBundleId']
|
|
|
+ token_val = uaql['token_val']
|
|
|
+ uid = uaql['uid']
|
|
|
+ gcm_config = {'xxx': 'key'}
|
|
|
+ serverKey = gcm_config[appBundleId]
|
|
|
+
|
|
|
+ msg = {'n_time': n_time, 'uid': uid}
|
|
|
json_data = {
|
|
|
"collapse_key": "WhatYouWant",
|
|
|
"data": msg,
|
|
|
"delay_while_idle": False,
|
|
|
"time_to_live": 3600,
|
|
|
- "registration_ids": [
|
|
|
- 'eSooD4fAARg:APA91bEPenBPnSn5aXIQk56QdLOQ1Mu3hevHsekP_0eDpg458y2ZMBP6By2rVsGYIoxZrXxvkkptPKUE9CmUygBxaZXABddUWB9FyLlznRFerC7RG9X5PsEOH58xK9_aTUdkT7p6Ocld']
|
|
|
+ "registration_ids": [token_val]
|
|
|
}
|
|
|
|
|
|
url = 'https://android.googleapis.com/gcm/send'
|
|
|
- serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I"
|
|
|
+ # 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, response):
|
|
|
- device_token = request_dict.get('device_token')
|
|
|
-
|
|
|
+ def do_apns(self, request_dict, uaql, response):
|
|
|
+ token_val = uaql['token_val']
|
|
|
+ n_time = request_dict.get('n_time')
|
|
|
+ appBundleId = uaql['appBundleId']
|
|
|
+ uid = uaql['uid']
|
|
|
+ apns_config = {'appbundleId': {'pem_path': 'xxxx', 'topic': 'topic'}}
|
|
|
try:
|
|
|
- daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103))
|
|
|
- print(daytime)
|
|
|
- pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem')
|
|
|
+ # 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=device_token, topic='com.ansjer.zsavcloud')
|
|
|
+ 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:
|
|
@@ -241,3 +242,15 @@ class NotificationView(View):
|
|
|
return response.json(404, res.reason)
|
|
|
except Exception as e:
|
|
|
return response.json(10, repr(e))
|
|
|
+
|
|
|
+ def do_save_equipment_info(self,userID_id):
|
|
|
+ Equipment_Info.objects.create(
|
|
|
+ userID_id=userID_id,
|
|
|
+ eventTime='',
|
|
|
+ eventType='',
|
|
|
+ devUid='',
|
|
|
+ devNickName='',
|
|
|
+ Channel='',
|
|
|
+ alarm='',
|
|
|
+ receiveTime='',)
|
|
|
+ return
|