EquipmentInfo.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import traceback
  2. from django.utils.decorators import method_decorator
  3. from django.views.decorators.csrf import csrf_exempt
  4. from django.views.generic import TemplateView
  5. from django.views.generic.base import View
  6. from Ansjer.config import SERVER_DOMAIN
  7. from Model.models import Equipment_Info, Device_Info, Device_User
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Object.UidTokenObject import UidTokenObject
  11. from Service.CommonService import CommonService
  12. from Service.ModelService import ModelService
  13. '''
  14. http://192.168.136.45:8077/equipment/info?token=test&operation=add&devUid=2N1K3LE78TYJ38CE111A&Channel=0&eventType=1&eventTime=1234567890&operation=add&alarm=21342134&receiveTime=1234567891234567
  15. http://192.168.136.45:8077/equipment/info?token=test&devUid=2N1K3LE78TYJ38CE111A&Channel=0&eventType=1&eventTime=1234567890&operation=add&alarm=21342134&receiveTime=1234567891234567
  16. http://test.dvema.com/equipment/info?token=test&operation=query&page=1&line=10
  17. http://192.168.136.45:8077/equipment/info?token=test&operation=delete&devUid=UKPAH63V23U4ZHEB111A&id=5&id=6&id=7
  18. http://192.168.136.40:8077/equipment/info?token=test&operation=update&devUid=UKPAH63V23U4ZHEB111A&id=3&id=4
  19. http://192.168.136.45:8077/equipment/info?token=test&operation=queryByAdmin&page=1&line=10&userID_id=151547867345163613800138001
  20. http://192.168.136.45:8077/equipment/info?token=test&operation=deleteByAdmin&id=5&id=6&id=7
  21. '''
  22. class EquipmentInfo(View):
  23. @method_decorator(csrf_exempt)
  24. def dispatch(self, *args, **kwargs):
  25. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  26. def get(self, request, *args, **kwargs):
  27. request.encoding = 'utf-8'
  28. return self.validation(request_dict=request.GET)
  29. def post(self, request, *args, **kwargs):
  30. request.encoding = 'utf-8'
  31. return self.validation(request_dict=request.POST)
  32. def validation(self, request_dict, *args, **kwargs):
  33. token = request_dict.get('token', None)
  34. response = ResponseObject()
  35. tko = TokenObject(token)
  36. if tko.code != 0:
  37. return response.json(tko.code)
  38. userID = tko.userID
  39. operation = request_dict.get('operation', None)
  40. if not userID:
  41. return response.json(309)
  42. if operation == 'query':
  43. return self.query_info(request_dict, userID, response)
  44. elif operation == 'add':
  45. return self.add_info(request_dict, userID, response)
  46. elif operation == 'update':
  47. return self.update_info(request_dict, userID, response)
  48. elif operation == 'delete':
  49. return self.delete_info(request_dict, userID, response)
  50. elif operation == 'findByTime':
  51. return self.findByTime_info(request_dict, userID, response)
  52. if operation == 'queryByAdmin':
  53. return self.query_all_info(request_dict, userID, response)
  54. if operation == 'deleteByAdmin':
  55. return self.delete_by_admin(request_dict, userID, response)
  56. else:
  57. return response.json(444, {"parm": "operation"})
  58. def add_info(self, request_dict, userID, response):
  59. devUid = request_dict.get('devUid', None)
  60. Channel = request_dict.get('Channel', None)
  61. eventType = request_dict.get('eventType', None)
  62. eventTime = request_dict.get('eventTime', None)
  63. receiveTime = request_dict.get('receiveTime', None)
  64. alarm = request_dict.get('alarm', None)
  65. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  66. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  67. own_device = ModelService.check_own_device(userID, UID=devUid)
  68. if own_device is not True:
  69. return response.json(14)
  70. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  71. if not device_info.exists():
  72. return response.json(14)
  73. try:
  74. equipment_info = Equipment_Info(
  75. userID=Device_User.objects.get(userID=userID),
  76. devUid=devUid,
  77. Channel=Channel,
  78. eventType=eventType,
  79. receiveTime=receiveTime,
  80. eventTime=eventTime,
  81. devNickName=device_info[0].NickName,
  82. alarm=alarm)
  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. 'devNickName': equipment_info.devNickName})
  98. else:
  99. return response.json(500)
  100. def query_info(self, request_dict, userID, response):
  101. page = int(request_dict.get('page', None))
  102. line = int(request_dict.get('line', None))
  103. if not page or not line:
  104. return response.json(444, 'page,line')
  105. qs = Equipment_Info.objects.filter(userID_id=userID)
  106. uid = request_dict.get('uid', None)
  107. if uid:
  108. qs.filter(devUid=uid)
  109. if not qs.exists():
  110. return response.json(0, {'datas': [], 'count': 0})
  111. count = qs.count()
  112. res = qs[(page - 1) * line:page * line]
  113. send_json = CommonService.qs_to_dict(res)
  114. res = []
  115. for p in send_json['datas']:
  116. p['fields']['viewAccont'] = ''
  117. p['fields']['viewPwd'] = ''
  118. res.append(p)
  119. return response.json(0, {'datas': res, 'count': count})
  120. # return response.json(0, send_json)
  121. def update_info(self, request_dict, userID, response):
  122. id_list = request_dict.getlist('id[]', None)
  123. if id_list is None or len(id_list) < 1:
  124. id_list = request_dict.getlist('id', None)
  125. param_flag = CommonService.get_param_flag(data=[id_list])
  126. if param_flag is True:
  127. count = 0
  128. for id in id_list:
  129. try:
  130. eq = Equipment_Info.objects.filter(id=int(id))
  131. if eq.exists():
  132. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  133. if own_dev is True:
  134. count += 1
  135. eq.update(status=1)
  136. except Exception as e:
  137. print(repr(e))
  138. return response.json(0, {'update_success': count})
  139. else:
  140. return response.json(444)
  141. def delete_info(self, request_dict, userID, response):
  142. id_list = request_dict.getlist('id[]', None)
  143. if id_list is None or len(id_list) < 1:
  144. id_list = request_dict.getlist('id', None)
  145. param_flag = CommonService.get_param_flag(data=[id_list])
  146. if param_flag is True:
  147. try:
  148. for id in id_list:
  149. eq = Equipment_Info.objects.filter(id=id)
  150. if eq.exists():
  151. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  152. if own_dev is True:
  153. eq.delete()
  154. except Exception as e:
  155. errorInfo = traceback.format_exc()
  156. print(errorInfo)
  157. return response.json(424, repr(e))
  158. else:
  159. return response.json(0)
  160. else:
  161. return response.json(444)
  162. def findByTime_info(self, request_dict, userID, response):
  163. startTime = request_dict.get('startTime')
  164. endTime = request_dict.get('endTime')
  165. page = int(request_dict.get('page', None))
  166. line = int(request_dict.get('line', None))
  167. if not startTime or not endTime or not page or not line:
  168. return response.json(444, 'startTime, endTime, page, line')
  169. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  170. if not len(uid_list):
  171. return response.json(0, {'datas': [], 'count': 0})
  172. qs = Equipment_Info.objects.filter(userID_id=userID,
  173. eventTime__range=(startTime, endTime)).order_by('-id')
  174. if qs.exists():
  175. count = qs.count()
  176. res = qs[(page - 1) * line:page * line]
  177. send_json = CommonService.qs_to_dict(res)
  178. send_json['count'] = count
  179. return response.json(0, send_json)
  180. # 管理员查询指定用户的推送信息接口
  181. def query_all_info(self, request_dict, userID, response):
  182. page = int(request_dict.get('page', None))
  183. line = int(request_dict.get('line', None))
  184. username = request_dict.get('username', None)
  185. userID_id = request_dict.get('userID_id', None)
  186. if not page or not line:
  187. return response.json(444, 'page,line')
  188. user = Device_User.objects.filter(username=username)
  189. if not user.exists():
  190. return response.json(104)
  191. users = CommonService.qs_to_dict(user)
  192. # 从字典类型中找到对应的pk值,即是userID
  193. user_id = users.get('datas')[0].get('pk')
  194. check_perm = ModelService.check_perm(userID=userID, permID=30)
  195. if not check_perm:
  196. return response.json(404)
  197. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('UID', flat=True)
  198. uid_list = Device_Info.objects.filter(userID_id=userID_id).values_list('UID', flat=True)
  199. if not len(uid_list):
  200. return response.json(0, {'datas': [], 'count': 0})
  201. qs = Equipment_Info.objects.filter(userID_id=user_id).order_by('-id')
  202. qs = Equipment_Info.objects.filter(userID_id=userID_id).order_by('-id')
  203. if not qs.exists():
  204. return response.json(0, {'datas': [], 'count': 0})
  205. count = qs.count()
  206. res = qs[(page - 1) * line:page * line]
  207. send_json = CommonService.qs_to_dict(res)
  208. send_json['count'] = count
  209. return response.json(0, send_json)
  210. # 管理员的删除推送信息的接口
  211. def delete_by_admin(self, request_dict, userID, response):
  212. id_list = request_dict.getlist('id', None)
  213. if not len(id_list):
  214. return response.json(444, 'id is None or not list')
  215. check_perm = ModelService.check_perm(userID=userID, permID=10)
  216. if check_perm is True:
  217. try:
  218. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  219. except Exception as e:
  220. errorInfo = traceback.format_exc()
  221. print(errorInfo)
  222. return response.json(424, {'details': repr(e)})
  223. else:
  224. return response.json(0, {'delete_count': is_delete[0]})
  225. else:
  226. return response.json(404)
  227. # http://192.168.136.40:8077/detect/add?UID=FTSLL8HM437Z38WU111A&Channel=0&eventType=1&eventTime=1234567890
  228. # 设备推送信息添加接口
  229. class AddDetectionView(TemplateView):
  230. @method_decorator(csrf_exempt)
  231. def dispatch(self, *args, **kwargs):
  232. return super(AddDetectionView, self).dispatch(*args, **kwargs)
  233. def get(self, request, *args, **kwargs):
  234. request.encoding = 'utf-8'
  235. return self.validation(request_dict=request.GET)
  236. def post(self, request, *args, **kwargs):
  237. request.encoding = 'utf-8'
  238. return self.validation(request_dict=request.POST)
  239. def validation(self, request_dict, *args, **kwargs):
  240. response = ResponseObject()
  241. # 设备添加token
  242. uidToken = request_dict.get('uidToken', None)
  243. Channel = request_dict.get('Channel', None)
  244. eventType = request_dict.get('eventType', None)
  245. eventTime = request_dict.get('eventTime', None)
  246. utko = UidTokenObject(uidToken)
  247. if utko.flag is False:
  248. return response.json(444, 'tidToken is wrong')
  249. UID = utko.UID
  250. if not Channel or not eventType or not eventTime:
  251. return response.json(444, 'uidToken,Channel,eventType,eventTime')
  252. dv_qs = Device_Info.objects.filter(UID=UID, iSNotification=True).values("userID_id", "NickName")
  253. if not dv_qs.exists():
  254. return response.json(10, 'device not detect')
  255. qs_list = []
  256. for dv in dv_qs:
  257. add_data = {
  258. 'userID_id': dv["userID_id"],
  259. 'devUid': UID,
  260. 'Channel': Channel,
  261. 'eventType': eventType,
  262. 'eventTime': eventTime,
  263. 'devNickName': dv["NickName"],
  264. }
  265. qs_list.append(Equipment_Info(**add_data))
  266. if qs_list:
  267. print(1)
  268. Equipment_Info.objects.bulk_create(qs_list)
  269. print(2)
  270. return response.json(0)
  271. else:
  272. return response.json(10, '无设备开启')
  273. # http://192.168.136.40:8077/detect/status?token=local&id=138001380001543918745881545&iSNotification=0
  274. # 设备推送开关
  275. class DetectStatusView(View):
  276. @method_decorator(csrf_exempt)
  277. def dispatch(self, *args, **kwargs):
  278. return super(DetectStatusView, self).dispatch(*args, **kwargs)
  279. def get(self, request, *args, **kwargs):
  280. request.encoding = 'utf-8'
  281. return self.validation(request_dict=request.GET)
  282. def post(self, request, *args, **kwargs):
  283. request.encoding = 'utf-8'
  284. return self.validation(request_dict=request.POST)
  285. def validation(self, request_dict, *args, **kwargs):
  286. token = request_dict.get('token', None)
  287. id = request_dict.get('id', None)
  288. iSNotification = request_dict.get('iSNotification', None)
  289. response = ResponseObject()
  290. if token is None:
  291. return response.json(309)
  292. tko = TokenObject(token)
  293. response.lang = tko.lang
  294. if tko.code != 0:
  295. return response.json(tko.code)
  296. userID = tko.userID
  297. return self.do_update(userID, id, iSNotification, response)
  298. def do_update(self, userID, id, iSNotification, response):
  299. qs = Device_Info.objects.filter(id=id, userID_id=userID)
  300. if qs.exists():
  301. qs.update(iSNotification=iSNotification)
  302. data = {'uid': qs[0].UID}
  303. utko = UidTokenObject()
  304. uid_token = utko.generate(data)
  305. return response.json(0, {'uid_token': uid_token,
  306. 'push_url': '{SERVER_DOMAIN}detect/add'.format(SERVER_DOMAIN=SERVER_DOMAIN)})
  307. else:
  308. return response.json(173)
  309. # 按季度删除访问日志
  310. def deleteExpireEquipmentInfo(request):
  311. response = ResponseObject()
  312. import time
  313. nowTime = int(time.time())
  314. ei = Equipment_Info.objects.filter(eventTime__lte=str(nowTime - 3600 * 24 * 90))[0:1000]
  315. id_list = ei.values_list("id",flat=True)
  316. print(id_list)
  317. Equipment_Info.objects.filter(id__in=list(id_list)).delete()
  318. return response.json(0)