浏览代码

lot 新增 CVM注册 ,创建证书

chenshibin 4 年之前
父节点
当前提交
54eea44c10
共有 2 个文件被更改,包括 316 次插入0 次删除
  1. 51 0
      Controller/IotCoreController.py
  2. 265 0
      Object/IOTCore/IotObject.py

+ 51 - 0
Controller/IotCoreController.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+import json
 import time
 
 import boto3
@@ -31,9 +32,12 @@ class IotCoreView(View):
 
         if operation == 'createProvisioningClaim':
             return self.create_provisioning_claim(request_dict, response)
+        elif operation == 'createKeysAndCertificate':
+            return self.create_keys_and_certificate(request_dict, response)
         else:
             return response.json(404)
 
+    #即时预置注册  :已放弃
     def create_provisioning_claim(self, request_dict, response):
         uid = request_dict.get('uid', None)
         token = request_dict.get('token', None)
@@ -60,6 +64,7 @@ class IotCoreView(View):
 
                 if user_region.region_id == 1:
                     iotClient = ChinaIOTClient()
+
                     return response.json(0, {'res': iotClient.create_provisioning_claim('Ansjer_Iot_Queue')})
                 elif user_region.region_id == 2:
                     iotClient = AsiaIOTClient()
@@ -72,3 +77,49 @@ class IotCoreView(View):
                     return response.json(0, {'res': iotClient.create_provisioning_claim('Ansjer_Iot_Queue')})
         else:
             return response.json(444)
+
+    # CVM注册  :正使用
+    def create_keys_and_certificate(self, request_dict, response):
+        uid = request_dict.get('uid', None)
+        uid_code = request_dict.get('uid_code', None)
+        token = request_dict.get('token', None)
+        time_stamp = request_dict.get('time_stamp', None)
+
+        if uid and token and time_stamp and uid_code:
+            uid_code = int(CommonService.decode_data(uid_code))
+            token = int(CommonService.decode_data(token))
+            time_stamp = int(time_stamp)
+
+            now_time = int(time.time())
+            distance = now_time - time_stamp
+
+            # if token != time_stamp and distance > 600: 暂时去掉延时
+            if token != time_stamp or uid != uid_code or distance > 600 :
+                return response.json(404)
+
+            region_country_qs = Device_Info.objects.filter(UID=uid).values('userID__region_country')
+            if not region_country_qs.exists():
+                return response.json(173)
+
+            region_country_qs = RegionCountryModel.objects.filter(
+                number=region_country_qs[0]['userID__region_country'])
+            if region_country_qs.exists():
+                user_region = region_country_qs[0]
+
+                if user_region.region_id == 1:
+                    iotClient = ChinaIOTClient()
+
+                elif user_region.region_id == 2:
+                    iotClient = AsiaIOTClient()
+
+                elif user_region.region_id == 3:
+                    iotClient = EuropeIOTClient()
+
+                else:
+                    iotClient = AmericaIOTClient()
+
+                res = iotClient.create_keys_and_certificate(uid)
+
+                return response.json(0, {'res': res})
+        else:
+            return response.json(444)

+ 265 - 0
Object/IOTCore/IotObject.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+import json
 from abc import ABCMeta,abstractmethod
 
 import boto3
@@ -15,6 +16,10 @@ class IOTObject(metaclass=ABCMeta):
     def create_provisioning_claim(self, templateName):
         pass
 
+    @abstractmethod
+    def create_keys_and_certificate(self, uid):
+        pass
+
 
 class ChinaIOTClient(IOTObject):
 
@@ -34,7 +39,72 @@ class ChinaIOTClient(IOTObject):
             '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
 
 
@@ -56,7 +126,72 @@ class AmericaIOTClient(IOTObject):
             '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
 
 
@@ -77,7 +212,72 @@ class AsiaIOTClient(IOTObject):
             '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
 
 
@@ -98,5 +298,70 @@ class EuropeIOTClient(IOTObject):
             '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