コード例 #1
0
    def register(self, entity_obj):
        """
        Register the entity Object to IoT Pulse DCC,
        metric is not registered so simply return the RegisteredMetric object
        :param entity_obj: Metric or Entity Object
        :return: RegisteredMetric or RegisteredEntity Object
        """
        self._assert_input(entity_obj.name, 50)
        self._assert_input(entity_obj.entity_type, 50)

        if isinstance(entity_obj, Metric):
            # reg_entity_id should be parent's one: not known here yet
            # will add in creat_relationship(); publish_unit should be done inside
            return RegisteredMetric(entity_obj, self, None)
        else:
            # finally will create a RegisteredEntity
            log.info("Registering resource with IoTCC {0}".format(entity_obj.name))
            reg_resp_q = Queue.Queue()

            def on_response(msg, reg_resp_q):
                log.debug("Received msg: {0}".format(msg))
                json_msg = json.loads(msg)
                log.debug("Processing msg: {0}".format(json_msg["type"]))
                self._check_version(json_msg)
                if json_msg["type"] == "create_or_find_resource_response" and json_msg["body"]["uuid"] != "null" and \
                                json_msg["body"]["id"] == entity_obj.entity_id:
                    log.info("FOUND RESOURCE: {0}".format(json_msg["body"]["uuid"]))
                    self.reg_entity_id = json_msg["body"]["uuid"]
                else:
                    log.info("Waiting for resource creation")
                    on_response(reg_resp_q.get(True, timeout), reg_resp_q)

            if entity_obj.entity_type == "EdgeSystem":
                entity_obj.entity_type = "HelixGateway"
            transaction_id = self._next_id()
            req = Request(transaction_id, reg_resp_q)
            log.debug("Updating resource registration queue for transaction_id:{0}".format(transaction_id))
            with self._req_ops_lock:
                self._req_dict.update({transaction_id: req})
            self.comms.send(json.dumps(
                self._registration(transaction_id, entity_obj.entity_id, entity_obj.name, entity_obj.entity_type)))
            on_response(reg_resp_q.get(True, timeout), reg_resp_q)
            if not self.reg_entity_id:
                raise RegistrationFailure()
            log.info("Resource Registered {0}".format(entity_obj.name))
            if entity_obj.entity_type == "HelixGateway":
                with self.file_ops_lock:
                    self._store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                                   entity_obj.entity_id)
                    self._store_reg_entity_attributes("EdgeSystem", entity_obj.name,
                                                      self.reg_entity_id, None, None)
            else:
                # get dev_type, and prop_dict if possible
                with self.file_ops_lock:
                    self._store_reg_entity_attributes("Devices", entity_obj.name, self.reg_entity_id,
                                                      entity_obj.entity_type, None)

            return RegisteredEntity(entity_obj, self, self.reg_entity_id)
コード例 #2
0
ファイル: iotcc.py プロジェクト: tompscanlan/liota
    def register(self, entity_obj):
        """ Register the objects

        """
        if isinstance(entity_obj, Metric):
            # reg_entity_id should be parent's one: not known here yet
            # will add in creat_relationship(); publish_unit should be done inside
            return RegisteredMetric(entity_obj, self, None)
        else:
            # finally will create a RegisteredEntity
            log.info("Registering resource with IoTCC {0}".format(entity_obj.name))

            def on_response(msg):
                try:
                    log.debug("Received msg: {0}".format(msg))
                    json_msg = json.loads(msg)
                    log.debug("Processing msg: {0}".format(json_msg["type"]))
                    self._check_version(json_msg)
                    if json_msg["type"] == "UNSUPPORTED_VERSION":
                         raise Exception("Exception in registering resource, version mismatch. Response received from server:" + json_msg["body"])
                    if json_msg["type"] == "create_or_find_resource_response" and json_msg["body"]["uuid"] != "null" and \
                                    json_msg["body"]["id"] == entity_obj.entity_id:
                        log.info("FOUND RESOURCE: {0}".format(json_msg["body"]["uuid"]))
                        self.reg_entity_id = json_msg["body"]["uuid"]
                    else:
                        log.info("Waiting for resource creation")
                        on_response(self.recv_msg_queue.get(True, 300))
                except Exception as err:
                    log.exception("Exception while registering resource")
                    raise err

            if entity_obj.entity_type == "EdgeSystem":
                entity_obj.entity_type = "HelixGateway"
            self.comms.send(json.dumps(
                self._registration(self.next_id(), entity_obj.entity_id, entity_obj.name, entity_obj.entity_type)))
            on_response(self.recv_msg_queue.get(True, 300))
            if not self.reg_entity_id:
                raise RegistrationFailure()
            log.info("Resource Registered {0}".format(entity_obj.name))
            if entity_obj.entity_type == "HelixGateway":
                with self.file_ops_lock:
                    self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                              entity_obj.entity_id)
                    self.store_reg_entity_attributes("EdgeSystem", entity_obj.name,
                                                     self.reg_entity_id, None, None)
            else:
                # get dev_type, and prop_dict if possible
                with self.file_ops_lock:
                    self.store_reg_entity_attributes("Devices", entity_obj.name, self.reg_entity_id,
                                                     entity_obj.entity_type, None)

            return RegisteredEntity(entity_obj, self, self.reg_entity_id)
コード例 #3
0
    def register(self, entity_obj):
        """ Register the objects

        """
        if isinstance(entity_obj, Metric):
            # reg_entity_id should be parent's one: not known here yet
            # will add in creat_relationship(); publish_unit should be done inside
            return RegisteredMetric(entity_obj, self, None)
        else:
            # finally will create a RegisteredEntity
            log.info("Registering resource with IoTCC {0}".format(entity_obj.name))

            def on_receive_safe(msg):
                try:
                    log.debug("Received msg: {0}".format(msg))
                    if msg != "":
                        json_msg = json.loads(msg)
                        self.proto.on_receive(json.loads(msg))
                        log.debug("Processed msg: {0}".format(json_msg["type"]))
                        if json_msg["type"] == "create_or_find_resource_response":
                            if json_msg["body"]["uuid"] != "null":
                                log.info("FOUND RESOURCE: {0}".format(json_msg["body"]["uuid"]))
                                self.reg_entity_id = json_msg["body"]["uuid"]
                                exit()
                            else:
                                log.info("Waiting for resource creation")
                                time.sleep(5)
                                self.con.send(
                                    self._registration(self.con.next_id(), entity_obj.entity_id, entity_obj.name,
                                                       entity_obj.entity_type))
                except:
                    raise

            thread = threading.Thread(target=self.con.run)
            self.con.on_receive = on_receive_safe
            thread.daemon = True
            thread.start()
            if entity_obj.entity_type == "EdgeSystem":
                entity_obj.entity_type = "HelixGateway"
            self.con.send(
                self._registration(self.con.next_id(), entity_obj.entity_id, entity_obj.name, entity_obj.entity_type))
            thread.join()
            if not hasattr(self, 'reg_entity_id'):
                raise RegistrationFailure()
            log.info("Resource Registered {0}".format(entity_obj.name))
            if entity_obj.entity_type == "HelixGateway":
                self.store_reg_entity_details("EdgeSystem", entity_obj.name, self.reg_entity_id)
                self.store_edge_system_uuid(entity_obj.name, self.reg_entity_id)
            else:
                self.store_reg_entity_details("Devices", entity_obj.name, self.reg_entity_id)
            return RegisteredEntity(entity_obj, self, self.reg_entity_id)