|
@@ -56,76 +56,93 @@ class IotCoreView(View):
|
|
|
|
|
|
# CVM注册 :正使用
|
|
|
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)
|
|
|
+ uid = request_dict.get('uid', '')
|
|
|
token = request_dict.get('token', None)
|
|
|
+ uid_code = request_dict.get('uid_code', '')
|
|
|
+ language = request_dict.get('language', None)
|
|
|
time_stamp = request_dict.get('time_stamp', None)
|
|
|
device_version = request_dict.get('device_version', None).replace('.', '_') # 物品组命名不能包含'.'
|
|
|
- language = request_dict.get('language', None)
|
|
|
|
|
|
- if serial_number and token and time_stamp and serial_number_code and device_version and language:
|
|
|
- serial_number_code = CommonService.decode_data(serial_number_code)
|
|
|
- token = int(CommonService.decode_data(token))
|
|
|
- time_stamp = int(time_stamp)
|
|
|
+ if not all([token, time_stamp, device_version, language]):
|
|
|
+ return response.json(444, {'param': 'token, time_stamp, device_version, language'})
|
|
|
|
|
|
- now_time = int(time.time())
|
|
|
- distance = now_time - time_stamp
|
|
|
- thingGroup = device_version + '_' + language
|
|
|
+ # token时间戳校验
|
|
|
+ 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 or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ 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'})
|
|
|
|
|
|
- if token != time_stamp or distance > 60000 or distance < -60000 or 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)
|
|
|
|
|
|
- iotqs = iotdeviceInfoModel.objects.filter(serial_number=serial)
|
|
|
-
|
|
|
- # 判断设备是否已注册证书
|
|
|
- if not iotqs.exists():
|
|
|
- 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_number, thingGroup, response)
|
|
|
- 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)
|
|
|
- try:
|
|
|
- sn = SerialNumberModel.objects.get(serial_number=serial)
|
|
|
- except:
|
|
|
- return response.json(444)
|
|
|
-
|
|
|
- iotdeviceInfoModel.objects.create(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'],
|
|
|
- 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:
|
|
|
+ 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)
|
|
|
|
|
|
- iot = iotqs[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})
|
|
|
+ serial = ''
|
|
|
+ ThingNameSuffix = uid # 物品名后缀
|
|
|
+ iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=uid)
|
|
|
+ # 判断设备是否已注册证书
|
|
|
+ if not iotdeviceInfo_qs.exists():
|
|
|
+ thingGroup = device_version + '_' + language
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
+ region_id = Device_Region().get_device_region(ip)
|
|
|
+
|
|
|
+ iotClient = IOTClient(region_id)
|
|
|
+ res = iotClient.create_keys_and_certificate(ThingNameSuffix, thingGroup, response)
|
|
|
+ token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(now_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'],
|
|
|
+ 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:
|
|
|
- return response.json(444)
|
|
|
+ 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})
|
|
|
|
|
|
def thing_regroup(self, request_dict, response, request):
|
|
|
# 物品重新分组
|