EquipmentInfo.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. from django.views.generic.base import View
  2. from django.utils.decorators import method_decorator
  3. from django.views.decorators.csrf import csrf_exempt
  4. from Service.ModelService import ModelService
  5. from Service.CommonService import CommonService
  6. from Model.models import Equipment_Info, Device_Info, Device_User
  7. import traceback
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from django.views.generic import TemplateView
  11. '''
  12. http://192.168.136.45:8077/equipment/info?token=test&devUid=2N1K3LE78TYJ38CE111A&Channel=0&eventType=1&eventTime=1234567890&operation=add&alarm=21342134&receiveTime=1234567891234567
  13. http://test.dvema.com/equipment/info?token=test&operation=query&page=1&line=10
  14. http://192.168.136.45:8077/equipment/info?token=test&operation=delete&devUid=UKPAH63V23U4ZHEB111A&id=5&id=6&id=7
  15. http://192.168.136.40:8077/equipment/info?token=test&operation=update&devUid=UKPAH63V23U4ZHEB111A&id=3&id=4
  16. http://192.168.136.45:8077/equipment/info?token=test&operation=queryByAdmin&page=1&line=10&userID_id=151547867345163613800138001
  17. http://192.168.136.45:8077/equipment/info?token=test&operation=deleteByAdmin&id=5&id=6&id=7
  18. '''
  19. class EquipmentInfo(View):
  20. @method_decorator(csrf_exempt)
  21. def dispatch(self, *args, **kwargs):
  22. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  23. def get(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. return self.validation(request_dict=request.GET)
  26. def post(self, request, *args, **kwargs):
  27. request.encoding = 'utf-8'
  28. return self.validation(request_dict=request.POST)
  29. def validation(self, request_dict, *args, **kwargs):
  30. token = request_dict.get('token', None)
  31. response = ResponseObject()
  32. tko = TokenObject(token)
  33. tko.valid()
  34. if tko.code != 0:
  35. return response.json(tko.code)
  36. userID = tko.userID
  37. operation = request_dict.get('operation', None)
  38. if not userID:
  39. return response.json(444, 'operation')
  40. if operation == 'query':
  41. return self.query_info(request_dict, userID, response)
  42. elif operation == 'add':
  43. return self.add_info(request_dict, userID, response)
  44. elif operation == 'update':
  45. return self.update_info(request_dict, userID, response)
  46. elif operation == 'delete':
  47. return self.delete_info(request_dict, userID, response)
  48. elif operation == 'findByTime':
  49. return self.findByTime_info(request_dict, userID, response)
  50. if operation == 'queryByAdmin':
  51. return self.query_all_info(request_dict, userID, response)
  52. if operation == 'deleteByAdmin':
  53. return self.delete_by_admin(request_dict, userID, response)
  54. else:
  55. return response.json(444, 'operation')
  56. def add_info(self, request_dict, userID, response):
  57. devUid = request_dict.get('devUid', None)
  58. Channel = request_dict.get('Channel', None)
  59. eventType = request_dict.get('eventType', None)
  60. eventTime = request_dict.get('eventTime', None)
  61. receiveTime = request_dict.get('receiveTime', None)
  62. alarm = request_dict.get('alarm', None)
  63. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  64. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  65. own_device = ModelService.check_own_device(userID, UID=devUid)
  66. if own_device is not True:
  67. return response.json(14)
  68. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  69. if not device_info.exists():
  70. return response.json(14)
  71. try:
  72. equipment_info = Equipment_Info(
  73. userID=Device_User.objects.get(userID=userID),
  74. devUid=devUid,
  75. Channel=Channel,
  76. eventType=eventType,
  77. receiveTime=receiveTime,
  78. eventTime=eventTime,
  79. viewPwd=device_info[0].View_Password,
  80. devNickName=device_info[0].NickName,
  81. viewAccont=device_info[0].View_Account,
  82. alarm=alarm
  83. )
  84. equipment_info.save()
  85. except Exception:
  86. errorInfo = traceback.format_exc()
  87. print(errorInfo)
  88. return response.json(500, errorInfo)
  89. else:
  90. if equipment_info.id:
  91. return response.json(0, {'infoID': equipment_info.id,
  92. 'devUid': devUid,
  93. 'Channel': Channel,
  94. 'alarm': alarm,
  95. 'eventType': eventType,
  96. 'eventTime': eventTime,
  97. 'receiveTime': receiveTime,
  98. 'viewPwd': equipment_info.viewPwd,
  99. 'devNickName': equipment_info.devNickName,
  100. 'viewAccont': equipment_info.viewAccont})
  101. else:
  102. return response.json(500)
  103. def query_info(self, request_dict, userID, response):
  104. page = int(request_dict.get('page', None))
  105. line = int(request_dict.get('line', None))
  106. if not page or not line:
  107. return response.json(444, 'page,line')
  108. qs = Equipment_Info.objects.filter(userID_id=userID)
  109. uid = request_dict.get('uid', None)
  110. if uid:
  111. qs.filter(devUid=uid)
  112. if not qs.exists():
  113. return response.json(0, {'datas': [], 'count': 0})
  114. count = qs.count()
  115. res = qs[(page - 1) * line:page * line]
  116. send_json = CommonService.qs_to_dict(res)
  117. send_json['count'] = count
  118. return response.json(0, send_json)
  119. def update_info(self, request_dict, userID, response):
  120. id_list = request_dict.getlist('id[]', None)
  121. if id_list is None or len(id_list) < 1:
  122. id_list = request_dict.getlist('id', None)
  123. param_flag = CommonService.get_param_flag(data=[id_list])
  124. if param_flag is True:
  125. count = 0
  126. for id in id_list:
  127. try:
  128. eq = Equipment_Info.objects.filter(id=int(id))
  129. if eq.exists():
  130. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  131. if own_dev is True:
  132. count += 1
  133. eq.update(status=1)
  134. except Exception as e:
  135. print(repr(e))
  136. return response.json(0, {'update_success': count})
  137. else:
  138. return response.json(444)
  139. def delete_info(self, request_dict, userID, response):
  140. id_list = request_dict.getlist('id[]', None)
  141. if id_list is None or len(id_list) < 1:
  142. id_list = request_dict.getlist('id', None)
  143. param_flag = CommonService.get_param_flag(data=[id_list])
  144. if param_flag is True:
  145. try:
  146. for id in id_list:
  147. eq = Equipment_Info.objects.filter(id=id)
  148. if eq.exists():
  149. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  150. if own_dev is True:
  151. eq.delete()
  152. except Exception as e:
  153. errorInfo = traceback.format_exc()
  154. print(errorInfo)
  155. return response.json(424, repr(e))
  156. else:
  157. return response.json(0)
  158. else:
  159. return response.json(444)
  160. def findByTime_info(self, request_dict, userID, response):
  161. startTime = request_dict.get('startTime')
  162. endTime = request_dict.get('endTime')
  163. page = int(request_dict.get('page', None))
  164. line = int(request_dict.get('line', None))
  165. if not startTime or not endTime or not page or not line:
  166. return response.json(444, 'startTime, endTime, page, line')
  167. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  168. if not len(uid_list):
  169. return response.json(0, {'datas': [], 'count': 0})
  170. qs = Equipment_Info.objects.filter(userID_id=userID,
  171. eventTime__range=(startTime, endTime)).order_by('-id')
  172. if qs.exists():
  173. count = qs.count()
  174. res = qs[(page - 1) * line:page * line]
  175. send_json = CommonService.qs_to_dict(res)
  176. send_json['count'] = count
  177. return response.json(0, send_json)
  178. def query_all_info(self, request_dict, userID, response):
  179. page = int(request_dict.get('page', None))
  180. line = int(request_dict.get('line', None))
  181. userID_id = request_dict.get('userID_id', None)
  182. if not page or not line:
  183. return response.json(444, 'page,line')
  184. check_perm = ModelService.check_permission(userID=userID, permID=30)
  185. if not check_perm:
  186. return response.json(404)
  187. uid_list = Device_Info.objects.filter(userID_id=userID_id).values_list('UID', flat=True)
  188. if not len(uid_list):
  189. return response.json(0, {'datas': [], 'count': 0})
  190. qs = Equipment_Info.objects.filter(userID_id=userID_id).order_by('-id')
  191. if not qs.exists():
  192. return response.json(0, {'datas': [], 'count': 0})
  193. count = qs.count()
  194. res = qs[(page - 1) * line:page * line]
  195. send_json = CommonService.qs_to_dict(res)
  196. send_json['count'] = count
  197. return response.json(0, send_json)
  198. def delete_by_admin(self, request_dict, userID, response):
  199. id_list = request_dict.getlist('id', None)
  200. if not len(id_list):
  201. return response.json(444, 'id is None or not list')
  202. check_perm = ModelService.check_permission(userID=userID, permID=10)
  203. if check_perm is True:
  204. try:
  205. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  206. except Exception as e:
  207. errorInfo = traceback.format_exc()
  208. print(errorInfo)
  209. return response.json(424, {'details': repr(e)})
  210. else:
  211. return response.json(0, {'delete_count': is_delete[0]})
  212. else:
  213. return response.json(404)
  214. # 设备推送信息添加接口
  215. class AddDetectionView(TemplateView):
  216. @method_decorator(csrf_exempt)
  217. def dispatch(self, *args, **kwargs):
  218. return super(AddDetectionView, self).dispatch(*args, **kwargs)
  219. def get(self, request, *args, **kwargs):
  220. request.encoding = 'utf-8'
  221. return self.validation(request_dict=request.GET)
  222. def post(self, request, *args, **kwargs):
  223. request.encoding = 'utf-8'
  224. return self.validation(request_dict=request.POST)
  225. def validation(self, request_dict, *args, **kwargs):
  226. response = ResponseObject()
  227. # 设备添加token
  228. UID = request_dict.get('UID', None)
  229. Channel = request_dict.get('Channel', None)
  230. eventType = request_dict.get('eventType', None)
  231. eventTime = request_dict.get('eventTime', None)
  232. receiveTime = request_dict.get('receiveTime', None)
  233. alarm = request_dict.get('alarm', None)
  234. if not UID or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  235. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  236. # dv_qs = Device_Info.objects.filter(UID=UID, isShare=False)
  237. dv_qs = Device_Info.objects.filter(UID=UID,isDetector=True).values("userID_id","NickName")
  238. if not dv_qs.exists():
  239. return response.json(10,'device not detect')
  240. qs_list = []
  241. for dv in dv_qs:
  242. print(dv)
  243. add_data = {
  244. # 'userID': Device_User.objects.get(userID=dv["userID_id"]),
  245. 'userID_id': dv["userID_id"],
  246. 'devUid': UID,
  247. 'Channel': Channel,
  248. 'eventType': eventType,
  249. 'receiveTime': receiveTime,
  250. 'eventTime': eventTime,
  251. 'devNickName': dv["NickName"],
  252. 'viewPwd': '',
  253. 'viewAccont': '',
  254. 'alarm': alarm
  255. }
  256. qs_list.append(Equipment_Info(**add_data))
  257. if qs_list:
  258. print(1)
  259. Equipment_Info.objects.bulk_create(qs_list)
  260. print(2)
  261. return response.json(0)