123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import json
- from abc import ABCMeta,abstractmethod
- import boto3
- from Ansjer.config import AWS_IOT_SES_ACCESS_CHINA_REGION, AWS_IOT_SES_ACCESS_CHINA_ID, AWS_IOT_SES_ACCESS_CHINA_SECRET, \
- AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, AWS_IOT_SES_ACCESS_FOREIGN_ID, AWS_IOT_SES_ACCESS_FOREIGN_SECRET, \
- AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE, AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA
- class IOTObject(metaclass=ABCMeta):
- @abstractmethod
- def create_provisioning_claim(self, templateName):
- pass
- @abstractmethod
- def create_keys_and_certificate(self, uid):
- pass
- class ChinaIOTClient(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 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': '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, parameters
- 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):
- 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.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, 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'
- }
- # 根据证书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 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
|