Przeglądaj źródła

新增物品重新分组接口

locky 4 lat temu
rodzic
commit
69dc9548da
1 zmienionych plików z 63 dodań i 1 usunięć
  1. 63 1
      Controller/IotCoreController.py

+ 63 - 1
Controller/IotCoreController.py

@@ -36,6 +36,8 @@ class IotCoreView(View):
 
         if operation == 'createKeysAndCertificate':
             return self.create_keys_and_certificate(request_dict, response, request)
+        elif operation == 'thingRegroup':
+            return self.thing_regroup(request_dict, response, request)
         else:
             return response.json(404)
 
@@ -108,4 +110,64 @@ class IotCoreView(View):
                 # print('此设备已注册证书')
                 return response.json(0, {'res': res})
         else:
-            return response.json(444)
+            return response.json(444)
+
+    def thing_regroup(self, request_dict, response, request):
+        # 物品重新分组
+        # token = request_dict.get('token', None)
+        # time_stamp = request_dict.get('time_stamp', None)
+        serial_number = request_dict.get('serial_number', None)
+        device_version = request_dict.get('device_version', None)
+        language = request_dict.get('language', None)
+        if not all([serial_number, device_version, language]):
+            return response.json(444)
+
+        # 封装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 and distance > 600: 暂时去掉延时
+        # if token != time_stamp or serial_number != serial_number_code :
+        #     return response.json(404)
+        # ip = CommonService.get_ip_address(request)
+        # region_id = Device_Region().get_device_region(ip)
+
+        thingName = 'Ansjer_Device_' + serial_number
+        new_thingGroupName = (device_version + '_' + language).replace('.', '_')    # 物品组命名不能包含'.'
+        # 调试参数
+        # thingName = 'Ansjer_Device_00EBEX'
+        # new_thingGroupName = 'C1Pro_V1_0_1_cn'
+        region_id = 1
+        iotClient = IOTClient(region_id)
+
+        try:
+            # 获取旧物品组
+            list_groups_res = iotClient.client.list_thing_groups_for_thing(thingName=thingName, maxResults=1)
+            old_thingGroupName = list_groups_res['thingGroups'][0]['groupName']
+            # 没有新物品组则创建
+            list_thing_groups_res = iotClient.client.list_thing_groups(namePrefixFilter=new_thingGroupName
+                                                                       , maxResults=1, recursive=False)
+            if not list_thing_groups_res['thingGroups']:
+                attributes = {
+                    "update_time": "0"
+                }
+                thingGroupProperties = {
+                    "thingGroupDescription": "OTA",
+                    "attributePayload": {
+                        "attributes": attributes,
+                        "merge": False  # 更新时覆盖掉而不是合并
+                    }
+                }
+                iotClient.client.create_thing_group(thingGroupName=new_thingGroupName
+                                                    , thingGroupProperties=thingGroupProperties)
+            iotClient.client.update_thing_groups_for_thing(thingName=thingName
+                                                           , thingGroupsToAdd=[new_thingGroupName]
+                                                           , thingGroupsToRemove=[old_thingGroupName])
+            # 更新设备版本信息
+            Device_Info.objects.filter(serial_number=serial_number).update(version=device_version)
+
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, repr(e))