EquipmentOTA.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import os
  2. import base64
  3. import simplejson as json
  4. import time
  5. from django.utils import timezone
  6. from django.utils.decorators import method_decorator
  7. from django.views.decorators.csrf import csrf_exempt
  8. from django.views.generic.base import View
  9. from Ansjer.config import BASE_DIR, SERVER_DOMAIN
  10. from Model.models import Equipment_Version
  11. from Object.ResponseObject import ResponseObject
  12. from Object.TokenObject import TokenObject
  13. from Object.UrlTokenObject import UrlTokenObject
  14. from Service.CommonService import CommonService
  15. from Service.ModelService import ModelService
  16. from var_dump import var_dump
  17. '''
  18. http://192.168.136.45:8077/equipment/OTA?token=test&operation=query&page=1&line=10
  19. http://192.168.136.45:8077/equipment/OTA?token=test&operation=find&page=1&line=10&content={}
  20. http://192.168.136.40:8077/equipment/OTA?eid=138001524641441203480138000&token=test&operation=delete
  21. http://192.168.136.40:8077/equipment/OTA?eid=138001524641441203480138000&token=test&operation=update&status=1
  22. https://cdna.artstation.com/p/assets/images/images/004/727/674/large/daniel-dociu-snowcavels.jpg?1485828233&dl=1
  23. https://cdna.artstation.com/p/assets/images/images/004/693/910/large/daniel-dociu-smoothsailing.jpg?1485560128&dl=1
  24. '''
  25. class EquipmentOTA(View):
  26. @method_decorator(csrf_exempt)
  27. def dispatch(self, *args, **kwargs):
  28. return super(EquipmentOTA, 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. if token is None:
  39. return response.json(309)
  40. tko = TokenObject(token)
  41. response.lang = tko.lang
  42. if tko.code != 0:
  43. return response.json(tko.code)
  44. userID = tko.userID
  45. if userID is None:
  46. return response.json(309)
  47. operation = request_dict.get('operation', None)
  48. if operation == 'delete':
  49. return self.delete(request_dict, userID, response)
  50. elif operation == 'query':
  51. return self.query(request_dict, userID, response)
  52. elif operation == 'find':
  53. return self.find(request_dict, userID, response)
  54. elif operation == 'update':
  55. return self.update(request_dict, userID, response)
  56. else:
  57. return response.json(444, 'operation')
  58. def delete(self, request_dict, userID, response):
  59. eid = request_dict.get('eid', None)
  60. if not eid:
  61. return response.json(444, 'eid')
  62. own_perm = ModelService.check_perm(userID=userID, permID=230)
  63. if not own_perm:
  64. return response.json(404)
  65. ev = Equipment_Version.objects.filter(eid=eid)
  66. if not ev.exists():
  67. return response.json(173)
  68. try:
  69. has_ev = Equipment_Version.objects.filter(filePath=ev[0].filePath)
  70. if has_ev.exists():
  71. pass
  72. else:
  73. filepath = os.path.join(BASE_DIR, ev[0].filePath)
  74. os.remove(filepath)
  75. ev.delete()
  76. except Exception as e:
  77. return response.json(176, repr(e))
  78. else:
  79. return response.json(0)
  80. def query(self, request_dict, userID, response):
  81. page = int(request_dict.get('page', None))
  82. line = int(request_dict.get('line', None))
  83. lang = request_dict.get('lang', None)
  84. if page is None or line is None:
  85. return response.json(444, 'page,line')
  86. own_permission = ModelService.check_perm(userID=userID, permID=240)
  87. if own_permission is not True:
  88. return response.json(404)
  89. if lang is None:
  90. qs = Equipment_Version.objects.all().order_by('-data_joined')
  91. else:
  92. qs = Equipment_Version.objects.filter(lang=lang).order_by('-data_joined')
  93. if qs.exists():
  94. count = qs.count()
  95. res = qs[(page - 1) * line:page * line]
  96. send_json = CommonService.qs_to_dict(res)
  97. send_json['count'] = count
  98. return response.json(0, send_json)
  99. return response.json(0)
  100. def find(self, request_dict, userID, response):
  101. page = int(request_dict.get('page', None))
  102. line = int(request_dict.get('line', None))
  103. content = request_dict.get('content', None)
  104. if page is None or line is None or content is None:
  105. return response.json(444)
  106. own_permission = ModelService.check_perm(userID=userID, permID=250)
  107. if own_permission is not True:
  108. return response.json(404)
  109. content = json.loads(content)
  110. search_kwargs = CommonService.get_kwargs(data=content)
  111. qs = Equipment_Version.objects.filter(**search_kwargs)
  112. if not qs.exists():
  113. return response.json(0, [])
  114. count = qs.count()
  115. res = qs[(page - 1) * line:page * line]
  116. send_json = CommonService.qs_to_dict(res)
  117. send_json['count'] = count
  118. return response.json(0, send_json)
  119. def update(self, request_dict, userID, response):
  120. eid = request_dict.get('eid', None)
  121. if eid is None:
  122. return response.json(444, 'eid')
  123. own_permission = ModelService.check_perm(userID=userID, permID=230)
  124. if own_permission is not True:
  125. return response.json(404)
  126. ev = Equipment_Version.objects.filter(eid=eid)
  127. if not ev.exists():
  128. return response.json(10, 'ev none')
  129. status = request_dict.get('status', None)
  130. try:
  131. update_time = timezone.localtime(timezone.now())
  132. ev.update(status=status, update_time=update_time)
  133. except Exception as e:
  134. print(repr(e))
  135. pass
  136. else:
  137. return response.json(0, {'update_time': str(update_time)})
  138. class EquipmentVersionView(View):
  139. def get(self, request, *args, **kwargs):
  140. request.encoding = 'utf-8'
  141. operation = kwargs.get('operation')
  142. return self.validation(request.GET, operation)
  143. def post(self, request, *args, **kwargs):
  144. request.encoding = 'utf-8'
  145. operation = kwargs.get('operation')
  146. return self.validation(request.POST, operation)
  147. def validation(self, request_dict, operation):
  148. response = ResponseObject()
  149. if operation is None:
  150. return response.json(444, 'error path')
  151. elif operation == 'checkVer':
  152. return self.do_check_value(request_dict, response)
  153. # return self.do_check_ver(request_dict, response)
  154. token = request_dict.get('token', None)
  155. # 设备主键uid
  156. tko = TokenObject(token)
  157. response.lang = tko.lang
  158. if tko.code != 0:
  159. return response.json(tko.code)
  160. userID = tko.userID
  161. own_perm = ModelService.check_perm(userID, 240)
  162. if own_perm is False:
  163. return response.json(404)
  164. if operation == 'query':
  165. return self.do_query(request_dict, response)
  166. if operation == 'update':
  167. return self.do_update(request_dict, response)
  168. if operation == 'filter':
  169. return self.do_filter(request_dict, response)
  170. else:
  171. return response.json(444, 'error path')
  172. def do_update(self, request_dict, response):
  173. code = request_dict.get('code', None)
  174. status = request_dict.get('status', None)
  175. lang = request_dict.get('lang', None)
  176. if code is not None:
  177. ev = Equipment_Version.objects.filter(code=code, lang=lang)
  178. if ev.exists():
  179. try:
  180. update_time = timezone.localtime(timezone.now())
  181. ev.update(status=status, update_time=update_time)
  182. except Exception as e:
  183. return response.json(404, repr(e))
  184. else:
  185. return response.json(0, {'update_time': str(update_time)})
  186. else:
  187. return response.json(173)
  188. else:
  189. return response.json(444, 'code')
  190. def do_check_ver(self, request_dict, response):
  191. code = request_dict.get('code', None)
  192. ov = request_dict.get('ov', None)
  193. uid = request_dict.get('uid', '')
  194. if not code:
  195. return response.json(444, 'code')
  196. eqs = Equipment_Version.objects.filter(code=uid, status=1, lang='en').order_by('-data_joined')
  197. if not eqs.exclude():
  198. eqs = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
  199. if not eqs.exists():
  200. return response.json(900)
  201. if ov is not None:
  202. # 判断大小
  203. if ov > eqs[0].softwareVersion:
  204. return response.json(0, {'softwareVersion': ov})
  205. file_path = eqs[0].filePath
  206. if file_path.find('static/Upgrade/') != -1:
  207. path = file_path.replace('static/Upgrade/', '').replace('\\', '/')
  208. url = SERVER_DOMAIN + 'OTA/downloads/' + path + '?time=' + str(time.time())
  209. elif file_path.find('static/otapack') != -1:
  210. url = SERVER_DOMAIN + 'OTA/downloadsPack/' + file_path + '?time=' + str(time.time())
  211. # url = SERVER_DOMAIN + 'dl/otaPack/' + file_path + '?time=' + str(time.time())
  212. else:
  213. return response.json(900, '2')
  214. res = {
  215. "url": url,
  216. "fileSize": eqs[0].fileSize,
  217. "Description": eqs[0].Description,
  218. "softwareVersion": eqs[0].softwareVersion
  219. }
  220. return response.json(0, res)
  221. # 修改加密的路径
  222. def do_check_value(self, request_dict, response):
  223. code = request_dict.get('code', None)
  224. ov = request_dict.get('ov', None)
  225. uid = request_dict.get('uid', '')
  226. if not code:
  227. return response.json(444, 'code')
  228. eqs = Equipment_Version.objects.filter(code=uid, status=1, lang='en').order_by('-data_joined')
  229. if not eqs.exclude():
  230. eqs = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
  231. if not eqs.exists():
  232. return response.json(900)
  233. if ov is not None:
  234. # 判断大小
  235. if ov > eqs[0].softwareVersion:
  236. return response.json(0, {'softwareVersion': ov})
  237. file_path = eqs[0].filePath
  238. if file_path.find('static/Upgrade/') != -1:
  239. path = file_path.replace('static/Upgrade/', '').replace('\\', '/')
  240. # 创建url的token
  241. tko = UrlTokenObject()
  242. path = tko.generate(data={'Url': "ansjer/" + CommonService.RandomStr(6) + "/" + file_path})
  243. url = SERVER_DOMAIN + 'OTA/downloads/' + path
  244. elif file_path.find('static/otapack') != -1:
  245. file_path = file_path
  246. # 创建url的token
  247. tko = UrlTokenObject()
  248. file_path = tko.generate(data={'Url': "ansjer/" + CommonService.RandomStr(6) + "/" + file_path})
  249. url = SERVER_DOMAIN + 'dlotapack/' + file_path
  250. else:
  251. return response.json(900, '2')
  252. # http: // 192.168.136.39:8000 / EquipmentVersion / checkVer?code = 20D20410010
  253. res = {
  254. "url": url,
  255. "fileSize": eqs[0].fileSize,
  256. "Description": eqs[0].Description,
  257. "softwareVersion": eqs[0].softwareVersion
  258. }
  259. print(url)
  260. return response.json(0, res)
  261. def do_query(self, request_dict, response):
  262. mci = request_dict.get('mci', None)
  263. page = request_dict.get('page', None)
  264. line = request_dict.get('line', None)
  265. code = request_dict.get('code', None)
  266. if not page or not line:
  267. return response.json(444, 'page,line')
  268. else:
  269. page = int(page)
  270. line = int(line)
  271. cv_qs = Equipment_Version.objects
  272. if mci is not None:
  273. cv_qs = cv_qs.filter(mci=mci)
  274. if code is not None:
  275. cv_qs = cv_qs.filter(code__contains=code)
  276. cv_qs = cv_qs.order_by('code').values_list('code', flat=True).distinct()
  277. print(cv_qs)
  278. count = cv_qs.count()
  279. cv_qs = list(cv_qs[(page - 1) * line:page * line])
  280. # return response.json(0, cv_qs)
  281. ev_qs = Equipment_Version.objects.filter(code__in=list(cv_qs)).values()
  282. ev_list = CommonService.qs_to_list(ev_qs)
  283. datas = {}
  284. # 组装数据
  285. for ev in ev_list:
  286. for code in cv_qs:
  287. if code not in datas.keys():
  288. datas[code] = []
  289. if code == ev['code']:
  290. datas[code].append(ev)
  291. res = {
  292. 'datas': datas,
  293. 'count': count
  294. }
  295. return response.json(0, res)
  296. def do_filter(self, request_dict, response):
  297. mci = request_dict.get('mci', None)
  298. page = request_dict.get('page', None)
  299. line = request_dict.get('line', None)
  300. code = request_dict.get('code', None)
  301. lang = request_dict.get('lang', None)
  302. if not page or not line:
  303. return response.json(444, 'page,line')
  304. else:
  305. page = int(page)
  306. line = int(line)
  307. ev_qs = Equipment_Version.objects
  308. if mci:
  309. ev_qs = ev_qs.filter(mci=mci)
  310. if code:
  311. ev_qs = ev_qs.filter(code__contains=code)
  312. if lang:
  313. ev_qs = ev_qs.filter(lang=lang)
  314. count = ev_qs.count()
  315. ev_qs = ev_qs[(page - 1) * line:page * line].values()
  316. ev_ls = CommonService.qs_to_list(ev_qs)
  317. res = {
  318. 'datas': ev_ls,
  319. 'count': count
  320. }
  321. return response.json(0, res)