IotObject.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 ChinaIOTClient(IOTObject):
  17. def __init__(self):
  18. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_CHINA_REGION,
  19. aws_access_key_id=AWS_IOT_SES_ACCESS_CHINA_ID,
  20. aws_secret_access_key=AWS_IOT_SES_ACCESS_CHINA_SECRET)
  21. def create_provisioning_claim(self, templateName):
  22. result = self.client.create_provisioning_claim(templateName=templateName)
  23. res = {
  24. 'certificateId': result['certificateId'],
  25. 'certificatePem': result['certificatePem'],
  26. 'publicKey': result['keyPair']['PublicKey'],
  27. 'privateKey': result['keyPair']['PrivateKey'],
  28. 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
  29. }
  30. return res
  31. def create_keys_and_certificate(self, uid):
  32. result = self.client.create_keys_and_certificate(setAsActive=True)
  33. res = {
  34. 'certificateId': result['certificateId'],
  35. 'certificatePem': result['certificatePem'],
  36. 'publicKey': result['keyPair']['PublicKey'],
  37. 'privateKey': result['keyPair']['PrivateKey'],
  38. 'endpoint': 'a250bbr0p9u7as-ats.iot.cn-northwest-1.amazonaws.com.cn'
  39. }
  40. # 根据证书ID注册物品和策略
  41. templateBody = {
  42. "Parameters": {
  43. "ThingName": {
  44. "Type": "String"
  45. },
  46. "SerialNumber": {
  47. "Type": "String"
  48. },
  49. "DeviceLocation": {
  50. "Type": "String"
  51. },
  52. "AWS::IoT::Certificate::Id": {
  53. "Type": "String"
  54. }
  55. },
  56. "Resources": {
  57. "thing": {
  58. "Type": "AWS::IoT::Thing",
  59. "Properties": {
  60. "AttributePayload": {},
  61. "ThingGroups": [],
  62. "ThingName": {
  63. "Ref": "ThingName"
  64. },
  65. },
  66. "OverrideSettings": {
  67. "AttributePayload": "MERGE",
  68. "ThingTypeName": "REPLACE",
  69. "ThingGroups": "DO_NOTHING"
  70. }
  71. },
  72. "certificate": {
  73. "Type": "AWS::IoT::Certificate",
  74. "Properties": {
  75. "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
  76. "Status": "Active"
  77. }
  78. },
  79. "policy": {
  80. "Properties": {
  81. "PolicyName": "My_Iot_Policy"
  82. },
  83. "Type": "AWS::IoT::Policy"
  84. },
  85. }
  86. }
  87. templateBody = json.dumps(templateBody)
  88. parameters = {"ThingName": "Ansjer_Device_" + uid,
  89. "AWS::IoT::Certificate::Id": res['certificateId']}
  90. self.client.register_thing(
  91. templateBody=templateBody,
  92. parameters=parameters
  93. )
  94. return res, parameters
  95. class AmericaIOTClient(IOTObject):
  96. def __init__(self):
  97. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA,
  98. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  99. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  100. def create_provisioning_claim(self, templateName):
  101. result = self.client.create_provisioning_claim(templateName=templateName)
  102. res = {
  103. 'certificateId': result['certificateId'],
  104. 'certificatePem': result['certificatePem'],
  105. 'publicKey': result['keyPair']['PublicKey'],
  106. 'privateKey': result['keyPair']['PrivateKey'],
  107. 'endpoint': 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
  108. }
  109. return res
  110. def create_keys_and_certificate(self, uid):
  111. result = self.client.create_keys_and_certificate(setAsActive=True)
  112. res = {
  113. 'certificateId': result['certificateId'],
  114. 'certificatePem': result['certificatePem'],
  115. 'publicKey': result['keyPair']['PublicKey'],
  116. 'privateKey': result['keyPair']['PrivateKey'],
  117. 'endpoint': 'a2rqy12o004ad8-ats.iot.us-east-1.amazonaws.com'
  118. }
  119. # 根据证书ID注册物品和策略
  120. templateBody = {
  121. "Parameters": {
  122. "ThingName": {
  123. "Type": "String"
  124. },
  125. "SerialNumber": {
  126. "Type": "String"
  127. },
  128. "DeviceLocation": {
  129. "Type": "String"
  130. },
  131. "AWS::IoT::Certificate::Id": {
  132. "Type": "String"
  133. }
  134. },
  135. "Resources": {
  136. "thing": {
  137. "Type": "AWS::IoT::Thing",
  138. "Properties": {
  139. "AttributePayload": {},
  140. "ThingGroups": [],
  141. "ThingName": {
  142. "Ref": "ThingName"
  143. },
  144. },
  145. "OverrideSettings": {
  146. "AttributePayload": "MERGE",
  147. "ThingTypeName": "REPLACE",
  148. "ThingGroups": "DO_NOTHING"
  149. }
  150. },
  151. "certificate": {
  152. "Type": "AWS::IoT::Certificate",
  153. "Properties": {
  154. "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
  155. "Status": "Active"
  156. }
  157. },
  158. "policy": {
  159. "Properties": {
  160. "PolicyName": "My_Iot_Policy"
  161. },
  162. "Type": "AWS::IoT::Policy"
  163. },
  164. }
  165. }
  166. templateBody = json.dumps(templateBody)
  167. parameters = {"ThingName": "Ansjer_Device_" + uid,
  168. "AWS::IoT::Certificate::Id": res['certificateId']}
  169. self.client.register_thing(
  170. templateBody=templateBody,
  171. parameters=parameters
  172. )
  173. return res, parameters
  174. class AsiaIOTClient(IOTObject):
  175. def __init__(self):
  176. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA,
  177. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  178. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  179. def create_provisioning_claim(self, templateName):
  180. result = self.client.create_provisioning_claim(templateName=templateName)
  181. res = {
  182. 'certificateId': result['certificateId'],
  183. 'certificatePem': result['certificatePem'],
  184. 'publicKey': result['keyPair']['PublicKey'],
  185. 'privateKey': result['keyPair']['PrivateKey'],
  186. 'endpoint': 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
  187. }
  188. return res
  189. def create_keys_and_certificate(self, uid):
  190. result = self.client.create_keys_and_certificate(setAsActive=True)
  191. res = {
  192. 'certificateId': result['certificateId'],
  193. 'certificatePem': result['certificatePem'],
  194. 'publicKey': result['keyPair']['PublicKey'],
  195. 'privateKey': result['keyPair']['PrivateKey'],
  196. 'endpoint': 'a2rqy12o004ad8-ats.iot.ap-southeast-1.amazonaws.com'
  197. }
  198. # 根据证书ID注册物品和策略
  199. templateBody = {
  200. "Parameters": {
  201. "ThingName": {
  202. "Type": "String"
  203. },
  204. "SerialNumber": {
  205. "Type": "String"
  206. },
  207. "DeviceLocation": {
  208. "Type": "String"
  209. },
  210. "AWS::IoT::Certificate::Id": {
  211. "Type": "String"
  212. }
  213. },
  214. "Resources": {
  215. "thing": {
  216. "Type": "AWS::IoT::Thing",
  217. "Properties": {
  218. "AttributePayload": {},
  219. "ThingGroups": [],
  220. "ThingName": {
  221. "Ref": "ThingName"
  222. },
  223. },
  224. "OverrideSettings": {
  225. "AttributePayload": "MERGE",
  226. "ThingTypeName": "REPLACE",
  227. "ThingGroups": "DO_NOTHING"
  228. }
  229. },
  230. "certificate": {
  231. "Type": "AWS::IoT::Certificate",
  232. "Properties": {
  233. "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
  234. "Status": "Active"
  235. }
  236. },
  237. "policy": {
  238. "Properties": {
  239. "PolicyName": "My_Iot_Policy"
  240. },
  241. "Type": "AWS::IoT::Policy"
  242. },
  243. }
  244. }
  245. templateBody = json.dumps(templateBody)
  246. parameters = {"ThingName": "Ansjer_Device_" + uid,
  247. "AWS::IoT::Certificate::Id": res['certificateId']}
  248. self.client.register_thing(
  249. templateBody=templateBody,
  250. parameters=parameters
  251. )
  252. return res, parameters
  253. class EuropeIOTClient(IOTObject):
  254. def __init__(self):
  255. self.client = boto3.client('iot', region_name=AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE,
  256. aws_access_key_id=AWS_IOT_SES_ACCESS_FOREIGN_ID,
  257. aws_secret_access_key=AWS_IOT_SES_ACCESS_FOREIGN_SECRET)
  258. def create_provisioning_claim(self, templateName):
  259. result = self.client.create_provisioning_claim(templateName=templateName)
  260. res = {
  261. 'certificateId': result['certificateId'],
  262. 'certificatePem': result['certificatePem'],
  263. 'publicKey': result['keyPair']['PublicKey'],
  264. 'privateKey': result['keyPair']['PrivateKey'],
  265. 'endpoint': 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
  266. }
  267. return res
  268. def create_keys_and_certificate(self, uid):
  269. result = self.client.create_keys_and_certificate(setAsActive=True)
  270. res = {
  271. 'certificateId': result['certificateId'],
  272. 'certificatePem': result['certificatePem'],
  273. 'publicKey': result['keyPair']['PublicKey'],
  274. 'privateKey': result['keyPair']['PrivateKey'],
  275. 'endpoint': 'a2rqy12o004ad8-ats.iot.eu-west-1.amazonaws.com'
  276. }
  277. # 根据证书ID注册物品和策略
  278. templateBody = {
  279. "Parameters": {
  280. "ThingName": {
  281. "Type": "String"
  282. },
  283. "SerialNumber": {
  284. "Type": "String"
  285. },
  286. "DeviceLocation": {
  287. "Type": "String"
  288. },
  289. "AWS::IoT::Certificate::Id": {
  290. "Type": "String"
  291. }
  292. },
  293. "Resources": {
  294. "thing": {
  295. "Type": "AWS::IoT::Thing",
  296. "Properties": {
  297. "AttributePayload": {},
  298. "ThingGroups": [],
  299. "ThingName": {
  300. "Ref": "ThingName"
  301. },
  302. },
  303. "OverrideSettings": {
  304. "AttributePayload": "MERGE",
  305. "ThingTypeName": "REPLACE",
  306. "ThingGroups": "DO_NOTHING"
  307. }
  308. },
  309. "certificate": {
  310. "Type": "AWS::IoT::Certificate",
  311. "Properties": {
  312. "CertificateId": {"Ref": "AWS::IoT::Certificate::Id"},
  313. "Status": "Active"
  314. }
  315. },
  316. "policy": {
  317. "Properties": {
  318. "PolicyName": "My_Iot_Policy"
  319. },
  320. "Type": "AWS::IoT::Policy"
  321. },
  322. }
  323. }
  324. templateBody = json.dumps(templateBody)
  325. parameters = {"ThingName": "Ansjer_Device_" + uid,
  326. "AWS::IoT::Certificate::Id": res['certificateId']}
  327. self.client.register_thing(
  328. templateBody=templateBody,
  329. parameters=parameters
  330. )
  331. return res, parameters