|
@@ -79,87 +79,88 @@ class IotCoreView(View):
|
|
|
if not all([token, time_stamp, device_version, language]):
|
|
|
return response.json(444, {'param': 'token, uid_code, time_stamp, device_version, language'})
|
|
|
|
|
|
- # 时间戳token校验
|
|
|
- if not CommonService.check_time_stamp_token(token, time_stamp):
|
|
|
- return response.json(13)
|
|
|
-
|
|
|
- if not uid:
|
|
|
- # 使用序列号
|
|
|
- serial_number = request_dict.get('serial_number', None)
|
|
|
- serial_number_code = request_dict.get('serial_number_code', None)
|
|
|
- if not all([serial_number, serial_number_code]):
|
|
|
- return response.json(444, {'param': 'serial_number, serial_number_code'})
|
|
|
-
|
|
|
- # 序列号编码解码校验
|
|
|
- serial_number_code = CommonService.decode_data(serial_number_code)
|
|
|
- if serial_number != serial_number_code:
|
|
|
- return response.json(404)
|
|
|
-
|
|
|
- serial = serial_number[0:6]
|
|
|
- try:
|
|
|
- SerialNumberModel.objects.get(serial_number=serial)
|
|
|
- except:
|
|
|
- return response.json(444)
|
|
|
-
|
|
|
- ThingNameSuffix = serial_number # 物品名后缀
|
|
|
- iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
- else:
|
|
|
- # 使用uid
|
|
|
- # uid编码解码校验
|
|
|
- uid_code = CommonService.decode_data(uid_code)
|
|
|
- if uid != uid_code:
|
|
|
- return response.json(404)
|
|
|
-
|
|
|
- serial = '' # iot_deviceInfo表写入serial_number为''
|
|
|
- ThingNameSuffix = uid # 物品名后缀
|
|
|
- iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
- # 判断设备是否已注册证书
|
|
|
- if not iotdeviceInfo_qs.exists():
|
|
|
- thingGroup = device_version + '_' + language
|
|
|
- # 设备模拟国外环境测试
|
|
|
- # 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)
|
|
|
- logger = logging.getLogger('info')
|
|
|
- logger.info('---设备注册到IoT Core接口--- ip:{},region_id:{}'.format(ip, region_id))
|
|
|
-
|
|
|
- iotClient = IOTClient(region_id)
|
|
|
- res = iotClient.register_to_iot_core(ThingNameSuffix, thingGroup, response)
|
|
|
- token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
|
|
|
-
|
|
|
- iotdeviceInfoModel.objects.create(uid=uid,
|
|
|
- serial_number=serial,
|
|
|
- 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'],
|
|
|
- thing_groups=res[1]['thingGroupName'],
|
|
|
- token_iot_number=token_iot_number
|
|
|
- )
|
|
|
- res = {
|
|
|
- 'certificateId': res[0]['certificateId'],
|
|
|
- 'certificatePem': res[0]['certificatePem'],
|
|
|
- 'publicKey': res[0]['publicKey'],
|
|
|
- 'privateKey': res[0]['privateKey'],
|
|
|
- 'endpoint': res[0]['endpoint']
|
|
|
- }
|
|
|
- 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
|
|
|
- }
|
|
|
- # print('此设备已注册证书')
|
|
|
- return response.json(0, {'res': res})
|
|
|
+ try:
|
|
|
+ # 时间戳token校验
|
|
|
+ if not CommonService.check_time_stamp_token(token, time_stamp):
|
|
|
+ return response.json(13)
|
|
|
+
|
|
|
+ if not uid:
|
|
|
+ # 使用序列号
|
|
|
+ serial_number = request_dict.get('serial_number', None)
|
|
|
+ serial_number_code = request_dict.get('serial_number_code', None)
|
|
|
+ if not all([serial_number, serial_number_code]):
|
|
|
+ return response.json(444, {'param': 'serial_number, serial_number_code'})
|
|
|
+
|
|
|
+ # 序列号编码解码校验
|
|
|
+ serial_number_code = CommonService.decode_data(serial_number_code)
|
|
|
+ if serial_number != serial_number_code:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ serial = serial_number[0:6]
|
|
|
+ try:
|
|
|
+ SerialNumberModel.objects.get(serial_number=serial)
|
|
|
+ except:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ ThingNameSuffix = serial_number # 物品名后缀
|
|
|
+ iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
+ else:
|
|
|
+ # 使用uid
|
|
|
+ # uid编码解码校验
|
|
|
+ uid_code = CommonService.decode_data(uid_code)
|
|
|
+ if uid != uid_code:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ serial = '' # iot_deviceInfo表写入serial_number为''
|
|
|
+ ThingNameSuffix = uid # 物品名后缀
|
|
|
+ iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
+ # 判断设备是否已注册证书
|
|
|
+ if not iotdeviceInfo_qs.exists():
|
|
|
+ thingGroup = device_version + '_' + language
|
|
|
+ # 设备模拟国外环境测试
|
|
|
+ # 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)
|
|
|
+
|
|
|
+ iotClient = IOTClient(region_id)
|
|
|
+ res = iotClient.register_to_iot_core(ThingNameSuffix, thingGroup, response)
|
|
|
+ token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
|
|
|
+
|
|
|
+ iotdeviceInfoModel.objects.create(uid=uid,
|
|
|
+ serial_number=serial,
|
|
|
+ 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'],
|
|
|
+ thing_groups=res[1]['thingGroupName'],
|
|
|
+ token_iot_number=token_iot_number
|
|
|
+ )
|
|
|
+ res = {
|
|
|
+ 'certificateId': res[0]['certificateId'],
|
|
|
+ 'certificatePem': res[0]['certificatePem'],
|
|
|
+ 'publicKey': res[0]['publicKey'],
|
|
|
+ 'privateKey': res[0]['privateKey'],
|
|
|
+ 'endpoint': res[0]['endpoint']
|
|
|
+ }
|
|
|
+ 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:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
|
|
def thing_regroup(self, request_dict, response, request):
|
|
|
# 物品重新分组
|