EquipmentInfo.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. '''
  11. http://192.168.136.45:8077/equipment/info?token=test&devUid=2N1K3LE78TYJ38CE111A&Channel=0&eventType=1&eventTime=1234567890&operation=add&alarm=21342134&receiveTime=1234567891234567
  12. http://192.168.136.45:8077/equipment/info?token=test&operation=query&page=1&line=10
  13. http://192.168.136.45:8077/equipment/info?token=test&operation=delete&devUid=UKPAH63V23U4ZHEB111A&id=5&id=6&id=7
  14. http://192.168.136.40:8077/equipment/info?token=test&operation=update&devUid=UKPAH63V23U4ZHEB111A&id=3&id=4
  15. http://192.168.136.45:8077/equipment/info?token=test&operation=queryByAdmin&page=1&line=10&userID_id=151547867345163613800138001
  16. http://192.168.136.45:8077/equipment/info?token=test&operation=deleteByAdmin&id=5&id=6&id=7
  17. '''
  18. class EquipmentInfo(View):
  19. @method_decorator(csrf_exempt)
  20. def dispatch(self, *args, **kwargs):
  21. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  22. def get(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. return self.validation(request_dict=request.GET)
  25. def post(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. return self.validation(request_dict=request.POST)
  28. def validation(self, request_dict, *args, **kwargs):
  29. token = request_dict.get('token', None)
  30. response = ResponseObject()
  31. tko = TokenObject(token)
  32. tko.valid()
  33. if tko.code != 0:
  34. return response.json(tko.code)
  35. userID = tko.userID
  36. operation = request_dict.get('operation', None)
  37. if not userID:
  38. return response.json(444, 'operation')
  39. if operation == 'query':
  40. return self.query_info(request_dict, userID, response)
  41. elif operation == 'add':
  42. return self.add_info(request_dict, userID, response)
  43. elif operation == 'update':
  44. return self.update_info(request_dict, userID, response)
  45. elif operation == 'delete':
  46. return self.delete_info(request_dict, userID, response)
  47. elif operation == 'findByTime':
  48. return self.findByTime_info(request_dict, userID, response)
  49. if operation == 'queryByAdmin':
  50. return self.query_all_info(request_dict, userID, response)
  51. if operation == 'deleteByAdmin':
  52. return self.delete_by_admin(request_dict, userID, response)
  53. else:
  54. return response.json(444, 'operation')
  55. def add_info(self, request_dict, userID,response):
  56. devUid = request_dict.get('devUid', None)
  57. Channel = request_dict.get('Channel', None)
  58. eventType = request_dict.get('eventType', None)
  59. eventTime = request_dict.get('eventTime', None)
  60. receiveTime = request_dict.get('receiveTime', None)
  61. alarm = request_dict.get('alarm', None)
  62. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  63. return response.json(444,'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  64. own_device = ModelService.check_own_device(userID, UID=devUid)
  65. if own_device is not True:
  66. return response.json(14)
  67. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  68. if not device_info.exists():
  69. return response.json(14)
  70. try:
  71. equipment_info = Equipment_Info(
  72. userID=Device_User.objects.get(userID=userID),
  73. devUid=devUid,
  74. Channel=Channel,
  75. eventType=eventType,
  76. receiveTime=receiveTime,
  77. eventTime=eventTime,
  78. viewPwd=device_info[0].View_Password,
  79. devNickName=device_info[0].NickName,
  80. viewAccont=device_info[0].View_Account,
  81. alarm=alarm
  82. )
  83. equipment_info.save()
  84. except Exception:
  85. errorInfo = traceback.format_exc()
  86. print(errorInfo)
  87. return response.json(500, errorInfo)
  88. else:
  89. if equipment_info.id:
  90. return response.json(0, {'infoID': equipment_info.id,
  91. 'devUid': devUid,
  92. 'Channel': Channel,
  93. 'alarm': alarm,
  94. 'eventType': eventType,
  95. 'eventTime': eventTime,
  96. 'receiveTime': receiveTime,
  97. 'viewPwd': equipment_info.viewPwd,
  98. 'devNickName': equipment_info.devNickName,
  99. 'viewAccont': equipment_info.viewAccont})
  100. else:
  101. return response.json(500)
  102. def query_info(self, request_dict, userID,response):
  103. page = int(request_dict.get('page', None))
  104. line = int(request_dict.get('line', None))
  105. if not page or not line:
  106. return response.json(444,'page,line')
  107. equipment_info_queryset = Equipment_Info.objects.filter(userID_id=userID)
  108. if not equipment_info_queryset.exists():
  109. return response.json(0, {'datas': [], 'count': 0})
  110. equipment_info_count = equipment_info_queryset.count()
  111. equipment_info_res = equipment_info_queryset[(page - 1) * line:page * line]
  112. send_json = CommonService.qs_to_dict(equipment_info_res)
  113. send_json['count'] = equipment_info_count
  114. return response.json(0, send_json)
  115. def update_info(self, request_dict, userID, response):
  116. id_list = request_dict.getlist('id[]', None)
  117. if id_list is None or len(id_list) < 1:
  118. id_list = request_dict.getlist('id', None)
  119. param_flag = CommonService.get_param_flag(data=[id_list])
  120. if param_flag is True:
  121. count = 0
  122. for id in id_list:
  123. try:
  124. eq = Equipment_Info.objects.filter(id=int(id))
  125. if eq.exists():
  126. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  127. if own_dev is True:
  128. count += 1
  129. eq.update(status=1)
  130. except Exception as e:
  131. print(repr(e))
  132. return response.json(0,{'update_success': count})
  133. else:
  134. return response.json(444)
  135. def delete_info(self, request_dict, userID, response):
  136. id_list = request_dict.getlist('id[]', None)
  137. if id_list is None or len(id_list) < 1:
  138. id_list = request_dict.getlist('id', None)
  139. param_flag = CommonService.get_param_flag(data=[id_list])
  140. if param_flag is True:
  141. try:
  142. for id in id_list:
  143. eq = Equipment_Info.objects.filter(id=id)
  144. if eq.exists():
  145. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  146. if own_dev is True:
  147. eq.delete()
  148. except Exception as e:
  149. errorInfo = traceback.format_exc()
  150. print(errorInfo)
  151. return response.json(424,repr(e))
  152. else:
  153. return response.json(0)
  154. else:
  155. return response.json(444)
  156. def findByTime_info(self, request_dict, userID,response):
  157. startTime = request_dict.get('startTime')
  158. endTime = request_dict.get('endTime')
  159. page = int(request_dict.get('page', None))
  160. line = int(request_dict.get('line', None))
  161. if not startTime or not endTime or not page or not line:
  162. return response.json(444,'startTime, endTime, page, line')
  163. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  164. if not len(uid_list):
  165. return response.json(0, {'datas': [], 'count': 0})
  166. qs = Equipment_Info.objects.filter(userID_id=userID,
  167. eventTime__range=(startTime, endTime)).order_by('-id')
  168. if qs.exists():
  169. count = qs.count()
  170. res = qs[(page - 1) * line:page * line]
  171. send_json = CommonService.qs_to_dict(res)
  172. send_json['count'] = count
  173. return response.json(0, send_json)
  174. def query_all_info(self, request_dict, userID,response):
  175. page = int(request_dict.get('page', None))
  176. line = int(request_dict.get('line', None))
  177. userID_id = request_dict.get('userID_id', None)
  178. if not page or not line:
  179. return response.json(444,'page,line')
  180. check_perm = ModelService.check_permission(userID=userID, permID=30)
  181. if not check_perm:
  182. return response.json(404)
  183. uid_list = Device_Info.objects.filter(userID_id=userID_id).values_list('UID', flat=True)
  184. if not len(uid_list):
  185. return response.json(0, {'datas': [], 'count': 0})
  186. qs = Equipment_Info.objects.filter(userID_id=userID_id).order_by('-id')
  187. if not qs.exists():
  188. return response.json(0, {'datas': [], 'count': 0})
  189. count = qs.count()
  190. res = qs[(page - 1) * line:page * line]
  191. send_json = CommonService.qs_to_dict(res)
  192. send_json['count'] = count
  193. return response.json(0, send_json)
  194. def delete_by_admin(self, request_dict, userID,response):
  195. id_list = request_dict.getlist('id', None)
  196. if not len(id_list):
  197. return response.json(444,'id is None or not list')
  198. check_perm = ModelService.check_permission(userID=userID, permID=10)
  199. if check_perm is True:
  200. try:
  201. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  202. except Exception as e:
  203. errorInfo = traceback.format_exc()
  204. print(errorInfo)
  205. return response.json(424, {'details': repr(e)})
  206. else:
  207. return response.json(0, {'delete_count': is_delete[0]})
  208. else:
  209. return response.json(404)
  210. class EquipmentInfo(View):
  211. @method_decorator(csrf_exempt)
  212. def dispatch(self, *args, **kwargs):
  213. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  214. def get(self, request, *args, **kwargs):
  215. request.encoding = 'utf-8'
  216. return self.validation(request_dict=request.GET)
  217. def post(self, request, *args, **kwargs):
  218. request.encoding = 'utf-8'
  219. return self.validation(request_dict=request.POST)
  220. def validation(self, request_dict, *args, **kwargs):
  221. token = request_dict.get('token', None)
  222. response = ResponseObject()
  223. tko = TokenObject(token)
  224. tko.valid()
  225. if tko.code != 0:
  226. return response.json(tko.code)
  227. userID = tko.userID
  228. operation = request_dict.get('operation', None)
  229. if not userID:
  230. return response.json(444, 'operation')
  231. if operation == 'query':
  232. return self.query_info(request_dict, userID, response)
  233. elif operation == 'add':
  234. return self.add_info(request_dict, userID, response)
  235. elif operation == 'update':
  236. return self.update_info(request_dict, userID, response)
  237. elif operation == 'delete':
  238. return self.delete_info(request_dict, userID, response)
  239. elif operation == 'findByTime':
  240. return self.findByTime_info(request_dict, userID, response)
  241. if operation == 'queryByAdmin':
  242. return self.query_all_info(request_dict, userID, response)
  243. if operation == 'deleteByAdmin':
  244. return self.delete_by_admin(request_dict, userID, response)
  245. else:
  246. return response.json(444, 'operation')
  247. def add_info(self, request_dict, userID,response):
  248. devUid = request_dict.get('devUid', None)
  249. Channel = request_dict.get('Channel', None)
  250. eventType = request_dict.get('eventType', None)
  251. eventTime = request_dict.get('eventTime', None)
  252. receiveTime = request_dict.get('receiveTime', None)
  253. alarm = request_dict.get('alarm', None)
  254. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  255. return response.json(444,'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  256. own_device = ModelService.check_own_device(userID, UID=devUid)
  257. if own_device is not True:
  258. return response.json(14)
  259. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  260. if not device_info.exists():
  261. return response.json(14)
  262. try:
  263. equipment_info = Equipment_Info(
  264. userID=Device_User.objects.get(userID=userID),
  265. devUid=devUid,
  266. Channel=Channel,
  267. eventType=eventType,
  268. receiveTime=receiveTime,
  269. eventTime=eventTime,
  270. viewPwd=device_info[0].View_Password,
  271. devNickName=device_info[0].NickName,
  272. viewAccont=device_info[0].View_Account,
  273. alarm=alarm
  274. )
  275. equipment_info.save()
  276. except Exception:
  277. errorInfo = traceback.format_exc()
  278. print(errorInfo)
  279. return response.json(500, errorInfo)
  280. else:
  281. if equipment_info.id:
  282. return response.json(0, {'infoID': equipment_info.id,
  283. 'devUid': devUid,
  284. 'Channel': Channel,
  285. 'alarm': alarm,
  286. 'eventType': eventType,
  287. 'eventTime': eventTime,
  288. 'receiveTime': receiveTime,
  289. 'viewPwd': equipment_info.viewPwd,
  290. 'devNickName': equipment_info.devNickName,
  291. 'viewAccont': equipment_info.viewAccont})
  292. else:
  293. return response.json(500)
  294. def query_info(self, request_dict, userID,response):
  295. page = int(request_dict.get('page', None))
  296. line = int(request_dict.get('line', None))
  297. if not page or not line:
  298. return response.json(444,'page,line')
  299. qs = Equipment_Info.objects.filter(userID_id=userID)
  300. uid = request_dict.get('uid', None)
  301. if uid:
  302. qs.filter(uid=uid)
  303. if not qs.exists():
  304. return response.json(0, {'datas': [], 'count': 0})
  305. count = qs.count()
  306. res = qs[(page - 1) * line:page * line]
  307. send_json = CommonService.qs_to_dict(res)
  308. send_json['count'] = count
  309. return response.json(0, send_json)
  310. def update_info(self, request_dict, userID, response):
  311. id_list = request_dict.getlist('id[]', None)
  312. if id_list is None or len(id_list) < 1:
  313. id_list = request_dict.getlist('id', None)
  314. param_flag = CommonService.get_param_flag(data=[id_list])
  315. if param_flag is True:
  316. count = 0
  317. for id in id_list:
  318. try:
  319. eq = Equipment_Info.objects.filter(id=int(id))
  320. if eq.exists():
  321. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  322. if own_dev is True:
  323. count += 1
  324. eq.update(status=1)
  325. except Exception as e:
  326. print(repr(e))
  327. return response.json(0,{'update_success': count})
  328. else:
  329. return response.json(444)
  330. def delete_info(self, request_dict, userID, response):
  331. id_list = request_dict.getlist('id[]', None)
  332. if id_list is None or len(id_list) < 1:
  333. id_list = request_dict.getlist('id', None)
  334. param_flag = CommonService.get_param_flag(data=[id_list])
  335. if param_flag is True:
  336. try:
  337. for id in id_list:
  338. eq = Equipment_Info.objects.filter(id=id)
  339. if eq.exists():
  340. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  341. if own_dev is True:
  342. eq.delete()
  343. except Exception as e:
  344. errorInfo = traceback.format_exc()
  345. print(errorInfo)
  346. return response.json(424,repr(e))
  347. else:
  348. return response.json(0)
  349. else:
  350. return response.json(444)
  351. def findByTime_info(self, request_dict, userID,response):
  352. startTime = request_dict.get('startTime')
  353. endTime = request_dict.get('endTime')
  354. page = int(request_dict.get('page', None))
  355. line = int(request_dict.get('line', None))
  356. if not startTime or not endTime or not page or not line:
  357. return response.json(444,'startTime, endTime, page, line')
  358. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  359. if not len(uid_list):
  360. return response.json(0, {'datas': [], 'count': 0})
  361. qs = Equipment_Info.objects.filter(userID_id=userID,
  362. eventTime__range=(startTime, endTime)).order_by('-id')
  363. if qs.exists():
  364. count = qs.count()
  365. res = qs[(page - 1) * line:page * line]
  366. send_json = CommonService.qs_to_dict(res)
  367. send_json['count'] = count
  368. return response.json(0, send_json)
  369. def query_all_info(self, request_dict, userID,response):
  370. page = int(request_dict.get('page', None))
  371. line = int(request_dict.get('line', None))
  372. userID_id = request_dict.get('userID_id', None)
  373. if not page or not line:
  374. return response.json(444,'page,line')
  375. check_perm = ModelService.check_permission(userID=userID, permID=30)
  376. if not check_perm:
  377. return response.json(404)
  378. uid_list = Device_Info.objects.filter(userID_id=userID_id).values_list('UID', flat=True)
  379. if not len(uid_list):
  380. return response.json(0, {'datas': [], 'count': 0})
  381. qs = Equipment_Info.objects.filter(userID_id=userID_id).order_by('-id')
  382. if not qs.exists():
  383. return response.json(0, {'datas': [], 'count': 0})
  384. count = qs.count()
  385. res = qs[(page - 1) * line:page * line]
  386. send_json = CommonService.qs_to_dict(res)
  387. send_json['count'] = count
  388. return response.json(0, send_json)
  389. def delete_by_admin(self, request_dict, userID,response):
  390. id_list = request_dict.getlist('id', None)
  391. if not len(id_list):
  392. return response.json(444,'id is None or not list')
  393. check_perm = ModelService.check_permission(userID=userID, permID=10)
  394. if check_perm is True:
  395. try:
  396. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  397. except Exception as e:
  398. errorInfo = traceback.format_exc()
  399. print(errorInfo)
  400. return response.json(424, {'details': repr(e)})
  401. else:
  402. return response.json(0, {'delete_count': is_delete[0]})
  403. else:
  404. return response.json(404)