DetectController.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/1/14 15:57
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: DetectController.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.utils.decorators import method_decorator
  15. from django.views.decorators.csrf import csrf_exempt
  16. from django.views.generic.base import View
  17. import time
  18. import apns2
  19. from Object.ResponseObject import ResponseObject
  20. import os
  21. from Ansjer.config import BASE_DIR
  22. from Object.TokenObject import TokenObject
  23. import jpush as jpush
  24. from Model.models import Device_User, Device_Info, Equipment_Info, App_Info,UID_App
  25. class DetectControllerView(View):
  26. @method_decorator(csrf_exempt)
  27. def dispatch(self, *args, **kwargs):
  28. return super(DetectControllerView, self).dispatch(*args, **kwargs)
  29. def get(self, request, *args, **kwargs):
  30. request.encoding = 'utf-8'
  31. operation = kwargs.get('operation')
  32. return self.validation(request.GET, operation)
  33. def post(self, request, *args, **kwargs):
  34. request.encoding = 'utf-8'
  35. operation = kwargs.get('operation')
  36. return self.validation(request.POST, operation)
  37. def validation(self, request_dict, operation):
  38. response = ResponseObject()
  39. if operation is None:
  40. return response.json(444, 'error path')
  41. token = request_dict.get('token', None)
  42. uid = request_dict.get('uid', None)
  43. tko = TokenObject(token)
  44. if tko.code == 0:
  45. userID = tko.userID
  46. if operation == 'changeStatus':
  47. return self.do_change_status(userID, uid, response)
  48. else:
  49. return response.json(414)
  50. else:
  51. return response.json(tko.code)
  52. def do_change_status(self, userID, uid,response):
  53. dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
  54. if dvqs.exists():
  55. return
  56. else:
  57. return response.json(173)
  58. def do_push(self, request_dict, response):
  59. device_token = request_dict.get('device_token')
  60. try:
  61. daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103))
  62. print(daytime)
  63. pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem')
  64. cli = apns2.APNSClient(mode="dev", client_cert=pem_path, password='111111')
  65. alert = apns2.PayloadAlert(body="body!", title="title!")
  66. payload = apns2.Payload(alert=alert)
  67. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  68. res = cli.push(n=n, device_token=device_token, topic='com.ansjer.zsavcloud')
  69. # assert res.status_code == 200, res.reason
  70. # assert res.apns_id
  71. if res.status_code == 200:
  72. return response.json(0)
  73. else:
  74. return response.json(404, res.reason)
  75. except Exception as e:
  76. return response.json(10, repr(e))
  77. class NotificationView(View):
  78. def get(self, request, *args, **kwargs):
  79. request.encoding = 'utf-8'
  80. operation = kwargs.get('operation')
  81. return self.validation(request.GET, operation)
  82. def post(self, request, *args, **kwargs):
  83. request.encoding = 'utf-8'
  84. operation = kwargs.get('operation')
  85. return self.validation(request.POST, operation)
  86. def validation(self, request_dict, operation):
  87. response = ResponseObject()
  88. if operation is None:
  89. return response.json(444, 'error path')
  90. uidToken = request_dict.get('uidToken', None)
  91. time = request_dict.get('time', None)
  92. # 设备主键uid
  93. tko = TokenObject(uidToken)
  94. response.lang = tko.lang
  95. if tko.code != 0:
  96. if operation == 'jpush':
  97. return
  98. elif operation == 'gcm':
  99. return
  100. elif operation == 'apns':
  101. return
  102. else:
  103. return response.json(414)
  104. else:
  105. return response.json(tko.code)
  106. def do_jpush(self):
  107. jpush_config = {
  108. 'com.ansjer.accloud_ab': {
  109. 'Key': 'f0dc047e5e53fd14199de5b0',
  110. 'Secret': 'aa7f7db33e9f0a7f3871aa1c'},
  111. 'com.ansjer.adcloud_ab': {
  112. 'Key': '76d97b535185114985608234',
  113. 'Secret': 'c9a92b301043cc9c52778692'},
  114. 'com.ansjer.zccloud_ab': {
  115. 'Key': 'd9924f56d3cc7c6017965130',
  116. 'Secret': '869d832d126a232f158b5987'},
  117. 'com.ansjer.loocamccloud_ab': {
  118. 'Key': 'd1cc44797b4642b0e05304fe',
  119. 'Secret': 'c3e8b4ca8c576de61401e56a'},
  120. 'com.ansjer.loocamdcloud_ab': {
  121. 'Key': '76d97b535185114985608234',
  122. 'Secret': 'c9a92b301043cc9c52778692'},
  123. 'com.ansjer.zccloud_a': {
  124. 'Key': '57de2a80d68bf270fd6bdf5a',
  125. 'Secret': '3d354eb6a0b49c2610decf42'},
  126. 'com.ansjer.accloud_a': {
  127. 'Key': 'ff95ee685f49c0dc4013347b',
  128. 'Secret': 'de2c20959f5516fdeeafe78e'},
  129. 'com.ansjer.adcloud_a': {
  130. 'Key': '2e47eb1aee9b164460df3668',
  131. 'Secret': 'b9137d8d684bc248f1809b6d'},
  132. 'com.ansjer.loocamccloud_a': {
  133. 'Key': '23c9213215c7ca0ec945629b',
  134. 'Secret': '81e4b1e859cc8387e2e6c431'},
  135. 'com.ansjer.loocamdcloud_a': {
  136. 'Key': '1dbdd60a16e9892d6f68a073',
  137. 'Secret': '80a97690e7e043109059b403'},
  138. 'com.ansjer.customizedb_a': {
  139. 'Key': '9d79630aa49adfa291fe2568',
  140. 'Secret': '4d8ff52f88136561875a0212'},
  141. }
  142. response = ResponseObject()
  143. devToken = ''
  144. app_key = ''
  145. master_secret = ''
  146. # 此处换成各自的app_key和master_secret
  147. _jpush = jpush.JPush(app_key, master_secret)
  148. push = _jpush.create_push()
  149. # if you set the logging level to "DEBUG",it will show the debug logging.
  150. _jpush.set_logging("DEBUG")
  151. # push.audience = jpush.all_
  152. push.audience = jpush.registration_id(devToken)
  153. push.notification = jpush.notification(alert="hello python jpush api")
  154. push.platform = jpush.all_
  155. try:
  156. res = push.send()
  157. except Exception as e:
  158. print("Exception")
  159. return response.json(10, repr(e))
  160. return response.json(0)
  161. def do_gmc(self):
  162. import json
  163. import requests
  164. data = {
  165. "key1": "abc",
  166. "key2": "def"
  167. }
  168. json_data = {
  169. "collapse_key": "WhatYouWant",
  170. "data": data,
  171. "delay_while_idle": False,
  172. "time_to_live": 3600,
  173. "registration_ids": [
  174. 'eSooD4fAARg:APA91bEPenBPnSn5aXIQk56QdLOQ1Mu3hevHsekP_0eDpg458y2ZMBP6By2rVsGYIoxZrXxvkkptPKUE9CmUygBxaZXABddUWB9FyLlznRFerC7RG9X5PsEOH58xK9_aTUdkT7p6Ocld']
  175. }
  176. url = 'https://android.googleapis.com/gcm/send'
  177. serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I"
  178. data = json.dumps(json_data).encode('utf-8')
  179. headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
  180. req = requests.post(url, data, headers=headers)
  181. response = ResponseObject()
  182. return response.json(0)
  183. def do_apns(self):
  184. return