EquipmentStatus.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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['utc_offset']
  105. gmtz = tz.replace('UTC','GMT')
  106. if ':' not in gmtz:
  107. gmtz = gmtz+':00'
  108. info['gmt_offset'] = gmtz
  109. # tz = info.timezone
  110. # of = info.utc_offset
  111. # from var_dump import var_dump
  112. # var_dump(info)
  113. return JsonResponse(status=200, data={'code': 0, 'msg': 'success', 'data': info})
  114. else:
  115. return JsonResponse(status=200, data={'code': 404, 'msg': '令牌错误'})
  116. else:
  117. return JsonResponse(status=200, data={'code': 444, 'msg': '接口参数错误 etk'})
  118. #
  119. # # 设备预览图
  120. # def addUIDPreview(request):
  121. # request.encoding = 'utf-8'
  122. # response = ResponseObject()
  123. # if request.method == 'GET':
  124. # request_dict = request.GET
  125. # elif request.method == 'POST':
  126. # request_dict = request.POST
  127. # else:
  128. # return response.json(414)
  129. # channel = request_dict.get('channel')
  130. # uid = request_dict.get('channel')
  131. # token = request_dict.get('token')
  132. # if all([token, uid, channel]):
  133. # tko = TokenObject(token)
  134. # if tko.code == 0:
  135. # nowTime = int(time.time())
  136. # upqs = UID_Preview.objects.filter(uid=uid, channel=channel)
  137. # if upqs.exists():
  138. # try:
  139. # is_update = upqs.update(updTime=nowTime)
  140. # except Exception as e:
  141. # print(repr(e))
  142. # return response.json(177)
  143. # else:
  144. # if is_update:
  145. # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  146. # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  147. # obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
  148. # # 设置此签名URL在60秒内有效。
  149. # url = bucket.sign_url('PUT', obj, 7200)
  150. # return response.json(0, url)
  151. # else:
  152. # return response.json(177)
  153. # else:
  154. # create_data = {
  155. # 'addTime': nowTime,
  156. # 'updTime': nowTime,
  157. # 'uid': uid,
  158. # 'channel': channel
  159. # }
  160. # try:
  161. # UID_Preview.objects.create(**create_data)
  162. # except Exception as e:
  163. # print(repr(e))
  164. # return response.json(178)
  165. # else:
  166. # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  167. # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  168. # obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
  169. # # 设置此签名URL在60秒内有效。
  170. # url = bucket.sign_url('PUT', obj, 7200)
  171. # return response.json(0, url)
  172. # else:
  173. # return response.json(tko.code)
  174. # else:
  175. # return response.json(444)