IotObject.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from abc import ABCMeta,abstractmethod
  5. import boto3
  6. from Ansjer.config import AWS_IOT_SES_ACCESS_CHINA_REGION, AWS_IOT_SES_ACCESS_CHINA_ID, AWS_IOT_SES_ACCESS_CHINA_SECRET, \
  7. AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, AWS_IOT_SES_ACCESS_FOREIGN_ID, AWS_IOT_SES_ACCESS_FOREIGN_SECRET, \
  8. AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE, AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA
  9. class IOTObject(metaclass=ABCMeta):
  10. @abstractmethod
  11. def create_provisioning_claim(self, templateName):
  12. pass
  13. @abstractmethod
  14. def create_keys_and_certificate(self, uid):
  15. pass
  16. class IOTClient(IOTObject):
  17. def __init__(self, region_id = 1):
  18. if region_id == 1:
  19. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_CHINA_REGION,
  20. aws_access_key_id=AWS_IOT_SES_ACCESS_CHINA_ID,
  21. aws_secret_access_key=AWS_IOT_SES_ACCESS_CHINA_SECRET)
  22. self.endpoint = 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
  23. if region_id == 2:
  24. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA,
  25. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  26. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  27. self.endpoint = 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
  28. if region_id == 3:
  29. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE,
  30. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  31. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  32. self.endpoint = 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
  33. if region_id == 4:
  34. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA,
  35. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  36. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  37. self.endpoint = 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
  38. def create_provisioning_claim(self, templateName):
  39. result = self.client.create_provisioning_claim(templateName=templateName)
  40. res = {
  41. 'certificateId': result['certificateId'],
  42. 'certificatePem': result['certificatePem'],
  43. 'publicKey': result['keyPair']['PublicKey'],
  44. 'privateKey': result['keyPair']['PrivateKey'],
  45. 'endpoint': self.endpoint
  46. }
  47. return res
  48. def create_keys_and_certificate(self, serial_number, device_version):
  49. result = self.client.create_keys_and_certificate(setAsActive=True)
  50. res = {
  51. 'certificateId': result['certificateId'],
  52. 'certificatePem': result['certificatePem'],
  53. 'publicKey': result['keyPair']['PublicKey'],
  54. 'privateKey': result['keyPair']['PrivateKey'],
  55. 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
  56. }
  57. # 根据证书ID注册物品和策略
  58. templateBody = {
  59. "Parameters": {
  60. "ThingName": {
  61. "Type": "String"
  62. },
  63. "SerialNumber": {
  64. "Type": "String"
  65. },
  66. "DeviceLocation": {
  67. "Type": "String"
  68. },
  69. "AWS::IoT::Certificate::Id": {
  70. "Type": "String"
  71. }
  72. },
  73. "Resources": {
  74. "thing": {
  75. "Type": "AWS::IoT::Thing",
  76. "Properties": {
  77. "AttributePayload": {},
  78. # "ThingGroups" : ["v1-lightbulbs", {"Ref" : "DeviceLocation"}],
  79. "ThingName": {
  80. "Ref": "ThingName"
  81. },
  82. "ThingGroups": []
  83. },
  84. "OverrideSettings": {
  85. "AttributePayload": "MERGE",
  86. "ThingTypeName": "REPLACE",
  87. "ThingGroups": "DO_NOTHING"
  88. }
  89. },
  90. "certificate": {
  91. "Type": "AWS::IoT::Certificate",
  92. "Properties": {
  93. "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
  94. "Status": "Active"
  95. }
  96. },
  97. "policy": {
  98. "Properties": {
  99. "PolicyName": "My_Iot_Policy"
  100. },
  101. "Type": "AWS::IoT::Policy"
  102. },
  103. }
  104. }
  105. templateBody = json.dumps(templateBody)
  106. parameters = {"ThingName": "Ansjer_Device_" + serial_number,
  107. #"DeviceLocation": device_version,
  108. "AWS::IoT::Certificate::Id": res['certificateId']}
  109. self.client.register_thing(
  110. templateBody=templateBody,
  111. parameters=parameters
  112. )
  113. return res, parameters