|
@@ -64,71 +64,94 @@ class IOTClient(IOTObject):
|
|
|
}
|
|
|
return res
|
|
|
|
|
|
- def create_keys_and_certificate(self, serial_number, device_version):
|
|
|
- result = self.client.create_keys_and_certificate(setAsActive=True)
|
|
|
- res = {
|
|
|
- 'certificateId': result['certificateId'],
|
|
|
- 'certificatePem': result['certificatePem'],
|
|
|
- 'publicKey': result['keyPair']['PublicKey'],
|
|
|
- 'privateKey': result['keyPair']['PrivateKey'],
|
|
|
- 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
|
|
|
- }
|
|
|
- # 根据证书ID注册物品和策略
|
|
|
- templateBody = {
|
|
|
- "Parameters": {
|
|
|
- "ThingName": {
|
|
|
- "Type": "String"
|
|
|
- },
|
|
|
- "SerialNumber": {
|
|
|
- "Type": "String"
|
|
|
- },
|
|
|
- "DeviceLocation": {
|
|
|
- "Type": "String"
|
|
|
- },
|
|
|
- "AWS::IoT::Certificate::Id": {
|
|
|
- "Type": "String"
|
|
|
+ def create_keys_and_certificate(self, serial_number, thingGroup):
|
|
|
+ try:
|
|
|
+ result = self.client.create_keys_and_certificate(setAsActive=True)
|
|
|
+ res = {
|
|
|
+ 'certificateId': result['certificateId'],
|
|
|
+ 'certificatePem': result['certificatePem'],
|
|
|
+ 'publicKey': result['keyPair']['PublicKey'],
|
|
|
+ 'privateKey': result['keyPair']['PrivateKey'],
|
|
|
+ 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
|
|
|
+ }
|
|
|
+ # 搜索是否存在该物品组
|
|
|
+ thing_groups_res = self.client.list_thing_groups(nextToken='', maxResults=1,
|
|
|
+ namePrefixFilter=thingGroup, recursive=False)
|
|
|
+ if thing_groups_res['thingGroups']:
|
|
|
+ thingGroupName = thing_groups_res['thingGroups'][0]['groupName'] # 获取物品组名称
|
|
|
+ else:
|
|
|
+ attributes = {
|
|
|
+ "update_time": "0"
|
|
|
}
|
|
|
- },
|
|
|
- "Resources": {
|
|
|
- "thing": {
|
|
|
- "Type": "AWS::IoT::Thing",
|
|
|
- "Properties": {
|
|
|
- "AttributePayload": {},
|
|
|
- # "ThingGroups" : ["v1-lightbulbs", {"Ref" : "DeviceLocation"}],
|
|
|
- "ThingName": {
|
|
|
- "Ref": "ThingName"
|
|
|
- },
|
|
|
- "ThingGroups": []
|
|
|
- },
|
|
|
- "OverrideSettings": {
|
|
|
- "AttributePayload": "MERGE",
|
|
|
- "ThingTypeName": "REPLACE",
|
|
|
- "ThingGroups": "DO_NOTHING"
|
|
|
+ thingGroupProperties = {
|
|
|
+ "thingGroupDescription": "OTA",
|
|
|
+ "attributePayload": {
|
|
|
+ "attributes": attributes,
|
|
|
+ "merge": False # 更新时覆盖掉而不是合并
|
|
|
}
|
|
|
- },
|
|
|
- "certificate": {
|
|
|
- "Type": "AWS::IoT::Certificate",
|
|
|
- "Properties": {
|
|
|
- "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
|
|
|
- "Status": "Active"
|
|
|
+ }
|
|
|
+ create_thing_group_res = self.client.create_thing_group(thingGroupName=thingGroup,
|
|
|
+ thingGroupProperties=thingGroupProperties)
|
|
|
+ thingGroupName = create_thing_group_res['thingGroupName'] # 获取物品组名称
|
|
|
+ print('物品组:', thingGroupName)
|
|
|
+ # 根据证书ID注册物品和策略
|
|
|
+ templateBody = {
|
|
|
+ "Parameters": {
|
|
|
+ "ThingName": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "SerialNumber": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "thingGroupName": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "AWS::IoT::Certificate::Id": {
|
|
|
+ "Type": "String"
|
|
|
}
|
|
|
},
|
|
|
- "policy": {
|
|
|
- "Properties": {
|
|
|
- "PolicyName": "My_Iot_Policy"
|
|
|
+ "Resources": {
|
|
|
+ "thing": {
|
|
|
+ "Type": "AWS::IoT::Thing",
|
|
|
+ "Properties": {
|
|
|
+ "AttributePayload": {},
|
|
|
+ # "ThingGroups" : ["v1-lightbulbs", {"Ref" : "DeviceLocation"}],
|
|
|
+ "ThingName": {
|
|
|
+ "Ref": "ThingName"
|
|
|
+ },
|
|
|
+ "ThingGroups": [{"Ref": "thingGroupName"}]
|
|
|
+ },
|
|
|
+ "OverrideSettings": {
|
|
|
+ "AttributePayload": "MERGE",
|
|
|
+ "ThingTypeName": "REPLACE",
|
|
|
+ "ThingGroups": "DO_NOTHING"
|
|
|
+ }
|
|
|
},
|
|
|
- "Type": "AWS::IoT::Policy"
|
|
|
- },
|
|
|
+ "certificate": {
|
|
|
+ "Type": "AWS::IoT::Certificate",
|
|
|
+ "Properties": {
|
|
|
+ "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
|
|
|
+ "Status": "Active"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "policy": {
|
|
|
+ "Properties": {
|
|
|
+ "PolicyName": "My_Iot_Policy"
|
|
|
+ },
|
|
|
+ "Type": "AWS::IoT::Policy"
|
|
|
+ },
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- templateBody = json.dumps(templateBody)
|
|
|
- parameters = {"ThingName": "Ansjer_Device_" + serial_number,
|
|
|
- #"DeviceLocation": device_version,
|
|
|
- "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
- self.client.register_thing(
|
|
|
- templateBody=templateBody,
|
|
|
- parameters=parameters
|
|
|
- )
|
|
|
- return res, parameters
|
|
|
|
|
|
+ templateBody = json.dumps(templateBody)
|
|
|
+ parameters = {"ThingName": "Ansjer_Device_" + serial_number,
|
|
|
+ "thingGroupName": thingGroupName,
|
|
|
+ "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
+ self.client.register_thing(
|
|
|
+ templateBody=templateBody,
|
|
|
+ parameters=parameters
|
|
|
+ )
|
|
|
+ return res, parameters
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ # return response.json(500, repr(e))
|