EquipmentInfo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import time
  2. import traceback
  3. from django.utils.decorators import method_decorator
  4. from django.views.decorators.csrf import csrf_exempt
  5. from django.views.generic.base import View
  6. from Model.models import Device_Info, Equipment_Info
  7. from Model.models import Device_User
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Object.utils import LocalDateTimeUtil
  11. from Service.CommonService import CommonService
  12. from Service.EquipmentInfoService import EquipmentInfoService
  13. from Service.ModelService import ModelService
  14. '''
  15. 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
  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. nowTime = int(time.time())
  75. equipment_info = Equipment_Info(
  76. userID_id=userID,
  77. devUid=devUid,
  78. Channel=Channel,
  79. eventType=eventType,
  80. receiveTime=receiveTime,
  81. eventTime=eventTime,
  82. devNickName=device_info[0].NickName,
  83. addTime=nowTime,
  84. alarm=alarm)
  85. equipment_info.save()
  86. except Exception:
  87. errorInfo = traceback.format_exc()
  88. print(errorInfo)
  89. return response.json(500, errorInfo)
  90. else:
  91. if equipment_info.id:
  92. return response.json(0, {'infoID': equipment_info.id,
  93. 'devUid': devUid,
  94. 'Channel': Channel,
  95. 'alarm': alarm,
  96. 'eventType': eventType,
  97. 'eventTime': eventTime,
  98. 'receiveTime': receiveTime,
  99. 'devNickName': equipment_info.devNickName})
  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. qs = Equipment_Info.objects.filter(userID_id=userID)
  108. uid = request_dict.get('uid', None)
  109. if uid:
  110. qs = qs.filter(devUid=uid)
  111. dvqs = Device_Info.objects.filter(UID=uid).values('Type', 'NickName')
  112. uid_type_dict = {uid: {'type': dvqs[0]['Type'], 'NickName': dvqs[0]['NickName']}}
  113. else:
  114. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  115. uid_type_dict = {}
  116. for dv in dvqs:
  117. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  118. if not qs.exists():
  119. return response.json(0, {'datas': [], 'count': 0})
  120. count = qs.count()
  121. res = qs[(page - 1) * line:page * line]
  122. send_json = CommonService.qs_to_dict(res)
  123. res = []
  124. for p in send_json['datas']:
  125. p['fields']['viewAccont'] = ''
  126. p['fields']['viewPwd'] = ''
  127. devUid = p['fields']['devUid']
  128. if devUid in uid_type_dict.keys():
  129. p['fields']['uid_type'] = uid_type_dict[devUid]['type']
  130. p['fields']['devNickName'] = uid_type_dict[devUid]['NickName']
  131. else:
  132. p['uid_type'] = ''
  133. res.append(p)
  134. return response.json(0, {'datas': res, 'count': count})
  135. def update_info(self, request_dict, userID, response):
  136. is_update_all = request_dict.get('is_update_all', 0)
  137. eventType = request_dict.get('eventType', None)
  138. if int(is_update_all) == 1:
  139. try:
  140. eq = Equipment_Info.objects.filter(userID_id=userID)
  141. if eventType:
  142. eq = eq.filter(eventType=int(eventType))
  143. is_update = eq.update(status=1)
  144. return response.json(0, {'update_count': is_update})
  145. except Exception as e:
  146. print(repr(e))
  147. return response.json(0, {'update_success': 'all'})
  148. else:
  149. id_list = request_dict.getlist('id[]', None)
  150. if id_list is None or len(id_list) < 1:
  151. id_list = request_dict.getlist('id', None)
  152. param_flag = CommonService.get_param_flag(data=[id_list])
  153. if param_flag is True:
  154. count = 0
  155. for id in id_list:
  156. try:
  157. eq = Equipment_Info.objects.filter(id=int(id))
  158. if eq.exists():
  159. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  160. if own_dev is True:
  161. count += 1
  162. eq.update(status=1)
  163. except Exception as e:
  164. print(repr(e))
  165. return response.json(0, {'update_success': count})
  166. else:
  167. return response.json(444)
  168. def delete_info(self, request_dict, userID, response):
  169. id_list = request_dict.getlist('id[]', None)
  170. if id_list is None or len(id_list) < 1:
  171. id_list = request_dict.getlist('id', None)
  172. param_flag = CommonService.get_param_flag(data=[id_list])
  173. if param_flag is True:
  174. try:
  175. for ei_id in id_list:
  176. event_date = '20{}'.format(ei_id[0:6])
  177. ei_id = ei_id[6:]
  178. val = LocalDateTimeUtil.format_date_to_week(event_date, '%Y%m%d')
  179. eq = EquipmentInfoService.get_equipment_info_model('', val)
  180. eq = eq.filter(id=ei_id)
  181. if eq.exists():
  182. own_dev = ModelService.check_own_device(userID, eq[0].device_uid)
  183. if own_dev is True:
  184. eq.delete()
  185. except Exception as e:
  186. errorInfo = traceback.format_exc()
  187. print(errorInfo)
  188. return response.json(424, repr(e))
  189. else:
  190. return response.json(0)
  191. else:
  192. return response.json(444)
  193. def findByTime_info(self, request_dict, userID, response):
  194. startTime = request_dict.get('startTime')
  195. endTime = request_dict.get('endTime')
  196. page = int(request_dict.get('page', None))
  197. line = int(request_dict.get('line', None))
  198. if not startTime or not endTime or not page or not line:
  199. return response.json(444, 'startTime, endTime, page, line')
  200. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  201. if not len(uid_list):
  202. return response.json(0, {'datas': [], 'count': 0})
  203. qs = Equipment_Info.objects.filter(userID_id=userID,
  204. eventTime__range=(startTime, endTime)).order_by('-id')
  205. if qs.exists():
  206. count = qs.count()
  207. res = qs[(page - 1) * line:page * line]
  208. send_json = CommonService.qs_to_dict(res)
  209. send_json['count'] = count
  210. return response.json(0, send_json)
  211. # 管理员查询指定用户的推送信息接口
  212. def query_all_info(self, request_dict, userID, response):
  213. page = int(request_dict.get('page', None))
  214. line = int(request_dict.get('line', None))
  215. username = request_dict.get('username', None)
  216. uid = request_dict.get('uid', None)
  217. devNickName = request_dict.get('devNickName', None)
  218. Channel = request_dict.get('Channel', None)
  219. if not page or not line:
  220. return response.json(444, 'page,line')
  221. check_perm = ModelService.check_perm(userID=userID, permID=30)
  222. if not check_perm:
  223. return response.json(404)
  224. if not username and not uid and not devNickName and not Channel:
  225. qs = Equipment_Info.objects.all().order_by('-id')
  226. elif username:
  227. user = Device_User.objects.filter(username=username)
  228. users = CommonService.qs_to_dict(user)
  229. # 从字典类型中找到对应的pk值,即是userID
  230. user_id = users.get('datas')[0].get('pk')
  231. if not user.exists():
  232. return response.json(104)
  233. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('UID', flat=True)
  234. if not len(uid_list):
  235. return response.json(0, {'datas': [], 'count': 0})
  236. qs = Equipment_Info.objects.filter(userID_id=user_id).order_by('-id')
  237. elif uid:
  238. qs = Equipment_Info.objects.filter(devUid=uid).order_by('-id')
  239. elif devNickName:
  240. qs = Equipment_Info.objects.filter(devNickName=devNickName).order_by('-id')
  241. elif Channel:
  242. qs = Equipment_Info.objects.filter(Channel=Channel).order_by('-id')
  243. if not qs.exists():
  244. return response.json(0, {'datas': [], 'count': 0})
  245. count = qs.count()
  246. res = qs[(page - 1) * line:page * line]
  247. send_json = CommonService.qs_to_dict(res)
  248. send_json['count'] = count
  249. return response.json(0, send_json)
  250. # 管理员的删除推送信息的接口
  251. def delete_by_admin(self, request_dict, userID, response):
  252. id_list = request_dict.getlist('id', None)
  253. if not len(id_list):
  254. return response.json(444, 'id is None or not list')
  255. check_perm = ModelService.check_perm(userID=userID, permID=10)
  256. if check_perm is True:
  257. try:
  258. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  259. except Exception as e:
  260. errorInfo = traceback.format_exc()
  261. print(errorInfo)
  262. return response.json(424, {'details': repr(e)})
  263. else:
  264. return response.json(0, {'delete_count': is_delete[0]})
  265. else:
  266. return response.json(404)
  267. '''
  268. use information_schema;
  269. select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='Ansjer81';
  270. '''
  271. # 按季度删除访问日志
  272. def deleteExpireEquipmentInfoById(request):
  273. response = ResponseObject()
  274. id = int(request.GET.get('id', None))
  275. if id is None:
  276. return response.json(0)
  277. Equipment_Info.objects.filter(id__lte=id).delete()
  278. return response.json(0)