|
@@ -21,99 +21,35 @@ class IOTObject(metaclass=ABCMeta):
|
|
|
pass
|
|
|
|
|
|
|
|
|
-class ChinaIOTClient(IOTObject):
|
|
|
+class IOTClient(IOTObject):
|
|
|
|
|
|
- def __init__(self):
|
|
|
- self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_CHINA_REGION,
|
|
|
- aws_access_key_id=AWS_IOT_SES_ACCESS_CHINA_ID,
|
|
|
- aws_secret_access_key=AWS_IOT_SES_ACCESS_CHINA_SECRET)
|
|
|
+ def __init__(self, region_id = 1):
|
|
|
|
|
|
- def create_provisioning_claim(self, templateName):
|
|
|
+ if region_id == 1:
|
|
|
+ self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_CHINA_REGION,
|
|
|
+ aws_access_key_id=AWS_IOT_SES_ACCESS_CHINA_ID,
|
|
|
+ aws_secret_access_key=AWS_IOT_SES_ACCESS_CHINA_SECRET)
|
|
|
+ self.endpoint = 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
|
|
|
|
|
|
- result = self.client.create_provisioning_claim(templateName=templateName)
|
|
|
+ if region_id == 2:
|
|
|
+ self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA,
|
|
|
+ aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
+ aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
+ self.endpoint = 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
|
|
|
|
|
|
- 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'
|
|
|
- }
|
|
|
- return res
|
|
|
+ if region_id == 3:
|
|
|
+ self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE,
|
|
|
+ aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
+ aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
+ self.endpoint = 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
|
|
|
|
|
|
- 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, parameters
|
|
|
+ if region_id == 4:
|
|
|
+ self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA,
|
|
|
+ aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
+ aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
+ self.endpoint = 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
|
|
|
|
|
|
|
|
|
-class AmericaIOTClient(IOTObject):
|
|
|
-
|
|
|
- def __init__(self):
|
|
|
- self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA,
|
|
|
- aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
- aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
|
|
|
def create_provisioning_claim(self, templateName):
|
|
|
|
|
@@ -124,104 +60,18 @@ class AmericaIOTClient(IOTObject):
|
|
|
'certificatePem': result['certificatePem'],
|
|
|
'publicKey': result['keyPair']['PublicKey'],
|
|
|
'privateKey': result['keyPair']['PrivateKey'],
|
|
|
- 'endpoint': 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
|
|
|
+ 'endpoint': self.endpoint
|
|
|
}
|
|
|
return res
|
|
|
|
|
|
- def create_keys_and_certificate(self, uid):
|
|
|
+ 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': '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, parameters
|
|
|
-
|
|
|
-
|
|
|
-class AsiaIOTClient(IOTObject):
|
|
|
-
|
|
|
- def __init__(self):
|
|
|
- self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA,
|
|
|
- aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
- aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
-
|
|
|
- def create_provisioning_claim(self, templateName):
|
|
|
- result = self.client.create_provisioning_claim(templateName=templateName)
|
|
|
-
|
|
|
- res = {
|
|
|
- 'certificateId': result['certificateId'],
|
|
|
- 'certificatePem': result['certificatePem'],
|
|
|
- 'publicKey': result['keyPair']['PublicKey'],
|
|
|
- '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'
|
|
|
+ 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
|
|
|
}
|
|
|
# 根据证书ID注册物品和策略
|
|
|
templateBody = {
|
|
@@ -244,10 +94,11 @@ class AsiaIOTClient(IOTObject):
|
|
|
"Type": "AWS::IoT::Thing",
|
|
|
"Properties": {
|
|
|
"AttributePayload": {},
|
|
|
- "ThingGroups": [],
|
|
|
+ # "ThingGroups" : ["v1-lightbulbs", {"Ref" : "DeviceLocation"}],
|
|
|
"ThingName": {
|
|
|
"Ref": "ThingName"
|
|
|
},
|
|
|
+ "ThingGroups": []
|
|
|
},
|
|
|
"OverrideSettings": {
|
|
|
"AttributePayload": "MERGE",
|
|
@@ -272,7 +123,8 @@ class AsiaIOTClient(IOTObject):
|
|
|
}
|
|
|
|
|
|
templateBody = json.dumps(templateBody)
|
|
|
- parameters = {"ThingName": "Ansjer_Device_" + uid,
|
|
|
+ parameters = {"ThingName": "Ansjer_Device_" + serial_number,
|
|
|
+ #"DeviceLocation": device_version,
|
|
|
"AWS::IoT::Certificate::Id": res['certificateId']}
|
|
|
self.client.register_thing(
|
|
|
templateBody=templateBody,
|
|
@@ -280,88 +132,3 @@ class AsiaIOTClient(IOTObject):
|
|
|
)
|
|
|
return res, parameters
|
|
|
|
|
|
-
|
|
|
-class EuropeIOTClient(IOTObject):
|
|
|
-
|
|
|
- def __init__(self):
|
|
|
- self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE,
|
|
|
- aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
|
|
|
- aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
|
|
|
-
|
|
|
- def create_provisioning_claim(self, templateName):
|
|
|
- result = self.client.create_provisioning_claim(templateName=templateName)
|
|
|
-
|
|
|
- res = {
|
|
|
- 'certificateId': result['certificateId'],
|
|
|
- 'certificatePem': result['certificatePem'],
|
|
|
- 'publicKey': result['keyPair']['PublicKey'],
|
|
|
- '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, parameters
|