Browse Source

Merge branch 'test' of http://192.168.136.99:3000/servers/ASJServer into test

lang 3 years ago
parent
commit
1b19f8fa12

+ 27 - 11
Controller/IotCoreController.py

@@ -67,23 +67,31 @@ class IotCoreView(View):
             else:
                 return response.json(404)
 
-    # CVM注册  :正使用
+    # 设备注册到aws iot core
     def create_keys_and_certificate(self, request_dict, response, request):
-        uid = request_dict.get('uid', '')
+        logger = logging.getLogger('info')
+        logger.info('设备注册到aws iot core请求参数:{}'.format(request_dict))
         token = request_dict.get('token', None)
-        uid_code = request_dict.get('uid_code', None)
         language = request_dict.get('language', None)
         time_stamp = request_dict.get('time_stamp', None)
         device_version = request_dict.get('device_version', None).replace('.', '_')  # 物品组命名不能包含'.'
 
-        if not all([token, time_stamp, device_version, language]):
-            return response.json(444, {'param': 'token, uid_code, time_stamp, device_version, language'})
+        if not all([token, language, time_stamp, device_version]):
+            return response.json(444, {'param': 'token, language, time_stamp, device_version'})
 
         try:
+            no_rtc = request_dict.get('no_rtc', None)
             # 时间戳token校验
-            if not CommonService.check_time_stamp_token(token, time_stamp):
-                return response.json(13)
+            if no_rtc:
+                if not CommonService.check_time_stamp_token_without_distance(token, time_stamp):
+                    return response.json(13)
+            else:
+                if not CommonService.check_time_stamp_token(token, time_stamp):
+                    return response.json(13)
 
+            uid = request_dict.get('uid', '')
+            uid_code = request_dict.get('uid_code', None)
+            companyMark = '11A'
             if not uid:
                 # 使用序列号
                 serial_number = request_dict.get('serial_number', None)
@@ -97,6 +105,7 @@ class IotCoreView(View):
                     return response.json(404)
 
                 serial = serial_number[0:6]
+                companyMark = serial_number[-3:]
                 try:
                     SerialNumberModel.objects.get(serial_number=serial)
                 except:
@@ -116,7 +125,6 @@ class IotCoreView(View):
                 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'
@@ -124,11 +132,19 @@ class IotCoreView(View):
                 #     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()
 
+                # 拼接物品名
+                if companyMark == '11A':
+                    ThingName = 'Ansjer_Device_' + ThingNameSuffix
+                elif companyMark == '11L':
+                    ThingName = 'Loocam_Device_' + ThingNameSuffix
+                else:
+                    ThingName = ThingNameSuffix
+                thingGroup = device_version + '_' + language
+                res = iotClient.register_to_iot_core(ThingName, 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'],

+ 2 - 2
Object/IOTCore/IotObject.py

@@ -58,7 +58,7 @@ class IOTClient:
         }
         return res
 
-    def register_to_iot_core(self, ThingNameSuffix, thingGroup, response):
+    def register_to_iot_core(self, ThingName, thingGroup, response):
         try:
             result = self.client.create_keys_and_certificate(setAsActive=True)
             res = {
@@ -136,7 +136,7 @@ class IOTClient:
                     },
                 }
             }
-            ThingName = 'Ansjer_Device_' + ThingNameSuffix
+
             templateBody = json.dumps(templateBody)
             parameters = {"ThingName": ThingName,
                           "thingGroupName": thingGroupName,

+ 1 - 1
SensorGateway/SensorGatewayController.py

@@ -51,7 +51,7 @@ class SensorGateway(View):
             return response.json(444, {'param': 'time_stamp, time_stamp_token'})
         try:
             # 时间戳token校验
-            if not CommonService.check_time_stamp_token_without_distance(time_stamp, time_stamp_token):
+            if not CommonService.check_time_stamp_token_without_distance(time_stamp_token, time_stamp):
                 return response.json(13)
 
             sensor_id = ''.join(random.sample(string.ascii_letters + string.digits, 6))

+ 2 - 2
Service/CommonService.py

@@ -455,7 +455,7 @@ class CommonService:
             return False
 
     @staticmethod
-    def check_time_stamp_token_without_distance(time_stamp, time_stamp_token):
+    def check_time_stamp_token_without_distance(time_stamp_token, time_stamp):
         """
         用于没有RTC设备的时间戳token校验
         @param time_stamp: 时间戳
@@ -463,7 +463,7 @@ class CommonService:
         @return: boolean True/False
         """
 
-        if not all([time_stamp, time_stamp_token]):
+        if not all([time_stamp_token, time_stamp]):
             return False
         try:
             token = CommonService.decode_data(time_stamp_token)