|
@@ -1,5 +1,6 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+import json
|
|
|
from abc import ABCMeta,abstractmethod
|
|
|
|
|
|
import boto3
|
|
@@ -15,6 +16,10 @@ class IOTObject(metaclass=ABCMeta):
|
|
|
def create_provisioning_claim(self, templateName):
|
|
|
pass
|
|
|
|
|
|
+ @abstractmethod
|
|
|
+ def create_keys_and_certificate(self, uid):
|
|
|
+ pass
|
|
|
+
|
|
|
|
|
|
class ChinaIOTClient(IOTObject):
|
|
|
|
|
@@ -34,7 +39,72 @@ class ChinaIOTClient(IOTObject):
|
|
|
'privateKey': result['keyPair']['PrivateKey'],
|
|
|
'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
|
|
|
}
|
|
|
+ return res
|
|
|
+
|
|
|
+ def create_keys_and_certificate(self, uid):
|
|
|
+ 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"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "Resources": {
|
|
|
+ "thing": {
|
|
|
+ "Type": "AWS::IoT::Thing",
|
|
|
+ "Properties": {
|
|
|
+ "AttributePayload": {},
|
|
|
+ "ThingGroups": [],
|
|
|
+ "ThingName": {
|
|
|
+ "Ref": "ThingName"
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "OverrideSettings": {
|
|
|
+ "AttributePayload": "MERGE",
|
|
|
+ "ThingTypeName": "REPLACE",
|
|
|
+ "ThingGroups": "DO_NOTHING"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "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_" + uid,
|
|
|
+ "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
+ self.client.register_thing(
|
|
|
+ templateBody=templateBody,
|
|
|
+ parameters=parameters
|
|
|
+ )
|
|
|
return res
|
|
|
|
|
|
|
|
@@ -56,7 +126,72 @@ class AmericaIOTClient(IOTObject):
|
|
|
'privateKey': result['keyPair']['PrivateKey'],
|
|
|
'endpoint': 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
|
|
|
}
|
|
|
+ return res
|
|
|
|
|
|
+ def create_keys_and_certificate(self, uid):
|
|
|
+ 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': 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
|
|
|
+ }
|
|
|
+ # 根据证书ID注册物品和策略
|
|
|
+ templateBody = {
|
|
|
+ "Parameters": {
|
|
|
+ "ThingName": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "SerialNumber": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "DeviceLocation": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "AWS::IoT::Certificate::Id": {
|
|
|
+ "Type": "String"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "Resources": {
|
|
|
+ "thing": {
|
|
|
+ "Type": "AWS::IoT::Thing",
|
|
|
+ "Properties": {
|
|
|
+ "AttributePayload": {},
|
|
|
+ "ThingGroups": [],
|
|
|
+ "ThingName": {
|
|
|
+ "Ref": "ThingName"
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "OverrideSettings": {
|
|
|
+ "AttributePayload": "MERGE",
|
|
|
+ "ThingTypeName": "REPLACE",
|
|
|
+ "ThingGroups": "DO_NOTHING"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "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_" + uid,
|
|
|
+ "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
+ self.client.register_thing(
|
|
|
+ templateBody=templateBody,
|
|
|
+ parameters=parameters
|
|
|
+ )
|
|
|
return res
|
|
|
|
|
|
|
|
@@ -77,7 +212,72 @@ class AsiaIOTClient(IOTObject):
|
|
|
'privateKey': result['keyPair']['PrivateKey'],
|
|
|
'endpoint': 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
|
|
|
}
|
|
|
+ return res
|
|
|
+
|
|
|
+ def create_keys_and_certificate(self, uid):
|
|
|
+ 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': 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
|
|
|
+ }
|
|
|
+ # 根据证书ID注册物品和策略
|
|
|
+ templateBody = {
|
|
|
+ "Parameters": {
|
|
|
+ "ThingName": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "SerialNumber": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "DeviceLocation": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "AWS::IoT::Certificate::Id": {
|
|
|
+ "Type": "String"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "Resources": {
|
|
|
+ "thing": {
|
|
|
+ "Type": "AWS::IoT::Thing",
|
|
|
+ "Properties": {
|
|
|
+ "AttributePayload": {},
|
|
|
+ "ThingGroups": [],
|
|
|
+ "ThingName": {
|
|
|
+ "Ref": "ThingName"
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "OverrideSettings": {
|
|
|
+ "AttributePayload": "MERGE",
|
|
|
+ "ThingTypeName": "REPLACE",
|
|
|
+ "ThingGroups": "DO_NOTHING"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "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_" + uid,
|
|
|
+ "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
+ self.client.register_thing(
|
|
|
+ templateBody=templateBody,
|
|
|
+ parameters=parameters
|
|
|
+ )
|
|
|
return res
|
|
|
|
|
|
|
|
@@ -98,5 +298,70 @@ class EuropeIOTClient(IOTObject):
|
|
|
'privateKey': result['keyPair']['PrivateKey'],
|
|
|
'endpoint': 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
|
|
|
}
|
|
|
+ return res
|
|
|
+
|
|
|
+ def create_keys_and_certificate(self, uid):
|
|
|
+ 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': 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
|
|
|
+ }
|
|
|
+ # 根据证书ID注册物品和策略
|
|
|
+ templateBody = {
|
|
|
+ "Parameters": {
|
|
|
+ "ThingName": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "SerialNumber": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "DeviceLocation": {
|
|
|
+ "Type": "String"
|
|
|
+ },
|
|
|
+ "AWS::IoT::Certificate::Id": {
|
|
|
+ "Type": "String"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "Resources": {
|
|
|
+ "thing": {
|
|
|
+ "Type": "AWS::IoT::Thing",
|
|
|
+ "Properties": {
|
|
|
+ "AttributePayload": {},
|
|
|
+ "ThingGroups": [],
|
|
|
+ "ThingName": {
|
|
|
+ "Ref": "ThingName"
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "OverrideSettings": {
|
|
|
+ "AttributePayload": "MERGE",
|
|
|
+ "ThingTypeName": "REPLACE",
|
|
|
+ "ThingGroups": "DO_NOTHING"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "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_" + uid,
|
|
|
+ "AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
+ self.client.register_thing(
|
|
|
+ templateBody=templateBody,
|
|
|
+ parameters=parameters
|
|
|
+ )
|
|
|
return res
|