IotObject.py 14 KB

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