|
@@ -8,7 +8,9 @@ import uuid
|
|
|
import boto3
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import Device_User, Device_Info, RegionCountryModel, iotdeviceInfoModel
|
|
|
+from Controller.DeviceConfirmRegion import Device_Region
|
|
|
+from Model.models import Device_User, Device_Info, iotdeviceInfoModel, UIDCompanySerialModel, \
|
|
|
+ SerialNumberModel
|
|
|
from Object.IOTCore.IotObject import IOTClient
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Service.CommonService import CommonService
|
|
@@ -20,64 +22,34 @@ class IotCoreView(View):
|
|
|
request.encoding = 'utf-8'
|
|
|
request_dict = request.GET
|
|
|
operation = kwargs.get('operation', None)
|
|
|
- return self.validate(operation, request_dict)
|
|
|
+ return self.validate(operation, request_dict, request)
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
request.encoding = 'utf-8'
|
|
|
request_dict = request.POST
|
|
|
operation = kwargs.get('operation', None)
|
|
|
- return self.validate(operation, request_dict)
|
|
|
+ return self.validate(operation, request_dict, request)
|
|
|
|
|
|
- def validate(self, operation, request_dict):
|
|
|
+ def validate(self, operation, request_dict, request):
|
|
|
|
|
|
response = ResponseObject()
|
|
|
|
|
|
- if operation == 'createProvisioningClaim':
|
|
|
- return self.create_provisioning_claim(request_dict, response)
|
|
|
- elif operation == 'createKeysAndCertificate':
|
|
|
- return self.create_keys_and_certificate(request_dict, response)
|
|
|
+ if operation == 'createKeysAndCertificate':
|
|
|
+ return self.create_keys_and_certificate(request_dict, response, request)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
|
- # 即时预置注册 :已放弃
|
|
|
- def create_provisioning_claim(self, request_dict, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- token = request_dict.get('token', None)
|
|
|
- time_stamp = request_dict.get('time_stamp', None)
|
|
|
-
|
|
|
- if uid and token and time_stamp:
|
|
|
- token = int(CommonService.decode_data(token))
|
|
|
- time_stamp = int(time_stamp)
|
|
|
-
|
|
|
- now_time = int(time.time())
|
|
|
- distance = now_time - time_stamp
|
|
|
-
|
|
|
- # if token != time_stamp and distance > 600: 暂时去掉延时
|
|
|
- if token != time_stamp :
|
|
|
- return response.json(404)
|
|
|
-
|
|
|
- region_country_qs = Device_Info.objects.filter(UID=uid).values('userID__region_country')
|
|
|
- if not region_country_qs.exists() or region_country_qs[0]['userID__region_country'] == 0 :
|
|
|
- return response.json(173)
|
|
|
-
|
|
|
- region_country_qs = RegionCountryModel.objects.filter(number=region_country_qs[0]['userID__region_country'])
|
|
|
- if region_country_qs.exists():
|
|
|
- user_region = region_country_qs[0]
|
|
|
- iotClient = IOTClient(user_region.region_id)
|
|
|
- return response.json(0, {'res': iotClient.create_provisioning_claim('Ansjer_Iot_Queue')})
|
|
|
- else:
|
|
|
- return response.json(444)
|
|
|
|
|
|
# CVM注册 :正使用
|
|
|
- def create_keys_and_certificate(self, request_dict, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- uid_code = request_dict.get('uid_code', None)
|
|
|
+ def create_keys_and_certificate(self, request_dict, response, request):
|
|
|
+ serial_number = request_dict.get('serial_number', None)
|
|
|
+ serial_number_code = request_dict.get('serial_number_code', None)
|
|
|
token = request_dict.get('token', None)
|
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
|
device_version = request_dict.get('device_version', None)
|
|
|
|
|
|
- if uid and token and time_stamp and uid_code:
|
|
|
- uid_code = CommonService.decode_data(uid_code)
|
|
|
+ if serial_number and token and time_stamp and serial_number_code:
|
|
|
+ uid_code = CommonService.decode_data(serial_number_code)
|
|
|
token = int(CommonService.decode_data(token))
|
|
|
time_stamp = int(time_stamp)
|
|
|
|
|
@@ -85,42 +57,44 @@ class IotCoreView(View):
|
|
|
distance = now_time - time_stamp
|
|
|
|
|
|
# if token != time_stamp and distance > 600: 暂时去掉延时
|
|
|
- # if token != time_stamp or uid != uid_code :
|
|
|
- # return response.json(404)
|
|
|
+ if token != time_stamp or serial_number != serial_number_code :
|
|
|
+ return response.json(404)
|
|
|
|
|
|
- region_country_qs = Device_Info.objects.filter(UID=uid).values('userID__region_country')
|
|
|
- if not region_country_qs.exists() or region_country_qs[0]['userID__region_country'] == 0 :
|
|
|
- return response.json(173)
|
|
|
+ serial = serial_number[0:6]
|
|
|
|
|
|
- iotqs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
+ iotqs = iotdeviceInfoModel.objects.filter(serial_number__serial_number=serial)
|
|
|
|
|
|
# 判断设备是否已注册证书
|
|
|
if not iotqs.exists():
|
|
|
- region_country_qs = RegionCountryModel.objects.filter(
|
|
|
- number=region_country_qs[0]['userID__region_country'])
|
|
|
- if region_country_qs.exists():
|
|
|
- user_region = region_country_qs[0]
|
|
|
-
|
|
|
- iotClient = IOTClient(user_region.region_id)
|
|
|
- res = iotClient.create_keys_and_certificate(uid, device_version)
|
|
|
- nowTime = int(time.time())
|
|
|
- token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(nowTime)).encode('utf-8')).hexdigest()
|
|
|
- Device_Info.objects.filter(UID=uid).update(endpoint=res[0]['endpoint'], token_iot_number=token_iot_number )
|
|
|
- iotdeviceInfoModel.objects.create(uid=uid, certificateId=res[0]['certificateId'],
|
|
|
- certificatePem=res[0]['certificatePem'],
|
|
|
- publicKey=res[0]['publicKey'],
|
|
|
- privateKey=res[0]['privateKey'],
|
|
|
- Thingname=res[1]['ThingName'])
|
|
|
- return response.json(0, {'res': res})
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
+ region_id = Device_Region().get_device_region(ip)
|
|
|
+
|
|
|
+ iotClient = IOTClient(region_id)
|
|
|
+ res = iotClient.create_keys_and_certificate(serial, device_version)
|
|
|
+ nowTime = int(time.time())
|
|
|
+ token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(nowTime)).encode('utf-8')).hexdigest()
|
|
|
+
|
|
|
+ sn = SerialNumberModel.objects.get(serial_number=serial)
|
|
|
+
|
|
|
+ iotdeviceInfoModel.objects.create(serial_number=sn,
|
|
|
+ endpoint=res[0]['endpoint'],
|
|
|
+ certificate_id=res[0]['certificateId'],
|
|
|
+ certificate_pem=res[0]['certificatePem'],
|
|
|
+ public_key=res[0]['publicKey'],
|
|
|
+ private_key=res[0]['privateKey'],
|
|
|
+ thing_name=res[1]['ThingName'],
|
|
|
+ token_iot_number=token_iot_number
|
|
|
+ )
|
|
|
+ return response.json(0, {'res': res})
|
|
|
else:
|
|
|
- dev_qs = Device_Info.objects.filter(UID=uid)
|
|
|
- iot = iotdeviceInfoModel.objects.get(uid=uid)
|
|
|
+
|
|
|
+ iot = iotqs[0]
|
|
|
res = {
|
|
|
- 'certificateId': iot.certificateId,
|
|
|
- 'certificatePem': iot.certificatePem,
|
|
|
- 'publicKey': iot.publicKey,
|
|
|
- 'privateKey': iot.privateKey,
|
|
|
- 'endpoint': dev_qs[0].endpoint
|
|
|
+ 'certificateId': iot.certificate_id,
|
|
|
+ 'certificatePem': iot.certificate_pem,
|
|
|
+ 'publicKey': iot.public_key,
|
|
|
+ 'privateKey': iot.private_key,
|
|
|
+ 'endpoint': iot.endpoint
|
|
|
}
|
|
|
# print('此设备已注册证书')
|
|
|
return response.json(0, {'res': res})
|