|
@@ -1,32 +1,14 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
-import hashlib
|
|
|
-import json
|
|
|
import os
|
|
|
-import time
|
|
|
-import urllib
|
|
|
-import uuid
|
|
|
-import boto3
|
|
|
-import threading
|
|
|
-import logging
|
|
|
-
|
|
|
-import xlwt
|
|
|
-from boto3.session import Session
|
|
|
-from django.http import JsonResponse, HttpResponseRedirect, HttpResponse, StreamingHttpResponse
|
|
|
+import hashlib
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Ansjer.config import BASE_DIR, SERVER_DOMAIN
|
|
|
-from Model.models import Device_Info, Role, MenuModel, VodBucketModel, CDKcontextModel, Store_Meal, Order_Model, \
|
|
|
- UID_Bucket, ExperienceContextModel, Lang, Device_User, CloudLogModel, UidSetModel, Equipment_Version, \
|
|
|
- EquipmentVersionLimitModel
|
|
|
-from Object.RedisObject import RedisObject
|
|
|
-from Object.ResponseObject import ResponseObject
|
|
|
+from Ansjer.config import BASE_DIR
|
|
|
+from Model.models import Equipment_Version
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Object.UidTokenObject import UidTokenObject
|
|
|
-from Object.UrlTokenObject import UrlTokenObject
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
from Service.CommonService import CommonService
|
|
|
-from django.db.models import Q, F
|
|
|
-from time import strftime
|
|
|
|
|
|
|
|
|
class VersionManagement(View):
|
|
@@ -43,23 +25,24 @@ class VersionManagement(View):
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
language = request_dict.get('language', 'en')
|
|
|
response = ResponseObject(language, 'pc')
|
|
|
- tko = TokenObject(
|
|
|
- request.META.get('HTTP_AUTHORIZATION'),
|
|
|
- returntpye='pc')
|
|
|
- if tko.code != 0:
|
|
|
- return response.json(tko.code)
|
|
|
- response.lang = tko.lang
|
|
|
- userID = tko.userID
|
|
|
- if operation == 'getEquipmentVersionList':
|
|
|
- return self.getEquipmentVersionList(request_dict, response)
|
|
|
- elif operation == 'query':
|
|
|
- return self.do_query(request_dict, response)
|
|
|
- elif operation == 'update':
|
|
|
- return self.do_update(request_dict, response, request)
|
|
|
- elif operation == 'filter':
|
|
|
- return self.do_filter(request_dict, response)
|
|
|
+ if operation == 'upLoadFile':
|
|
|
+ return self.upLoadFile(request, request_dict, response)
|
|
|
else:
|
|
|
- return response.json(404)
|
|
|
+ tko = TokenObject(
|
|
|
+ request.META.get('HTTP_AUTHORIZATION'),
|
|
|
+ returntpye='pc')
|
|
|
+ if tko.code != 0:
|
|
|
+ return response.json(tko.code)
|
|
|
+ response.lang = tko.lang
|
|
|
+ userID = tko.userID
|
|
|
+ if operation == 'getEquipmentVersionList':
|
|
|
+ return self.getEquipmentVersionList(request_dict, response)
|
|
|
+ elif operation == 'editVersionInformation':
|
|
|
+ return self.editVersionInformation(request_dict, response)
|
|
|
+ elif operation == 'deleteEquipmentVersion':
|
|
|
+ return self.deleteEquipmentVersion(request_dict, response)
|
|
|
+ else:
|
|
|
+ return response.json(404)
|
|
|
|
|
|
def getEquipmentVersionList(self, request_dict, response):
|
|
|
mci = request_dict.get('mci', None)
|
|
@@ -91,215 +74,106 @@ class VersionManagement(View):
|
|
|
print(e)
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- def do_update(self, request_dict, response, request):
|
|
|
- code = request_dict.get('code', None)
|
|
|
- status = request_dict.get('status', None)
|
|
|
- lang = request_dict.get('lang', None)
|
|
|
- max_ver = request_dict.get('max_ver', None)
|
|
|
- file_s = request.FILES.get('file', None)
|
|
|
- paths = 'static/versions/image'
|
|
|
- if not os.path.exists(paths):
|
|
|
- os.makedirs(paths) # makedirs 创建文件时如果路径不存在会创建这个路径
|
|
|
- # print ('该文件不存在')
|
|
|
- else:
|
|
|
- print('该文件存在')
|
|
|
- ev = Equipment_Version.objects.filter(code=code, lang=lang)
|
|
|
- if file_s is None:
|
|
|
- rv_path = 'static/versions/image/' + code + '.png'
|
|
|
- img = SERVER_DOMAIN + 'sysfile/' + rv_path
|
|
|
- else:
|
|
|
- rv_path = 'static/versions/image/' + code + '.png'
|
|
|
- try:
|
|
|
- as_path = os.path.join(BASE_DIR, rv_path)
|
|
|
- if os.path.exists(as_path):
|
|
|
- os.remove(as_path)
|
|
|
- with open(as_path, 'wb+') as destination:
|
|
|
- for chunk in file_s.chunks():
|
|
|
- destination.write(chunk)
|
|
|
- except Exception as e:
|
|
|
- pass
|
|
|
- else:
|
|
|
- img = SERVER_DOMAIN + 'sysfile/' + rv_path
|
|
|
- if code is not None:
|
|
|
+ def upLoadFile(self, request, request_dict, response):
|
|
|
+ file = request.FILES.get('file', None)
|
|
|
+ mci = request_dict.get('mci', '')
|
|
|
+ lang = request_dict.get('lang', '')
|
|
|
+ ESN = request_dict.get('ESN', '')
|
|
|
+ max_ver = request_dict.get('max_ver', '')
|
|
|
+ channel = request_dict.get('channel', '')
|
|
|
+ resolutionRatio = request_dict.get('resolutionRatio', '')
|
|
|
+ Description = request_dict.get('Description', '')
|
|
|
+ status = request_dict.get('status', 0)
|
|
|
+ fileSize = request_dict.get('fileSize', '')
|
|
|
+
|
|
|
+ if not all([file, mci, lang, ESN, max_ver, channel, resolutionRatio]):
|
|
|
+ return response.json(444)
|
|
|
|
|
|
- if ev.exists():
|
|
|
- try:
|
|
|
- update_time = time.timezone.localtime(time.timezone.now())
|
|
|
- update_dict = {
|
|
|
- 'status': status,
|
|
|
- 'img': img,
|
|
|
- 'update_time': update_time
|
|
|
- }
|
|
|
- if max_ver:
|
|
|
- update_dict['max_ver'] = max_ver
|
|
|
- ev.update(**update_dict)
|
|
|
- except Exception as e:
|
|
|
- return response.json(404, repr(e))
|
|
|
- else:
|
|
|
- return response.json(0, {'update_time': str(update_time), 'img': img})
|
|
|
+ try:
|
|
|
+ channel = int(channel)
|
|
|
+ resolutionRatio = int(resolutionRatio)
|
|
|
+ status = int(status)
|
|
|
+ # 文件名为设备版本,最后一个'.'的前面为软件版本,后面为设备规格名称
|
|
|
+ # V2.2.4.16E201252CA,软件版本:2.2.4,设备规格名称:16E201252CA
|
|
|
+ file_name = str(file) # 文件名
|
|
|
+ version = file_name[:file_name.rindex('.')] # 设备版本
|
|
|
+ rpoint = version.rindex('.')
|
|
|
+ softwareVersion = version[1:rpoint] # 软件版本
|
|
|
+ code = version[rpoint+1:] # 设备规格名称
|
|
|
+ chipModelList2Code = code[:4] # 主芯片
|
|
|
+ type = code[-3:-1] # 设备机型
|
|
|
+ companyCode = code[-1:] # 公司代码
|
|
|
+ fileSize = fileSize
|
|
|
+ filePath = '/'.join(('static/otapack', mci, lang, file_name))
|
|
|
+ file_data = file.read()
|
|
|
+ fileMd5 = hashlib.md5(file_data).hexdigest()
|
|
|
+ data_dict = {'mci': mci, 'lang': lang, 'ESN': ESN, 'max_ver': max_ver, 'channel': channel,
|
|
|
+ 'resolutionRatio': resolutionRatio, 'Description': Description, 'status': status,
|
|
|
+ 'softwareVersion': softwareVersion, 'code': code, 'chipModelList2Code': chipModelList2Code,
|
|
|
+ 'type': type, 'companyCode': companyCode, 'fileSize': fileSize, 'filePath': filePath, 'fileMd5': fileMd5}
|
|
|
+ # Equipment_Version表创建或更新数据
|
|
|
+ equipment_version_qs = Equipment_Version.objects.filter(code=code, lang=lang)
|
|
|
+ if not equipment_version_qs.exists():
|
|
|
+ Equipment_Version.objects.create(eid=CommonService.getUserID(getUser=False, setOTAID=True), **data_dict)
|
|
|
else:
|
|
|
- return response.json(173)
|
|
|
- else:
|
|
|
- return response.json(444, 'code')
|
|
|
-
|
|
|
- def do_check_ver(self, request_dict, response):
|
|
|
- code = request_dict.get('code', None)
|
|
|
- ov = request_dict.get('ov', None)
|
|
|
- uid = request_dict.get('uid', '')
|
|
|
- if not code:
|
|
|
- return response.json(444, 'code')
|
|
|
- eqs = Equipment_Version.objects.filter(code=uid, status=1, lang='en').order_by('-data_joined')
|
|
|
- if not eqs.exclude():
|
|
|
- eqs = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
|
|
|
- if not eqs.exists():
|
|
|
- return response.json(900)
|
|
|
- if ov is not None:
|
|
|
- # 判断大小
|
|
|
- if ov > eqs[0].softwareVersion:
|
|
|
- return response.json(0, {'softwareVersion': ov})
|
|
|
- file_path = eqs[0].filePath
|
|
|
- if file_path.find('static/Upgrade/') != -1:
|
|
|
- path = file_path.replace('static/Upgrade/', '').replace('\\', '/')
|
|
|
- url = SERVER_DOMAIN + 'OTA/downloads/' + path + '?time=' + str(time.time())
|
|
|
- elif file_path.find('static/otapack') != -1:
|
|
|
- url = SERVER_DOMAIN + 'OTA/downloadsPack/' + file_path + '?time=' + str(time.time())
|
|
|
- # url = SERVER_DOMAIN + 'dl/otaPack/' + file_path + '?time=' + str(time.time())
|
|
|
- else:
|
|
|
- return response.json(900, '2')
|
|
|
- res = {
|
|
|
- "url": url,
|
|
|
- "fileSize": eqs[0].fileSize,
|
|
|
- "Description": eqs[0].Description,
|
|
|
- "softwareVersion": eqs[0].softwareVersion
|
|
|
- }
|
|
|
- return response.json(0, res)
|
|
|
-
|
|
|
- # 修改加密的路径
|
|
|
- def do_check_value(self, request_dict, response):
|
|
|
- code = request_dict.get('code', None)
|
|
|
- ov = request_dict.get('ov', None)
|
|
|
- uid = request_dict.get('uid', '')
|
|
|
- if not code:
|
|
|
- return response.json(444, 'code')
|
|
|
- eqs = Equipment_Version.objects.filter(code=uid, status=1, lang='en').order_by('-data_joined')
|
|
|
-
|
|
|
- if not eqs.exclude():
|
|
|
- eqs = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
|
|
|
- if not eqs.exists():
|
|
|
- return response.json(900)
|
|
|
- if ov is not None:
|
|
|
- # 判断大小
|
|
|
- if ov > eqs[0].softwareVersion:
|
|
|
- return response.json(0, {'softwareVersion': ov})
|
|
|
- equipment = eqs[0]
|
|
|
- redisObject = RedisObject()
|
|
|
- key = 'limit_{eid}'.format(eid=equipment.eid)
|
|
|
+ equipment_version_qs.update(**data_dict)
|
|
|
+
|
|
|
+ # 上传文件到服务器
|
|
|
+ upload_path = '/'.join((BASE_DIR, 'static/otapack', mci, lang)).replace('\\', '/') + '/'
|
|
|
+ if not os.path.exists(upload_path): # 上传目录不存在则创建
|
|
|
+ os.makedirs(upload_path)
|
|
|
+ # 文件上传
|
|
|
+ full_name = upload_path + file_name
|
|
|
+ if os.path.exists(full_name): # 删除同名文件
|
|
|
+ os.remove(full_name)
|
|
|
+ with open(full_name, 'wb+') as write_file:
|
|
|
+ for chunk in file.chunks():
|
|
|
+ write_file.write(chunk)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
- evl_qs = redisObject.get_data(key=key)
|
|
|
- if evl_qs:
|
|
|
- evl_qs = json.loads(evl_qs)
|
|
|
- else:
|
|
|
- evl_qs = EquipmentVersionLimitModel.objects.filter(equipment_version_id=equipment.eid,
|
|
|
- status=1).values()
|
|
|
- if evl_qs.exists():
|
|
|
- redisObject.set_data(key=key, val=json.dumps(list(evl_qs.values())), expire=600)
|
|
|
- if evl_qs and len(evl_qs) > 0:
|
|
|
- evl = evl_qs[0]
|
|
|
- if evl['type'] == 1: # uid限制
|
|
|
- uids = json.loads(evl['content'])
|
|
|
- if not uids.__contains__(uid):
|
|
|
- return response.json(902)
|
|
|
- file_path = eqs[0].filePath
|
|
|
- if file_path.find('static/Upgrade/') != -1:
|
|
|
- path = file_path.replace('static/Upgrade/', '').replace('\\', '/')
|
|
|
- # 创建url的token
|
|
|
- tko = UrlTokenObject()
|
|
|
- path = tko.generate(data={'Url': "ansjer/" + CommonService.RandomStr(6) + "/" + file_path})
|
|
|
+ def editVersionInformation(self, request_dict, response):
|
|
|
+ eid = request_dict.get('eid', None)
|
|
|
+ ESN = request_dict.get('ESN', '')
|
|
|
+ max_ver = request_dict.get('max_ver', '')
|
|
|
+ status = request_dict.get('status', '')
|
|
|
+ channel = request_dict.get('channel', '')
|
|
|
+ resolutionRatio = request_dict.get('resolutionRatio', '')
|
|
|
+ Description = request_dict.get('Description', '')
|
|
|
|
|
|
- url = SERVER_DOMAIN + 'OTA/downloads/' + path
|
|
|
- elif file_path.find('static/otapack') != -1:
|
|
|
- file_path = file_path
|
|
|
- # 创建url的token
|
|
|
- tko = UrlTokenObject()
|
|
|
- file_path = tko.generate(data={'Url': "ansjer/" + CommonService.RandomStr(6) + "/" + file_path})
|
|
|
+ if not eid:
|
|
|
+ return response.json(444)
|
|
|
+ status = 1 if status == 'true' else 0
|
|
|
+ try:
|
|
|
+ equipment_version_qs = Equipment_Version.objects.filter(eid=eid)
|
|
|
+ if not equipment_version_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ data_dict = {'ESN': ESN, 'max_ver': max_ver, 'status': status, 'channel': channel,
|
|
|
+ 'resolutionRatio': resolutionRatio, 'Description': Description}
|
|
|
+ equipment_version_qs.update(**data_dict)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
- url = SERVER_DOMAIN + 'dlotapack/' + file_path
|
|
|
- else:
|
|
|
- return response.json(900, '2')
|
|
|
- # http: // 192.168.136.39:8000 / EquipmentVersion / checkVer?code = 20D20410010
|
|
|
+ def deleteEquipmentVersion(self, request_dict, response):
|
|
|
+ eid = request_dict.get('eid', None)
|
|
|
|
|
|
- res = {
|
|
|
- "url": url,
|
|
|
- "fileSize": eqs[0].fileSize,
|
|
|
- "Description": eqs[0].Description,
|
|
|
- "softwareVersion": eqs[0].softwareVersion
|
|
|
- }
|
|
|
- print(url)
|
|
|
- return response.json(0, res)
|
|
|
-
|
|
|
- def do_query(self, request_dict, response):
|
|
|
- mci = request_dict.get('mci', None)
|
|
|
- page = request_dict.get('page', None)
|
|
|
- line = request_dict.get('line', None)
|
|
|
- code = request_dict.get('code', None)
|
|
|
- if not page or not line:
|
|
|
- return response.json(444, 'page,line')
|
|
|
- else:
|
|
|
- page = int(page)
|
|
|
- line = int(line)
|
|
|
- cv_qs = Equipment_Version.objects
|
|
|
- if mci is not None:
|
|
|
- cv_qs = cv_qs.filter(mci=mci)
|
|
|
- if code is not None:
|
|
|
- cv_qs = cv_qs.filter(code__contains=code)
|
|
|
- cv_qs = cv_qs.order_by('code').values_list('code', flat=True).distinct()
|
|
|
- print(cv_qs)
|
|
|
- count = cv_qs.count()
|
|
|
- cv_qs = list(cv_qs[(page - 1) * line:page * line])
|
|
|
- # return response.json(0, cv_qs)
|
|
|
- ev_qs = Equipment_Version.objects.filter(code__in=list(cv_qs)).values()
|
|
|
- ev_list = CommonService.qs_to_list(ev_qs)
|
|
|
- datas = {}
|
|
|
- # 组装数据
|
|
|
- for ev in ev_list:
|
|
|
- for code in cv_qs:
|
|
|
- if code not in datas.keys():
|
|
|
- datas[code] = []
|
|
|
- if code == ev['code']:
|
|
|
- datas[code].append(ev)
|
|
|
- res = {
|
|
|
- 'datas': datas,
|
|
|
- 'count': count
|
|
|
- }
|
|
|
- return response.json(0, res)
|
|
|
+ if not eid:
|
|
|
+ return response.json(444)
|
|
|
|
|
|
- def do_filter(self, request_dict, response):
|
|
|
- mci = request_dict.get('mci', None)
|
|
|
- page = request_dict.get('page', None)
|
|
|
- line = request_dict.get('line', None)
|
|
|
- code = request_dict.get('code', None)
|
|
|
- lang = request_dict.get('lang', None)
|
|
|
- if not page or not line:
|
|
|
- return response.json(444, 'page,line')
|
|
|
- else:
|
|
|
- page = int(page)
|
|
|
- line = int(line)
|
|
|
- ev_qs = Equipment_Version.objects
|
|
|
- if mci:
|
|
|
- ev_qs = ev_qs.filter(mci=mci)
|
|
|
- if code:
|
|
|
- ev_qs = ev_qs.filter(code__contains=code)
|
|
|
- if lang:
|
|
|
- if lang == 'cn' or lang == 'zh-Hans':
|
|
|
- ev_qs = ev_qs.filter(Q(lang='cn') | Q(lang='zh-Hans'))
|
|
|
- else:
|
|
|
- ev_qs = ev_qs.filter(lang=lang)
|
|
|
+ try:
|
|
|
+ equipment_version_qs = Equipment_Version.objects.filter(eid=eid)
|
|
|
+ filePath = equipment_version_qs.values('filePath')[0]['filePath']
|
|
|
+ equipment_version_qs.delete()
|
|
|
+ # 删除文件
|
|
|
+ full_name = '/'.join((BASE_DIR, filePath)).replace('\\', '/')
|
|
|
+ if os.path.exists(full_name):
|
|
|
+ os.remove(full_name)
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
- count = ev_qs.count()
|
|
|
- ev_qs = ev_qs[(page - 1) * line:page * line].values()
|
|
|
- ev_ls = CommonService.qs_to_list(ev_qs)
|
|
|
- res = {
|
|
|
- 'datas': ev_ls,
|
|
|
- 'count': count
|
|
|
- }
|
|
|
- return response.json(0, res)
|