|
@@ -1,5 +1,6 @@
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# -*- coding: utf-8 -*-
|
|
|
|
+import json
|
|
import logging
|
|
import logging
|
|
import random
|
|
import random
|
|
import time
|
|
import time
|
|
@@ -10,7 +11,7 @@ from django.views import View
|
|
from Controller.DetectController import DetectControllerView
|
|
from Controller.DetectController import DetectControllerView
|
|
from Controller.DeviceConfirmRegion import Device_Region
|
|
from Controller.DeviceConfirmRegion import Device_Region
|
|
from Model.models import SerialNumberModel, CompanySerialModel, UIDCompanySerialModel, CompanyModel, RegionModel, \
|
|
from Model.models import SerialNumberModel, CompanySerialModel, UIDCompanySerialModel, CompanyModel, RegionModel, \
|
|
- CountryModel, UIDModel, Device_Info, iotdeviceInfoModel, UidPushModel
|
|
|
|
|
|
+ CountryModel, UIDModel, Device_Info, iotdeviceInfoModel, UidPushModel, LogModel
|
|
from Object.RedisObject import RedisObject
|
|
from Object.RedisObject import RedisObject
|
|
from Object.uidManageResponseObject import uidManageResponseObject
|
|
from Object.uidManageResponseObject import uidManageResponseObject
|
|
from Object.TokenObject import TokenObject
|
|
from Object.TokenObject import TokenObject
|
|
@@ -45,7 +46,7 @@ class SerialNumberView(View):
|
|
elif operation == 'attachUID':
|
|
elif operation == 'attachUID':
|
|
return self.do_attach_uid(request_dict, response, request)
|
|
return self.do_attach_uid(request_dict, response, request)
|
|
elif operation == 'detachUID':
|
|
elif operation == 'detachUID':
|
|
- return self.do_detach_uid(request_dict, response)
|
|
|
|
|
|
+ return self.do_detach_uid(request, request_dict, response)
|
|
elif operation == 'create':
|
|
elif operation == 'create':
|
|
return self.do_create(request_dict, response)
|
|
return self.do_create(request_dict, response)
|
|
else:
|
|
else:
|
|
@@ -188,128 +189,112 @@ class SerialNumberView(View):
|
|
token = request_dict.get('token', None)
|
|
token = request_dict.get('token', None)
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
|
|
|
|
- if serial_number and len(serial_number) == 9 and company_id:
|
|
|
|
|
|
+ if not all([serial_number, company_id, token, time_stamp]) or len(serial_number) != 9:
|
|
|
|
+ return response.json(444)
|
|
|
|
|
|
- token = int(CommonService.decode_data(token))
|
|
|
|
- time_stamp = int(time_stamp)
|
|
|
|
|
|
+ token = int(CommonService.decode_data(token))
|
|
|
|
+ time_stamp = int(time_stamp)
|
|
|
|
|
|
- now_time = int(time.time())
|
|
|
|
- distance = now_time - time_stamp
|
|
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ distance = now_time - time_stamp
|
|
|
|
|
|
- if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
|
|
|
|
- return response.json(404)
|
|
|
|
|
|
+ if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
|
|
|
|
+ return response.json(404)
|
|
|
|
|
|
- mark = serial_number[6:9]
|
|
|
|
- serial = serial_number[0:6]
|
|
|
|
|
|
+ mark = serial_number[6:9]
|
|
|
|
+ serial = serial_number[0:6]
|
|
|
|
|
|
- savePoint = transaction.savepoint()
|
|
|
|
|
|
+ savePoint = transaction.savepoint()
|
|
|
|
+ try:
|
|
try:
|
|
try:
|
|
- try:
|
|
|
|
- if not country_id:
|
|
|
|
- ip = CommonService.get_ip_address(request)
|
|
|
|
- country_id = Device_Region().get_device_region(ip)
|
|
|
|
-
|
|
|
|
- # 判断序列号是否已和企业关联
|
|
|
|
- company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_id,
|
|
|
|
- serial_number=serial)
|
|
|
|
- if not company_serial_qs.exists():
|
|
|
|
- return response.json(173)
|
|
|
|
-
|
|
|
|
- # 当序列号已关联UID
|
|
|
|
- company_serial = company_serial_qs[0]
|
|
|
|
-
|
|
|
|
- if company_serial.status == 0:
|
|
|
|
- # 该序列号未绑定企业
|
|
|
|
- return response.json(173)
|
|
|
|
- elif company_serial.status == 1:
|
|
|
|
-
|
|
|
|
- count = 0
|
|
|
|
- while count < 3:
|
|
|
|
- p2p = SerialNumberModel.objects.filter(serial_number=serial).values('p2p')
|
|
|
|
- print('此序列号的p2p类型:', p2p[0]['p2p'])
|
|
|
|
- uid_qs = UIDModel.objects.filter(vpg__company_id=company_serial.company.id,
|
|
|
|
- vpg__region_id=country_id,
|
|
|
|
- status=0, p2p_type=p2p[0]['p2p']).order_by('id')[0:10]
|
|
|
|
- # uid_qs:未进行绑定的uid列表
|
|
|
|
- if uid_qs.exists():
|
|
|
|
- uid = uid_qs[0]
|
|
|
|
- # uid.status = 2
|
|
|
|
- # uid.update_time = int(time.time())
|
|
|
|
- result = UIDModel.objects.filter(id=uid.id, status=0).update(**{
|
|
|
|
- 'status': 2, 'update_time': int(time.time())
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- if int(result) <= 0:
|
|
|
|
- count += 1
|
|
|
|
- continue
|
|
|
|
-
|
|
|
|
- now_time = int(time.time())
|
|
|
|
- uid_serial = UIDCompanySerialModel(uid_id=uid.id, company_serial_id=company_serial.id,
|
|
|
|
- add_time=now_time, update_time=now_time)
|
|
|
|
- uid_serial.save()
|
|
|
|
-
|
|
|
|
- company_serial.status = 2
|
|
|
|
- company_serial.save()
|
|
|
|
-
|
|
|
|
- dev = Device_Info.objects.filter(UID=uid.uid)
|
|
|
|
- if dev.exists():
|
|
|
|
- dev.update(serial_number=serial_number)
|
|
|
|
-
|
|
|
|
- full_uid_code = uid.uid__full_uid_code
|
|
|
|
- if uid.platform in CRCKey.keys():
|
|
|
|
- full_uid_code += ':'+CRCKey[uid.platform]
|
|
|
|
-
|
|
|
|
- res = {
|
|
|
|
- 'full_uid_code': CommonService.encode_data(full_uid_code),
|
|
|
|
- 'uid': CommonService.encode_data(uid.uid),
|
|
|
|
- 'mac': CommonService.encode_data(uid.mac),
|
|
|
|
- 'extra': uid.uid_extra,
|
|
|
|
- 'platform': uid.platform,
|
|
|
|
- 'initString': uid.init_string,
|
|
|
|
- 'initStringApp': uid.init_string_app,
|
|
|
|
- }
|
|
|
|
- return response.json(0, res)
|
|
|
|
- else:
|
|
|
|
- return response.json(375)
|
|
|
|
-
|
|
|
|
- return response.json(5)
|
|
|
|
- else:
|
|
|
|
- uid_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial.id)
|
|
|
|
- if uid_qs.exists():
|
|
|
|
- uid = uid_qs.values('uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__full_uid_code',
|
|
|
|
- 'uid__platform', 'uid__init_string', 'uid__init_string_app')[0]
|
|
|
|
- full_uid_code = uid['uid__full_uid_code']
|
|
|
|
- if uid['uid__platform'] in CRCKey.keys():
|
|
|
|
- full_uid_code += ':'+CRCKey[uid['uid__platform']]
|
|
|
|
- res = {
|
|
|
|
- 'full_uid_code': CommonService.encode_data(full_uid_code),
|
|
|
|
- 'uid': CommonService.encode_data(uid['uid__uid']),
|
|
|
|
- 'mac': CommonService.encode_data(uid['uid__mac']),
|
|
|
|
- 'extra': uid['uid__uid_extra'],
|
|
|
|
- 'platform': uid['uid__platform'],
|
|
|
|
- 'initString': uid['uid__init_string'],
|
|
|
|
- 'initStringApp': uid['uid__init_string_app'],
|
|
|
|
- }
|
|
|
|
- return response.json(0, res)
|
|
|
|
- else:
|
|
|
|
|
|
+ if not country_id:
|
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
|
+ country_id = Device_Region().get_device_region(ip)
|
|
|
|
+
|
|
|
|
+ # 判断序列号是否已和企业关联
|
|
|
|
+ company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_id, serial_number=serial)
|
|
|
|
+ if not company_serial_qs.exists():
|
|
|
|
+ return response.json(173)
|
|
|
|
+
|
|
|
|
+ # 当序列号已关联UID
|
|
|
|
+ company_serial = company_serial_qs[0]
|
|
|
|
+
|
|
|
|
+ if company_serial.status == 0: # 该序列号未绑定企业
|
|
|
|
+ return response.json(173)
|
|
|
|
+ elif company_serial.status == 1: # 绑定uid
|
|
|
|
+ count = 0
|
|
|
|
+ while count < 3:
|
|
|
|
+ p2p = SerialNumberModel.objects.filter(serial_number=serial).values('p2p')
|
|
|
|
+ print('此序列号的p2p类型:', p2p[0]['p2p'])
|
|
|
|
+ uid_qs = UIDModel.objects.filter(vpg__company_id=company_serial.company.id,
|
|
|
|
+ vpg__region_id=country_id,
|
|
|
|
+ status=0, p2p_type=p2p[0]['p2p']).order_by('id')[0:10]
|
|
|
|
+ # uid_qs:未进行绑定的uid列表
|
|
|
|
+ if not uid_qs.exists():
|
|
return response.json(173)
|
|
return response.json(173)
|
|
- except Exception as e:
|
|
|
|
- # print('--------------------------error 5000')
|
|
|
|
- # print(repr(e))
|
|
|
|
- if savePoint:
|
|
|
|
- transaction.rollback(savePoint)
|
|
|
|
- djangoLogger = logging.getLogger('django')
|
|
|
|
- djangoLogger.exception(repr(e))
|
|
|
|
- return response.json(176, str(e))
|
|
|
|
|
|
+ uid = uid_qs[0]
|
|
|
|
+ result = UIDModel.objects.filter(id=uid.id, status=0).\
|
|
|
|
+ update(**{'status': 2, 'update_time': int(time.time())})
|
|
|
|
+
|
|
|
|
+ if int(result) <= 0: # 更新失败
|
|
|
|
+ count += 1
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ uid_serial = UIDCompanySerialModel(uid_id=uid.id, company_serial_id=company_serial.id,
|
|
|
|
+ add_time=now_time, update_time=now_time)
|
|
|
|
+ uid_serial.save()
|
|
|
|
+
|
|
|
|
+ company_serial.status = 2
|
|
|
|
+ company_serial.save()
|
|
|
|
+
|
|
|
|
+ dev = Device_Info.objects.filter(UID=uid.uid)
|
|
|
|
+ if dev.exists():
|
|
|
|
+ dev.update(serial_number=serial_number)
|
|
|
|
+
|
|
|
|
+ full_uid_code = uid.uid__full_uid_code
|
|
|
|
+ if uid.platform in CRCKey.keys():
|
|
|
|
+ full_uid_code += ':'+CRCKey[uid.platform]
|
|
|
|
+
|
|
|
|
+ res = {
|
|
|
|
+ 'full_uid_code': CommonService.encode_data(full_uid_code),
|
|
|
|
+ 'uid': CommonService.encode_data(uid.uid),
|
|
|
|
+ 'mac': CommonService.encode_data(uid.mac),
|
|
|
|
+ 'extra': uid.uid_extra,
|
|
|
|
+ 'platform': uid.platform,
|
|
|
|
+ 'initString': uid.init_string,
|
|
|
|
+ 'initStringApp': uid.init_string_app,
|
|
|
|
+ }
|
|
|
|
+ return response.json(0, res)
|
|
|
|
+ return response.json(5)
|
|
|
|
+ else:
|
|
|
|
+ uid_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial.id)
|
|
|
|
+ if not uid_qs.exists():
|
|
|
|
+ return response.json(173)
|
|
|
|
+ uid = uid_qs.values('uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__full_uid_code',
|
|
|
|
+ 'uid__platform', 'uid__init_string', 'uid__init_string_app')[0]
|
|
|
|
+ full_uid_code = uid['uid__full_uid_code']
|
|
|
|
+ if uid['uid__platform'] in CRCKey.keys():
|
|
|
|
+ full_uid_code += ':'+CRCKey[uid['uid__platform']]
|
|
|
|
+ res = {
|
|
|
|
+ 'full_uid_code': CommonService.encode_data(full_uid_code),
|
|
|
|
+ 'uid': CommonService.encode_data(uid['uid__uid']),
|
|
|
|
+ 'mac': CommonService.encode_data(uid['uid__mac']),
|
|
|
|
+ 'extra': uid['uid__uid_extra'],
|
|
|
|
+ 'platform': uid['uid__platform'],
|
|
|
|
+ 'initString': uid['uid__init_string'],
|
|
|
|
+ 'initStringApp': uid['uid__init_string_app'],
|
|
|
|
+ }
|
|
|
|
+ return response.json(0, res)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- # print('--------------------------error 5001')
|
|
|
|
- # print(repr(e))
|
|
|
|
|
|
+ if savePoint:
|
|
|
|
+ transaction.rollback(savePoint)
|
|
djangoLogger = logging.getLogger('django')
|
|
djangoLogger = logging.getLogger('django')
|
|
djangoLogger.exception(repr(e))
|
|
djangoLogger.exception(repr(e))
|
|
return response.json(176, str(e))
|
|
return response.json(176, str(e))
|
|
-
|
|
|
|
- else:
|
|
|
|
- return response.json(444)
|
|
|
|
|
|
+ except Exception as e:
|
|
|
|
+ djangoLogger = logging.getLogger('django')
|
|
|
|
+ djangoLogger.exception(repr(e))
|
|
|
|
+ return response.json(176, str(e))
|
|
|
|
|
|
def do_get_uid(self, request_dict, response):
|
|
def do_get_uid(self, request_dict, response):
|
|
serial_number = request_dict.get('serial_number', None)
|
|
serial_number = request_dict.get('serial_number', None)
|
|
@@ -340,14 +325,13 @@ class SerialNumberView(View):
|
|
'mac': CommonService.encode_data(uid['uid__mac']),
|
|
'mac': CommonService.encode_data(uid['uid__mac']),
|
|
'extra': uid['uid__uid_extra']
|
|
'extra': uid['uid__uid_extra']
|
|
}
|
|
}
|
|
-
|
|
|
|
return response.json(0, res)
|
|
return response.json(0, res)
|
|
else:
|
|
else:
|
|
return response.json(173)
|
|
return response.json(173)
|
|
else:
|
|
else:
|
|
return response.json(444)
|
|
return response.json(444)
|
|
|
|
|
|
- def do_detach_uid(self, request_dict, response):
|
|
|
|
|
|
+ def do_detach_uid(self, request, request_dict, response):
|
|
serial_number = request_dict.get('serial_number', None)
|
|
serial_number = request_dict.get('serial_number', None)
|
|
token = request_dict.get('token', None)
|
|
token = request_dict.get('token', None)
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
@@ -369,8 +353,8 @@ class SerialNumberView(View):
|
|
return response.json(173)
|
|
return response.json(173)
|
|
uid_serial = uid_serial_qs[0]
|
|
uid_serial = uid_serial_qs[0]
|
|
|
|
|
|
|
|
+ # 删除iot设备信息表数据
|
|
iot = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
iot = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
- # iot = iotdeviceInfoModel.objects.filter(thing_name="Ansjer_Device_" + serial_number)
|
|
|
|
if iot.exists():
|
|
if iot.exists():
|
|
iot.delete()
|
|
iot.delete()
|
|
|
|
|
|
@@ -393,12 +377,22 @@ class SerialNumberView(View):
|
|
else:
|
|
else:
|
|
up_qs.delete()
|
|
up_qs.delete()
|
|
|
|
|
|
- uid_qs = UIDModel.objects.filter(uid=uid_serial.uid.uid)
|
|
|
|
- if uid_qs.exists():
|
|
|
|
- uid = uid_qs[0]
|
|
|
|
- uid.status = 0
|
|
|
|
- uid.save()
|
|
|
|
|
|
+ UIDModel.objects.filter(uid=uid_serial.uid.uid).update(status=0) # 重置uid的使用状态为未分配
|
|
uid_serial.delete()
|
|
uid_serial.delete()
|
|
|
|
+
|
|
|
|
+ # 记录操作日志
|
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
|
+ content = json.loads(json.dumps(request_dict))
|
|
|
|
+ log = {
|
|
|
|
+ 'ip': ip,
|
|
|
|
+ 'user': 0,
|
|
|
|
+ 'status': 200,
|
|
|
|
+ 'time': now_time,
|
|
|
|
+ 'operation': '解绑uid',
|
|
|
|
+ 'content': json.dumps(content),
|
|
|
|
+ 'url': 'serialNumber/detachUID',
|
|
|
|
+ }
|
|
|
|
+ LogModel.objects.create(**log)
|
|
return response.json(0)
|
|
return response.json(0)
|
|
|
|
|
|
def do_update(self, userID, request_dict, response):
|
|
def do_update(self, userID, request_dict, response):
|