|
@@ -68,7 +68,8 @@ class IotCoreView(View):
|
|
return response.json(404)
|
|
return response.json(404)
|
|
|
|
|
|
# 设备注册到aws iot core
|
|
# 设备注册到aws iot core
|
|
- def create_keys_and_certificate(self, request_dict, response, request):
|
|
|
|
|
|
+ @staticmethod
|
|
|
|
+ def create_keys_and_certificate(request_dict, response, request):
|
|
logger = logging.getLogger('info')
|
|
logger = logging.getLogger('info')
|
|
logger.info('设备注册到aws iot core请求参数:{}'.format(request_dict))
|
|
logger.info('设备注册到aws iot core请求参数:{}'.format(request_dict))
|
|
token = request_dict.get('token', None)
|
|
token = request_dict.get('token', None)
|
|
@@ -80,8 +81,8 @@ class IotCoreView(View):
|
|
return response.json(444, {'param': 'token, language, time_stamp, device_version'})
|
|
return response.json(444, {'param': 'token, language, time_stamp, device_version'})
|
|
|
|
|
|
try:
|
|
try:
|
|
- no_rtc = request_dict.get('no_rtc', None)
|
|
|
|
# 时间戳token校验
|
|
# 时间戳token校验
|
|
|
|
+ no_rtc = request_dict.get('no_rtc', None)
|
|
if no_rtc:
|
|
if no_rtc:
|
|
if not CommonService.check_time_stamp_token_without_distance(token, time_stamp):
|
|
if not CommonService.check_time_stamp_token_without_distance(token, time_stamp):
|
|
return response.json(13)
|
|
return response.json(13)
|
|
@@ -92,8 +93,7 @@ class IotCoreView(View):
|
|
uid = request_dict.get('uid', '')
|
|
uid = request_dict.get('uid', '')
|
|
uid_code = request_dict.get('uid_code', None)
|
|
uid_code = request_dict.get('uid_code', None)
|
|
companyMark = '11A'
|
|
companyMark = '11A'
|
|
- if not uid:
|
|
|
|
- # 使用序列号
|
|
|
|
|
|
+ if not uid: # 传序列号
|
|
serial_number = request_dict.get('serial_number', None)
|
|
serial_number = request_dict.get('serial_number', None)
|
|
serial_number_code = request_dict.get('serial_number_code', None)
|
|
serial_number_code = request_dict.get('serial_number_code', None)
|
|
if not all([serial_number, serial_number_code]):
|
|
if not all([serial_number, serial_number_code]):
|
|
@@ -112,9 +112,8 @@ class IotCoreView(View):
|
|
return response.json(444)
|
|
return response.json(444)
|
|
|
|
|
|
ThingNameSuffix = serial_number # 物品名后缀
|
|
ThingNameSuffix = serial_number # 物品名后缀
|
|
- iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
|
- else:
|
|
|
|
- # 使用uid
|
|
|
|
|
|
+ iot_device_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
|
+ else: # 传uid
|
|
# uid编码解码校验
|
|
# uid编码解码校验
|
|
uid_code = CommonService.decode_data(uid_code)
|
|
uid_code = CommonService.decode_data(uid_code)
|
|
if uid != uid_code:
|
|
if uid != uid_code:
|
|
@@ -122,16 +121,26 @@ class IotCoreView(View):
|
|
|
|
|
|
serial = '' # iot_deviceInfo表写入serial_number为''
|
|
serial = '' # iot_deviceInfo表写入serial_number为''
|
|
ThingNameSuffix = uid # 物品名后缀
|
|
ThingNameSuffix = uid # 物品名后缀
|
|
- iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
|
- # 判断设备是否已注册证书
|
|
|
|
- if not iotdeviceInfo_qs.exists():
|
|
|
|
- # 设备模拟国外环境测试
|
|
|
|
- # if SERVER_TYPE == 'Ansjer.us_config.formal_settings': # 国外正式配置使用固定ip进行测试
|
|
|
|
- # ip = '67.220.90.13'
|
|
|
|
- # else:
|
|
|
|
- # ip = CommonService.get_ip_address(request)
|
|
|
|
- ip = CommonService.get_ip_address(request)
|
|
|
|
- region_id = Device_Region().get_device_region(ip)
|
|
|
|
|
|
+ iot_device_info_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
|
+ # 判断设备是否已注册过
|
|
|
|
+ if iot_device_info_qs.exists():
|
|
|
|
+ iot = iot_device_info_qs[0]
|
|
|
|
+ res = {
|
|
|
|
+ 'certificateId': iot.certificate_id,
|
|
|
|
+ 'certificatePem': iot.certificate_pem,
|
|
|
|
+ 'publicKey': iot.public_key,
|
|
|
|
+ 'privateKey': iot.private_key,
|
|
|
|
+ 'endpoint': iot.endpoint
|
|
|
|
+ }
|
|
|
|
+ return response.json(0, {'res': res})
|
|
|
|
+ else:
|
|
|
|
+ # 根据地区id确定使用的boto3 client
|
|
|
|
+ region_id = request_dict.get('region_id', None)
|
|
|
|
+ if region_id:
|
|
|
|
+ region_id = int(region_id)
|
|
|
|
+ else:
|
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
|
+ region_id = Device_Region().get_device_region(ip)
|
|
iotClient = IOTClient(region_id)
|
|
iotClient = IOTClient(region_id)
|
|
|
|
|
|
# 拼接物品名
|
|
# 拼接物品名
|
|
@@ -164,16 +173,6 @@ class IotCoreView(View):
|
|
'endpoint': res[0]['endpoint']
|
|
'endpoint': res[0]['endpoint']
|
|
}
|
|
}
|
|
return response.json(0, {'res': res})
|
|
return response.json(0, {'res': res})
|
|
- else:
|
|
|
|
- iot = iotdeviceInfo_qs[0]
|
|
|
|
- res = {
|
|
|
|
- 'certificateId': iot.certificate_id,
|
|
|
|
- 'certificatePem': iot.certificate_pem,
|
|
|
|
- 'publicKey': iot.public_key,
|
|
|
|
- 'privateKey': iot.private_key,
|
|
|
|
- 'endpoint': iot.endpoint
|
|
|
|
- }
|
|
|
|
- return response.json(0, {'res': res})
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
return response.json(500, repr(e))
|
|
return response.json(500, repr(e))
|