EquipmentInfo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import time
  2. import traceback
  3. import oss2
  4. from django.utils.decorators import method_decorator
  5. from django.views.decorators.csrf import csrf_exempt
  6. from django.views.generic import TemplateView
  7. from django.views.generic.base import View
  8. from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
  9. from Ansjer.config import SERVER_DOMAIN
  10. from Model.models import Device_Info, VodHlsModel, Equipment_Info
  11. from Model.models import Device_User
  12. from Object.ResponseObject import ResponseObject
  13. from Object.TokenObject import TokenObject
  14. from Object.UidTokenObject import UidTokenObject
  15. from Service.CommonService import CommonService
  16. from Service.ModelService import ModelService
  17. '''
  18. 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
  19. http://test.dvema.com/equipment/info?token=test&operation=query&page=1&line=10
  20. http://192.168.136.45:8077/equipment/info?token=test&operation=delete&devUid=UKPAH63V23U4ZHEB111A&id=5&id=6&id=7
  21. http://192.168.136.40:8077/equipment/info?token=test&operation=update&devUid=UKPAH63V23U4ZHEB111A&id=3&id=4
  22. http://192.168.136.45:8077/equipment/info?token=test&operation=queryByAdmin&page=1&line=10&userID_id=151547867345163613800138001
  23. http://192.168.136.45:8077/equipment/info?token=test&operation=deleteByAdmin&id=5&id=6&id=7
  24. '''
  25. class EquipmentInfo(View):
  26. @method_decorator(csrf_exempt)
  27. def dispatch(self, *args, **kwargs):
  28. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  29. def get(self, request, *args, **kwargs):
  30. request.encoding = 'utf-8'
  31. return self.validation(request_dict=request.GET)
  32. def post(self, request, *args, **kwargs):
  33. request.encoding = 'utf-8'
  34. return self.validation(request_dict=request.POST)
  35. def validation(self, request_dict, *args, **kwargs):
  36. token = request_dict.get('token', None)
  37. response = ResponseObject()
  38. tko = TokenObject(token)
  39. if tko.code != 0:
  40. return response.json(tko.code)
  41. userID = tko.userID
  42. operation = request_dict.get('operation', None)
  43. if not userID:
  44. return response.json(309)
  45. if operation == 'query':
  46. return self.query_info(request_dict, userID, response)
  47. elif operation == 'add':
  48. return self.add_info(request_dict, userID, response)
  49. elif operation == 'update':
  50. return self.update_info(request_dict, userID, response)
  51. elif operation == 'delete':
  52. return self.delete_info(request_dict, userID, response)
  53. elif operation == 'findByTime':
  54. return self.findByTime_info(request_dict, userID, response)
  55. if operation == 'queryByAdmin':
  56. return self.query_all_info(request_dict, userID, response)
  57. if operation == 'deleteByAdmin':
  58. return self.delete_by_admin(request_dict, userID, response)
  59. else:
  60. return response.json(444, {"parm": "operation"})
  61. def add_info(self, request_dict, userID, response):
  62. devUid = request_dict.get('devUid', None)
  63. Channel = request_dict.get('Channel', None)
  64. eventType = request_dict.get('eventType', None)
  65. eventTime = request_dict.get('eventTime', None)
  66. receiveTime = request_dict.get('receiveTime', None)
  67. alarm = request_dict.get('alarm', None)
  68. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  69. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  70. own_device = ModelService.check_own_device(userID, UID=devUid)
  71. if own_device is not True:
  72. return response.json(14)
  73. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  74. if not device_info.exists():
  75. return response.json(14)
  76. try:
  77. nowTime = int(time.time())
  78. equipment_info = Equipment_Info(
  79. userID=Device_User.objects.get(userID=userID),
  80. devUid=devUid,
  81. Channel=Channel,
  82. eventType=eventType,
  83. receiveTime=receiveTime,
  84. eventTime=eventTime,
  85. devNickName=device_info[0].NickName,
  86. addTime=nowTime,
  87. alarm=alarm)
  88. equipment_info.save()
  89. except Exception:
  90. errorInfo = traceback.format_exc()
  91. print(errorInfo)
  92. return response.json(500, errorInfo)
  93. else:
  94. if equipment_info.id:
  95. return response.json(0, {'infoID': equipment_info.id,
  96. 'devUid': devUid,
  97. 'Channel': Channel,
  98. 'alarm': alarm,
  99. 'eventType': eventType,
  100. 'eventTime': eventTime,
  101. 'receiveTime': receiveTime,
  102. 'devNickName': equipment_info.devNickName})
  103. else:
  104. return response.json(500)
  105. def query_info(self, request_dict, userID, response):
  106. page = int(request_dict.get('page', None))
  107. line = int(request_dict.get('line', None))
  108. if not page or not line:
  109. return response.json(444, 'page,line')
  110. qs = Equipment_Info.objects.filter(userID_id=userID)
  111. uid = request_dict.get('uid', None)
  112. if uid:
  113. qs = qs.filter(devUid=uid)
  114. dvqs = Device_Info.objects.filter(UID=uid).values('Type', 'NickName')
  115. uid_type_dict = {uid: {'type': dvqs[0]['Type'], 'NickName': dvqs[0]['NickName']}}
  116. else:
  117. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  118. uid_type_dict = {}
  119. for dv in dvqs:
  120. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  121. if not qs.exists():
  122. return response.json(0, {'datas': [], 'count': 0})
  123. count = qs.count()
  124. res = qs[(page - 1) * line:page * line]
  125. send_json = CommonService.qs_to_dict(res)
  126. res = []
  127. for p in send_json['datas']:
  128. p['fields']['viewAccont'] = ''
  129. p['fields']['viewPwd'] = ''
  130. devUid = p['fields']['devUid']
  131. if devUid in uid_type_dict.keys():
  132. p['fields']['uid_type'] = uid_type_dict[devUid]['type']
  133. p['fields']['devNickName'] = uid_type_dict[devUid]['NickName']
  134. else:
  135. p['uid_type'] = ''
  136. res.append(p)
  137. return response.json(0, {'datas': res, 'count': count})
  138. def update_info(self, request_dict, userID, response):
  139. id_list = request_dict.getlist('id[]', None)
  140. if id_list is None or len(id_list) < 1:
  141. id_list = request_dict.getlist('id', None)
  142. param_flag = CommonService.get_param_flag(data=[id_list])
  143. if param_flag is True:
  144. count = 0
  145. for id in id_list:
  146. try:
  147. eq = Equipment_Info.objects.filter(id=int(id))
  148. if eq.exists():
  149. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  150. if own_dev is True:
  151. count += 1
  152. eq.update(status=1)
  153. except Exception as e:
  154. print(repr(e))
  155. return response.json(0, {'update_success': count})
  156. else:
  157. return response.json(444)
  158. def delete_info(self, request_dict, userID, response):
  159. id_list = request_dict.getlist('id[]', None)
  160. if id_list is None or len(id_list) < 1:
  161. id_list = request_dict.getlist('id', None)
  162. param_flag = CommonService.get_param_flag(data=[id_list])
  163. if param_flag is True:
  164. try:
  165. for id in id_list:
  166. eq = Equipment_Info.objects.filter(id=id)
  167. if eq.exists():
  168. own_dev = ModelService.check_own_device(userID, eq[0].devUid)
  169. if own_dev is True:
  170. eq.delete()
  171. except Exception as e:
  172. errorInfo = traceback.format_exc()
  173. print(errorInfo)
  174. return response.json(424, repr(e))
  175. else:
  176. return response.json(0)
  177. else:
  178. return response.json(444)
  179. def findByTime_info(self, request_dict, userID, response):
  180. startTime = request_dict.get('startTime')
  181. endTime = request_dict.get('endTime')
  182. page = int(request_dict.get('page', None))
  183. line = int(request_dict.get('line', None))
  184. if not startTime or not endTime or not page or not line:
  185. return response.json(444, 'startTime, endTime, page, line')
  186. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  187. if not len(uid_list):
  188. return response.json(0, {'datas': [], 'count': 0})
  189. qs = Equipment_Info.objects.filter(userID_id=userID,
  190. eventTime__range=(startTime, endTime)).order_by('-id')
  191. if qs.exists():
  192. count = qs.count()
  193. res = qs[(page - 1) * line:page * line]
  194. send_json = CommonService.qs_to_dict(res)
  195. send_json['count'] = count
  196. return response.json(0, send_json)
  197. # 管理员查询指定用户的推送信息接口
  198. def query_all_info(self, request_dict, userID, response):
  199. page = int(request_dict.get('page', None))
  200. line = int(request_dict.get('line', None))
  201. username = request_dict.get('username', None)
  202. uid = request_dict.get('uid', None)
  203. devNickName = request_dict.get('devNickName', None)
  204. Channel = request_dict.get('Channel', None)
  205. if not page or not line:
  206. return response.json(444, 'page,line')
  207. check_perm = ModelService.check_perm(userID=userID, permID=30)
  208. if not check_perm:
  209. return response.json(404)
  210. if not username and not uid and not devNickName and not Channel:
  211. qs = Equipment_Info.objects.all().order_by('-id')
  212. elif username:
  213. user = Device_User.objects.filter(username=username)
  214. users = CommonService.qs_to_dict(user)
  215. # 从字典类型中找到对应的pk值,即是userID
  216. user_id = users.get('datas')[0].get('pk')
  217. if not user.exists():
  218. return response.json(104)
  219. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('UID', flat=True)
  220. if not len(uid_list):
  221. return response.json(0, {'datas': [], 'count': 0})
  222. qs = Equipment_Info.objects.filter(userID_id=user_id).order_by('-id')
  223. elif uid:
  224. qs = Equipment_Info.objects.filter(devUid=uid).order_by('-id')
  225. elif devNickName:
  226. qs = Equipment_Info.objects.filter(devNickName=devNickName).order_by('-id')
  227. elif Channel:
  228. qs = Equipment_Info.objects.filter(Channel=Channel).order_by('-id')
  229. if not qs.exists():
  230. return response.json(0, {'datas': [], 'count': 0})
  231. count = qs.count()
  232. res = qs[(page - 1) * line:page * line]
  233. send_json = CommonService.qs_to_dict(res)
  234. send_json['count'] = count
  235. return response.json(0, send_json)
  236. # 管理员的删除推送信息的接口
  237. def delete_by_admin(self, request_dict, userID, response):
  238. id_list = request_dict.getlist('id', None)
  239. if not len(id_list):
  240. return response.json(444, 'id is None or not list')
  241. check_perm = ModelService.check_perm(userID=userID, permID=10)
  242. if check_perm is True:
  243. try:
  244. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  245. except Exception as e:
  246. errorInfo = traceback.format_exc()
  247. print(errorInfo)
  248. return response.json(424, {'details': repr(e)})
  249. else:
  250. return response.json(0, {'delete_count': is_delete[0]})
  251. else:
  252. return response.json(404)
  253. '''
  254. use information_schema;
  255. select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='Ansjer81';
  256. '''
  257. # 按季度删除访问日志
  258. def deleteExpireEquipmentInfo(request):
  259. response = ResponseObject()
  260. i = int(request.GET.get('i', 5))
  261. import time
  262. nowTime = int(time.time())
  263. for i in range(i):
  264. ei = Equipment_Info.objects.filter(addTime__lte=str(nowTime - 3600 * 24 * 7))[0:10000]
  265. id_list = ei.values_list("id", flat=True)
  266. print(id_list)
  267. Equipment_Info.objects.filter(id__in=list(id_list)).delete()
  268. return response.json(0)