EquipmentInfo.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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, UidPushModel
  7. from Model.models import Device_User
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. from Service.EquipmentInfoService import EquipmentInfoService
  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://test.dvema.com/equipment/info?token=test&operation=query&page=1&line=10
  16. http://192.168.136.45:8077/equipment/info?token=test&operation=delete&devUid=UKPAH63V23U4ZHEB111A&id=5&id=6&id=7
  17. http://192.168.136.40:8077/equipment/info?token=test&operation=update&devUid=UKPAH63V23U4ZHEB111A&id=3&id=4
  18. http://192.168.136.45:8077/equipment/info?token=test&operation=queryByAdmin&page=1&line=10&userID_id=151547867345163613800138001
  19. http://192.168.136.45:8077/equipment/info?token=test&operation=deleteByAdmin&id=5&id=6&id=7
  20. '''
  21. class EquipmentInfo(View):
  22. @method_decorator(csrf_exempt)
  23. def dispatch(self, *args, **kwargs):
  24. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  25. def get(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. return self.validation(request_dict=request.GET)
  28. def post(self, request, *args, **kwargs):
  29. request.encoding = 'utf-8'
  30. return self.validation(request_dict=request.POST)
  31. def validation(self, request_dict, *args, **kwargs):
  32. token = request_dict.get('token', None)
  33. response = ResponseObject()
  34. tko = TokenObject(token)
  35. if tko.code != 0:
  36. return response.json(tko.code)
  37. userID = tko.userID
  38. operation = request_dict.get('operation', None)
  39. if not userID:
  40. return response.json(309)
  41. if operation == 'query':
  42. return self.query_info(request_dict, userID, response)
  43. elif operation == 'add':
  44. return self.add_info(request_dict, userID, response)
  45. elif operation == 'update':
  46. return self.update_info(request_dict, userID, response)
  47. elif operation == 'update-answer-status':
  48. return self.update_answer_status(request_dict, userID, response)
  49. elif operation == 'delete':
  50. return self.delete_info(request_dict, userID, response)
  51. elif operation == 'findByTime':
  52. return self.findByTime_info(request_dict, userID, response)
  53. if operation == 'queryByAdmin':
  54. return self.query_all_info(request_dict, userID, response)
  55. if operation == 'deleteByAdmin':
  56. return self.delete_by_admin(request_dict, userID, response)
  57. else:
  58. return response.json(444, {"parm": "operation"})
  59. def add_info(self, request_dict, userID, response):
  60. devUid = request_dict.get('devUid', None)
  61. Channel = request_dict.get('Channel', None)
  62. eventType = request_dict.get('eventType', None)
  63. eventTime = request_dict.get('eventTime', None)
  64. receiveTime = request_dict.get('receiveTime', None)
  65. alarm = request_dict.get('alarm', None)
  66. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  67. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  68. own_device = ModelService.check_own_device(userID, UID=devUid)
  69. if own_device is not True:
  70. return response.json(14)
  71. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  72. if not device_info.exists():
  73. return response.json(14)
  74. try:
  75. nowTime = int(time.time())
  76. equipment_info = Equipment_Info(
  77. userID_id=userID,
  78. devUid=devUid,
  79. Channel=Channel,
  80. eventType=eventType,
  81. receiveTime=receiveTime,
  82. eventTime=eventTime,
  83. devNickName=device_info[0].NickName,
  84. addTime=nowTime,
  85. alarm=alarm)
  86. equipment_info.save()
  87. uid_push_qs = UidPushModel.objects.filter(uid_set__uid=devUid, uid_set__detect_status=1). \
  88. values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
  89. 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
  90. 'uid_set__channel')
  91. tz = '0'
  92. if uid_push_qs.exists():
  93. tz = uid_push_qs.first().tz
  94. local_date_time = CommonService.get_now_time_str(n_time=nowTime, tz=tz, lang='cn')
  95. local_date_time = local_date_time[0:10]
  96. equipment_info_vo = EquipmentInfoService.get_equipment_info_obj(
  97. local_date_time,
  98. device_user_id=userID,
  99. event_time=nowTime,
  100. event_type=eventType,
  101. device_uid=devUid,
  102. device_nick_name=device_info[0].NickName,
  103. channel=Channel,
  104. alarm=alarm,
  105. receive_time=receiveTime,
  106. add_time=nowTime,
  107. )
  108. equipment_info_vo.save()
  109. except Exception:
  110. errorInfo = traceback.format_exc()
  111. print(errorInfo)
  112. return response.json(500, errorInfo)
  113. else:
  114. if equipment_info.id:
  115. return response.json(0, {'infoID': equipment_info.id,
  116. 'devUid': devUid,
  117. 'Channel': Channel,
  118. 'alarm': alarm,
  119. 'eventType': eventType,
  120. 'eventTime': eventTime,
  121. 'receiveTime': receiveTime,
  122. 'devNickName': equipment_info.devNickName})
  123. else:
  124. return response.json(500)
  125. def query_info(self, request_dict, userID, response):
  126. page = int(request_dict.get('page', None))
  127. line = int(request_dict.get('line', None))
  128. if not page or not line:
  129. return response.json(444, 'page,line')
  130. qs = Equipment_Info.objects.filter(userID_id=userID)
  131. uid = request_dict.get('uid', None)
  132. if uid:
  133. qs = qs.filter(devUid=uid)
  134. dvqs = Device_Info.objects.filter(UID=uid).values('Type', 'NickName')
  135. uid_type_dict = {uid: {'type': dvqs[0]['Type'], 'NickName': dvqs[0]['NickName']}}
  136. else:
  137. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  138. uid_type_dict = {}
  139. for dv in dvqs:
  140. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  141. if not qs.exists():
  142. return response.json(0, {'datas': [], 'count': 0})
  143. count = qs.count()
  144. res = qs[(page - 1) * line:page * line]
  145. send_json = CommonService.qs_to_dict(res)
  146. res = []
  147. for p in send_json['datas']:
  148. p['fields']['viewAccont'] = ''
  149. p['fields']['viewPwd'] = ''
  150. devUid = p['fields']['devUid']
  151. if devUid in uid_type_dict.keys():
  152. p['fields']['uid_type'] = uid_type_dict[devUid]['type']
  153. p['fields']['devNickName'] = uid_type_dict[devUid]['NickName']
  154. else:
  155. p['uid_type'] = ''
  156. res.append(p)
  157. return response.json(0, {'datas': res, 'count': count})
  158. @staticmethod
  159. def update_info(request_dict, userID, response):
  160. is_update_all = int(request_dict.get('is_update_all', 0))
  161. event_type = request_dict.get('eventType', None)
  162. if is_update_all == 1:
  163. kwargs = {
  164. 'device_user_id': userID
  165. }
  166. if event_type:
  167. kwargs['event_type'] = int(event_type)
  168. try:
  169. EquipmentInfoService.all_read_equipment_info(**kwargs)
  170. except Exception as e:
  171. print(repr(e))
  172. else:
  173. id_list = request_dict.getlist('id[]', None)
  174. if id_list is None or len(id_list) < 1:
  175. id_list = request_dict.getlist('id', None)
  176. param_flag = CommonService.get_param_flag(data=[id_list])
  177. if not param_flag:
  178. return response.json(444)
  179. for full_id in id_list:
  180. try:
  181. ei_id = int(full_id[2:])
  182. equipment_info_model = EquipmentInfoService.get_equipment_info_model_with_full_id(full_id)
  183. equipment_info_model.filter(id=ei_id).update(status=1)
  184. except Exception as e:
  185. print(repr(e))
  186. return response.json(0)
  187. @staticmethod
  188. def update_answer_status(request_dict, user_id, response):
  189. """
  190. 更新一键通话消息状态为已接听
  191. @param request_dict:
  192. @param user_id: 用户id
  193. @param response:
  194. @return:
  195. """
  196. uid = request_dict.get('uid', None)
  197. event_time = request_dict.get('event_time', None)
  198. if not all([uid, event_time]):
  199. return response.json(444)
  200. updated = EquipmentInfoService.update_equipment_answer_status(user_id, uid, event_time)
  201. if updated:
  202. return response.json(0)
  203. else:
  204. return response.json(5)
  205. def delete_info(self, request_dict, userID, response):
  206. id_list = request_dict.getlist('id[]', None)
  207. if id_list is None or len(id_list) < 1:
  208. id_list = request_dict.getlist('id', None)
  209. param_flag = CommonService.get_param_flag(data=[id_list])
  210. if param_flag is True:
  211. try:
  212. for ei_id in id_list:
  213. tab_val = int(ei_id[0:1])
  214. ei_id = int(ei_id[1:])
  215. eq = EquipmentInfoService.get_equipment_info_model('', tab_val)
  216. eq.filter(id=ei_id).delete()
  217. except Exception as e:
  218. errorInfo = traceback.format_exc()
  219. print(errorInfo)
  220. return response.json(424, repr(e))
  221. else:
  222. return response.json(0)
  223. else:
  224. return response.json(444)
  225. def findByTime_info(self, request_dict, userID, response):
  226. startTime = request_dict.get('startTime')
  227. endTime = request_dict.get('endTime')
  228. page = int(request_dict.get('page', None))
  229. line = int(request_dict.get('line', None))
  230. if not startTime or not endTime or not page or not line:
  231. return response.json(444, 'startTime, endTime, page, line')
  232. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  233. if not len(uid_list):
  234. return response.json(0, {'datas': [], 'count': 0})
  235. qs = Equipment_Info.objects.filter(userID_id=userID,
  236. eventTime__range=(startTime, endTime)).order_by('-id')
  237. if qs.exists():
  238. count = qs.count()
  239. res = qs[(page - 1) * line:page * line]
  240. send_json = CommonService.qs_to_dict(res)
  241. send_json['count'] = count
  242. return response.json(0, send_json)
  243. # 管理员查询指定用户的推送信息接口
  244. def query_all_info(self, request_dict, userID, response):
  245. page = int(request_dict.get('page', None))
  246. line = int(request_dict.get('line', None))
  247. username = request_dict.get('username', None)
  248. uid = request_dict.get('uid', None)
  249. devNickName = request_dict.get('devNickName', None)
  250. Channel = request_dict.get('Channel', None)
  251. if not page or not line:
  252. return response.json(444, 'page,line')
  253. check_perm = ModelService.check_perm(userID=userID, permID=30)
  254. if not check_perm:
  255. return response.json(404)
  256. if not username and not uid and not devNickName and not Channel:
  257. qs = Equipment_Info.objects.all().order_by('-id')
  258. elif username:
  259. user = Device_User.objects.filter(username=username)
  260. users = CommonService.qs_to_dict(user)
  261. # 从字典类型中找到对应的pk值,即是userID
  262. user_id = users.get('datas')[0].get('pk')
  263. if not user.exists():
  264. return response.json(104)
  265. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('UID', flat=True)
  266. if not len(uid_list):
  267. return response.json(0, {'datas': [], 'count': 0})
  268. qs = Equipment_Info.objects.filter(userID_id=user_id).order_by('-id')
  269. elif uid:
  270. qs = Equipment_Info.objects.filter(devUid=uid).order_by('-id')
  271. elif devNickName:
  272. qs = Equipment_Info.objects.filter(devNickName=devNickName).order_by('-id')
  273. elif Channel:
  274. qs = Equipment_Info.objects.filter(Channel=Channel).order_by('-id')
  275. if not qs.exists():
  276. return response.json(0, {'datas': [], 'count': 0})
  277. count = qs.count()
  278. res = qs[(page - 1) * line:page * line]
  279. send_json = CommonService.qs_to_dict(res)
  280. send_json['count'] = count
  281. return response.json(0, send_json)
  282. # 管理员的删除推送信息的接口
  283. def delete_by_admin(self, request_dict, userID, response):
  284. id_list = request_dict.getlist('id', None)
  285. if not len(id_list):
  286. return response.json(444, 'id is None or not list')
  287. check_perm = ModelService.check_perm(userID=userID, permID=10)
  288. if check_perm is True:
  289. try:
  290. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  291. except Exception as e:
  292. errorInfo = traceback.format_exc()
  293. print(errorInfo)
  294. return response.json(424, {'details': repr(e)})
  295. else:
  296. return response.json(0, {'delete_count': is_delete[0]})
  297. else:
  298. return response.json(404)
  299. '''
  300. use information_schema;
  301. select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='Ansjer81';
  302. '''
  303. # 按季度删除访问日志
  304. def deleteExpireEquipmentInfoById(request):
  305. response = ResponseObject()
  306. id = int(request.GET.get('id', None))
  307. if id is None:
  308. return response.json(0)
  309. Equipment_Info.objects.filter(id__lte=id).delete()
  310. return response.json(0)