EquipmentStatus.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/6/7 9:27
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: EquipmentStat.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import base64
  15. import urllib.parse
  16. from Ansjer.config import BASE_DIR
  17. from django.http import JsonResponse
  18. from django.views.decorators.csrf import csrf_exempt
  19. import ipdb
  20. from Object.TokenObject import TokenObject
  21. from Model.models import Device_Info, UID_Preview
  22. from Object.ResponseObject import ResponseObject
  23. from Service.CommonService import CommonService
  24. import time
  25. import oss2
  26. from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
  27. '''
  28. http://192.168.136.45:8077/device/online?UID=C2887N9EBS87ZAT1111A
  29. http://192.168.136.45:8077/device/offline?UID=C2887N9EBS87ZAT1111A
  30. http://192.168.136.45:8077/device/updateIP?UID=C2887N9EBS87ZAT1111A?ip=190.9.194.216
  31. '''
  32. @csrf_exempt
  33. def EquipmentOnline(request):
  34. response = ResponseObject()
  35. if request.method == 'GET':
  36. request.encoding = 'utf-8'
  37. UID = request.GET.get('UID', None)
  38. if request.method == 'POST':
  39. request.encoding = 'utf-8'
  40. UID = request.POST.get('UID', None)
  41. qs = Device_Info.objects.filter(UID=UID)
  42. if not qs.exists():
  43. return response.json(173)
  44. @csrf_exempt
  45. def EquipmentOffline(request):
  46. response = ResponseObject()
  47. if request.method == 'GET':
  48. request.encoding = 'utf-8'
  49. UID = request.GET.get('UID', None)
  50. if request.method == 'POST':
  51. request.encoding = 'utf-8'
  52. UID = request.POST.get('UID', None)
  53. qs = Device_Info.objects.filter(UID=UID)
  54. if not qs.exists():
  55. return response.json(173)
  56. try:
  57. qs.update(Online=False)
  58. except Exception as e:
  59. print(repr(e))
  60. return response.json(0)
  61. @csrf_exempt
  62. def updateIP(request):
  63. response = ResponseObject()
  64. if request.method == 'GET':
  65. request.encoding = 'utf-8'
  66. request_dict = request.GET
  67. if request.method == 'POST':
  68. request.encoding = 'utf-8'
  69. request_dict = request.POST
  70. UID = request_dict.get('UID', None)
  71. ip = request_dict.get('ip', None)
  72. qs = Device_Info.objects.filter(UID=UID)
  73. if not qs.exists():
  74. return response.json(173)
  75. area = ''
  76. try:
  77. area = CommonService.getAddr(ip=ip)
  78. qs.update(ip=ip, area=area)
  79. except Exception as e:
  80. print(repr(e))
  81. return response.json(0, {'area': area})
  82. def getTZ(request):
  83. request.encoding = 'utf-8'
  84. if request.method == 'GET':
  85. request_dict = request.GET
  86. elif request.method == 'POST':
  87. request_dict = request.POST
  88. else:
  89. return JsonResponse(status=200, data={'code': 403, 'msg': '接口请求方法错误'})
  90. c = request_dict.get('etk', None)
  91. ip = request_dict.get('ip', None)
  92. lang = request_dict.get('lang', None)
  93. if not ip:
  94. ip = CommonService.get_ip_address(request)
  95. if c:
  96. c = base64.b64decode(c)
  97. c = c[2:-2]
  98. c = urllib.parse.unquote(c.decode('utf-8'))
  99. c = base64.b64decode(c)
  100. uid = c.decode('utf-8')
  101. if len(uid) == 20:
  102. print(uid)
  103. info = CommonService.getIpIpInfo(ip=ip, lang=lang)
  104. # tz = info.timezone
  105. # of = info.utc_offset
  106. # from var_dump import var_dump
  107. # var_dump(info)
  108. return JsonResponse(status=200, data={'code': 0, 'msg': 'success', 'data': info})
  109. else:
  110. return JsonResponse(status=200, data={'code': 404, 'msg': '令牌错误'})
  111. else:
  112. return JsonResponse(status=200, data={'code': 444, 'msg': '接口参数错误 etk'})
  113. # 设备预览图
  114. def addUIDPreview(request):
  115. request.encoding = 'utf-8'
  116. response = ResponseObject()
  117. if request.method == 'GET':
  118. request_dict = request.GET
  119. elif request.method == 'POST':
  120. request_dict = request.POST
  121. else:
  122. return response.json(414)
  123. channel = request_dict.get('channel')
  124. uid = request_dict.get('channel')
  125. token = request_dict.get('token')
  126. if all([token, uid, channel]):
  127. tko = TokenObject(token)
  128. if tko.code == 0:
  129. nowTime = int(time.time())
  130. upqs = UID_Preview.objects.filter(uid=uid, channel=channel)
  131. if upqs.exists():
  132. try:
  133. is_update = upqs.update(updTime=nowTime)
  134. except Exception as e:
  135. print(repr(e))
  136. return response.json(177)
  137. else:
  138. if is_update:
  139. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  140. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  141. obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
  142. # 设置此签名URL在60秒内有效。
  143. url = bucket.sign_url('PUT', obj, 7200)
  144. return response.json(0, url)
  145. else:
  146. return response.json(177)
  147. else:
  148. create_data = {
  149. 'addTime': nowTime,
  150. 'updTime': nowTime,
  151. 'uid': uid,
  152. 'channel': channel
  153. }
  154. try:
  155. UID_Preview.objects.create(**create_data)
  156. except Exception as e:
  157. print(repr(e))
  158. return response.json(178)
  159. else:
  160. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  161. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  162. obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
  163. # 设置此签名URL在60秒内有效。
  164. url = bucket.sign_url('PUT', obj, 7200)
  165. return response.json(0, url)
  166. else:
  167. return response.json(tko.code)
  168. else:
  169. return response.json(444)