DetectControllerV2.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. import os
  15. import time
  16. import apns2
  17. import jpush as jpush
  18. import oss2
  19. from django.http import JsonResponse
  20. from django.views.generic.base import View
  21. from pyfcm import FCMNotification
  22. from Object.RedisObject import RedisObject
  23. from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, DETECT_PUSH_DOMAIN, \
  24. JPUSH_CONFIG, \
  25. FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE, DETECT_PUSH_DOMAINS, DETECT_PUSH_DOMAIN_JIUAN, \
  26. DETECT_PUSH_DOMAINS_JIUAN
  27. from Model.models import Device_Info, VodHlsModel, Equipment_Info, UidSetModel, UidPushModel, CompanyModel
  28. from Object.ETkObject import ETkObject
  29. from Object.RedisObject import RedisObject
  30. from Object.ResponseObject import ResponseObject
  31. from Object.TokenObject import TokenObject
  32. from Object.UidTokenObject import UidTokenObject
  33. from Service.CommonService import CommonService
  34. from Service.ModelService import ModelService
  35. '''
  36. http://test.push.dvema.com/notify/push?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJUNEFaM0NVS0NFUkg5RlpBMTExQSJ9.GtrXeq5gb2Z9M3mKECxi9eNQbPxqC-6PtgJkOOg6PwI&n_time=1598456451&channel=1&event_type=1&is_st=1
  37. http://push.dvema.com/notify/push?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJUNEFaM0NVS0NFUkg5RlpBMTExQSJ9.GtrXeq5gb2Z9M3mKECxi9eNQbPxqC-6PtgJkOOg6PwI&n_time=1598456451&channel=1&event_type=1&is_st=1
  38. http://www.dvema.com/detect/changeStatus?push_type=2&token_val=1a0018970a332cca935&appBundleId=com.ansjer.zccloud_ab&tz=+08.00&uid=HLK7EJ2VYLNHHUMG111A&status=1&m_code=AN02000025070000001207.zccloud_ab&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiMTMxMTk2NTc3MTMiLCJsYW5nIjoiZW4iLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJ1c2VySUQiOiIxNTQ0ODM4MjMyOTczMTM4MDAxMzgwMDAiLCJleHAiOjE1ODQxNDc0NjN9.NjK91B26jZDtdmq8tW-8hXNQPqqDfo9Zwf_Jg6Uf7co&lang=cn&app_type=2
  39. http://127.0.0.1:8077/detect/changeStatus?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTg0MzUxODk2MjgyMTM4MDAxMzgwMDAiLCJsYW5nIjoiZW4iLCJ1c2VyIjoiMTM2ODAzMTc1OTYiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1ODcyNjEzODN9.H87931EDg6PGZK63EAsvY1FRJov9qo_S70mBKvpZeQM&push_type=2&token_val=token_val&appBundleId=appBundleId&tz=0&uid=158440619973313800138000&status=1&m_code&lang=cn&app_type=0&start_status=1&interval=60&eventType=60
  40. http://192.168.136.140:8077/detect/changeStatus?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTg0MzUxODk2MjgyMTM4MDAxMzgwMDAiLCJsYW5nIjoiZW4iLCJ1c2VyIjoiMTM2ODAzMTc1OTYiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1ODcyNjEzODN9.H87931EDg6PGZK63EAsvY1FRJov9qo_S70mBKvpZeQM&push_type=2&token_val=token_val&appBundleId=appBundleId&tz=0&uid=158440619973313800138000&status=1&m_code&lang=cn&app_type=0&start_status=1&interval=60&eventType=60
  41. detect/changeStatus
  42. '''
  43. class DetectControllerViewV2(View):
  44. def get(self, request, *args, **kwargs):
  45. request.encoding = 'utf-8'
  46. operation = kwargs.get('operation')
  47. # self.ip = CommonService.get_ip_address(request)
  48. return self.validation(request.GET, operation)
  49. def post(self, request, *args, **kwargs):
  50. request.encoding = 'utf-8'
  51. operation = kwargs.get('operation')
  52. # self.ip = CommonService.get_ip_address(request)
  53. return self.validation(request.POST, operation)
  54. def validation(self, request_dict, operation):
  55. response = ResponseObject()
  56. if operation == 'generateUIDToken':
  57. return self.generateUIDToken(request_dict, response)
  58. if operation is None:
  59. return response.json(444, 'error path')
  60. token = request_dict.get('token', None)
  61. tko = TokenObject(token)
  62. if tko.code == 0:
  63. userID = tko.userID
  64. # 修改推送设置
  65. if operation == 'changeStatus':
  66. print("进入changeStatus")
  67. return self.do_change_status(userID, request_dict, response)
  68. else:
  69. return response.json(414)
  70. else:
  71. return response.json(tko.code)
  72. def do_change_status(self, userID, request_dict, response):
  73. token_val = request_dict.get('token_val', None)
  74. appBundleId = request_dict.get('appBundleId', None)
  75. app_type = request_dict.get('app_type', None)
  76. push_type = request_dict.get('push_type', None)
  77. status = request_dict.get('status', None)
  78. m_code = request_dict.get('m_code', None)
  79. uid = request_dict.get('uid', None)
  80. # 设备语言
  81. lang = request_dict.get('lang', 'en')
  82. tz = request_dict.get('tz', '0')
  83. company_secrete = request_dict.get('company_secrete', None)
  84. # 消息提醒功能新增
  85. # 如果传空上来,就默认为0
  86. if tz == '':
  87. tz = 0
  88. detect_group = request_dict.get('detect_group', None)
  89. interval = request_dict.get('interval', None)
  90. if not status:
  91. return response.json(444, 'status')
  92. if not company_secrete:
  93. return response.json(444, 'company_secrete')
  94. company = CompanyModel.objects.filter(secret=company_secrete)
  95. if not company.exists():
  96. return response.json(444, 'company_secrete')
  97. # 关闭推送
  98. if not all([appBundleId, app_type, token_val, uid, m_code]):
  99. return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
  100. # 判断推送类型对应key是否存在
  101. print('push_type:', push_type)
  102. if push_type == '0':
  103. if appBundleId not in APNS_CONFIG.keys():
  104. return response.json(904)
  105. elif push_type == '1':
  106. if appBundleId not in FCM_CONFIG.keys():
  107. return response.json(904)
  108. elif push_type == '2':
  109. if appBundleId not in JPUSH_CONFIG.keys():
  110. return response.json(904)
  111. else:
  112. return response.json(173)
  113. dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
  114. status = int(status)
  115. # 获取用户区域
  116. # ip = self.ip
  117. # ipInfo = CommonService.getIpIpInfo(ip=ip, lang='EN')
  118. # area = ipInfo['country_name']
  119. # if area == 'China':
  120. # DETECT_PUSH_DOMAIN_V2 = 'cn.push.dvema.com'
  121. # else:
  122. # DETECT_PUSH_DOMAIN_V2 = 'en.push.dvema.com'
  123. nowTime = int(time.time())
  124. if dvqs.exists():
  125. # 修改状态
  126. dvqs.update(NotificationMode=status)
  127. uid_set_qs = UidSetModel.objects.filter(uid=uid)
  128. # uid配置信息是否存在
  129. if uid_set_qs.exists():
  130. uid_set_id = uid_set_qs[0].id
  131. qs_data = {
  132. 'detect_status': status,
  133. 'updTime': nowTime,
  134. }
  135. if interval:
  136. qs_data['detect_interval'] = int(interval)
  137. if detect_group:
  138. qs_data['detect_group'] = detect_group
  139. print(qs_data)
  140. uid_set_qs.update(**qs_data)
  141. else:
  142. qs_data = {
  143. 'uid': uid,
  144. 'addTime': nowTime,
  145. 'updTime': nowTime,
  146. 'detect_status': status,
  147. }
  148. if interval:
  149. qs_data['detect_interval'] = int(interval)
  150. if detect_group:
  151. qs_data['detect_group'] = detect_group
  152. # 添加设备配置
  153. uid_set_qs = UidSetModel.objects.create(**qs_data)
  154. uid_set_id = uid_set_qs.id
  155. if status == 0:
  156. UidPushModel.objects.filter(uid_set__uid=uid).delete()
  157. # 状态为0的时候删除redis缓存数据
  158. self.do_delete_redis(uid)
  159. return response.json(0)
  160. elif status == 1:
  161. uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
  162. # ykey = '{uid}_redis_qs'.format(uid=uid)
  163. # redisObj = RedisObject(db=6, SERVER_HOST='push.dvema.com')
  164. # redisObj.del_data(key=ykey)
  165. if uid_push_qs.exists():
  166. uid_push_update_dict = {
  167. 'appBundleId': appBundleId,
  168. 'app_type': app_type,
  169. 'push_type': push_type,
  170. 'token_val': token_val,
  171. 'updTime': nowTime,
  172. 'lang': lang,
  173. 'tz': tz
  174. }
  175. uid_push_qs.update(**uid_push_update_dict)
  176. else:
  177. # uid_set_id = uid_set_qs[0].id
  178. uid_push_create_dict = {
  179. 'uid_set_id': uid_set_id,
  180. 'userID_id': userID,
  181. 'appBundleId': appBundleId,
  182. 'app_type': app_type,
  183. 'push_type': push_type,
  184. 'token_val': token_val,
  185. 'm_code': m_code,
  186. 'addTime': nowTime,
  187. 'updTime': nowTime,
  188. 'lang': lang,
  189. 'tz': tz
  190. }
  191. # 绑定设备推送
  192. UidPushModel.objects.create(**uid_push_create_dict)
  193. if interval:
  194. self.do_delete_redis(uid, int(interval))
  195. else:
  196. self.do_delete_redis(uid)
  197. # utko = UidTokenObject()
  198. # # right
  199. # utko.generate(data={'uid': uid})
  200. etkObj = ETkObject(etk='')
  201. etk = etkObj.encrypt(uid)
  202. if company_secrete == 'MTEyMTNB':
  203. url = DETECT_PUSH_DOMAIN
  204. urls = DETECT_PUSH_DOMAINS
  205. else:
  206. url = DETECT_PUSH_DOMAIN_JIUAN
  207. urls = DETECT_PUSH_DOMAINS_JIUAN
  208. detectUrl = "{DETECT_PUSH_DOMAIN}notifyV2/push?etk={etk}&company_secrete={company_secrete}". \
  209. format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN=url)
  210. detectUrls = "{DETECT_PUSH_DOMAIN_V2}notifyV2/push?etk={etk}&company_secrete={company_secrete}". \
  211. format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN_V2=urls)
  212. return response.json(0, {'detectUrl': detectUrl, 'detectUrls': detectUrls})
  213. else:
  214. return response.json(14)
  215. def do_delete_redis(self, uid, detect_interval=0):
  216. keyPattern = '{uid}*'.format(uid=uid)
  217. redisObj = RedisObject(db=6, SERVER_HOST='push.dvema.com')
  218. keys = redisObj.get_keys(keyPattern)
  219. if keys:
  220. for key in keys:
  221. key = key.decode()
  222. if detect_interval == 0:
  223. redisObj.del_data(key=key)
  224. elif key.find('plt') != -1:
  225. continue
  226. elif key.find('flag') != -1:
  227. redisObj.set_data(key=key, val=1, expire=detect_interval)
  228. else:
  229. redisObj.del_data(key=key)