Beispiel #1
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_response(msg):
                try:
                    log.debug("Received msg: {0}".format(msg))
                    json_msg = json.loads(msg)
                    log.debug("Processed msg: {0}".format(json_msg["type"]))
                    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:
                    raise Exception("Exception while registering resource")

            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":
                self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                              entity_obj.entity_id)
                store_edge_system_uuid(entity_name=entity_obj.name, entity_id=entity_obj.entity_id,
                                       reg_entity_id=self.reg_entity_id)
                with self.file_ops_lock:
                    self.store_reg_entity_attributes("EdgeSystem", entity_obj.name,
                                                     self.reg_entity_id, None, None)
            else:
                self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                              entity_obj.entity_id)
                # 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)
Beispiel #2
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_response(msg):
                try:
                    log.debug("Received msg: {0}".format(msg))
                    json_msg = json.loads(msg)
                    log.debug("Processed msg: {0}".format(json_msg["type"]))
                    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:
                    raise Exception("Exception while registering resource")

            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":
                self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                              entity_obj.entity_id)
                store_edge_system_uuid(entity_name=entity_obj.name, entity_id=entity_obj.entity_id,
                                       reg_entity_id=self.reg_entity_id)
                with self.file_ops_lock:
                    self.store_reg_entity_attributes("EdgeSystem", entity_obj.name,
                                                     self.reg_entity_id, None, None)
            else:
                self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id,
                                              entity_obj.entity_id)
                # 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)
Beispiel #3
0
    def __init__(self, edge_system_name, url, port, identity=None, tls_conf=None, qos_details=None,
                 client_id="", clean_session=False, protocol="MQTTv311", transport="tcp", keep_alive=60,
                 mqtt_msg_attr=None, enable_authentication=False, conn_disconn_timeout=10):

        """
        :param edge_system_name: EdgeSystem's name for auto-generation of topic
        :param url: MQTT Broker URL or IP
        :param port: MQTT Broker Port
        :param tls_conf: TLSConf object
        :param identity: Identity object
        :param qos_details: QoSDetails object
        :param client_id: Client ID
        :param clean_session: Connect with Clean session or not
        :param userdata: userdata is user defined data of any type that is passed as the "userdata"
                         parameter to callbacks.

        :param protocol: allows explicit setting of the MQTT version to use for this client
        :param transport: Set transport to "websockets" to use WebSockets as the transport
                          mechanism. Set to "tcp" to use raw TCP, which is the default.

        :param keep_alive: KeepAliveInterval
        :param mqtt_msg_attr: MqttMessagingAttributes object or None.
                            In case of None, topics will be auto-generated. User provided topic will be used otherwise.
        :param enable_authentication: Enable user-name password authentication or not
        :param conn_disconn_timeout: Connect-Disconnect-Timeout
        """

        self.client_id = client_id

        if mqtt_msg_attr is None:
            #  pub-topic and sub-topic will be auto-generated
            log.info("pub-topic and sub-topic is auto-generated")
            self.msg_attr = MqttMessagingAttributes(edge_system_name)
            if self.client_id is None or self.client_id == "":
                #  local_uuid generated will be the client ID
                self.client_id = systemUUID().get_uuid(edge_system_name)
                log.info("generated local uuid will be the client ID")
            else:
                log.info("Client ID is provided by user")
            # Storing edge_system name and generated local_uuid which will be used in auto-generation of pub-sub topic
            store_edge_system_uuid(entity_name=edge_system_name,
                                   entity_id=self.client_id,
                                   reg_entity_id=None)
        elif isinstance(mqtt_msg_attr, MqttMessagingAttributes):
            log.info("User configured pub-topic and sub-topic")
            self.msg_attr = mqtt_msg_attr
        else:
            log.error("mqtt_mess_attr should either be None or of type MqttMessagingAttributes")
            raise TypeError("mqtt_mess_attr should either be None or of type MqttMessagingAttributes")

        self.url = url
        self.port = port
        self.identity = identity
        self.tls_conf = tls_conf
        self.qos_details = qos_details
        self.clean_session = clean_session
        self.userdata = Queue.Queue()
        self.protocol = protocol
        self.transport = transport
        self.keep_alive = keep_alive
        self.enable_authentication = enable_authentication
        self.conn_disconn_timeout = conn_disconn_timeout
        self._connect()
    def __init__(self,
                 edge_system_name,
                 url,
                 port,
                 identity=None,
                 tls_conf=None,
                 qos_details=None,
                 client_id="",
                 clean_session=False,
                 protocol="MQTTv311",
                 transport="tcp",
                 keep_alive=60,
                 mqtt_msg_attr=None,
                 enable_authentication=False,
                 conn_disconn_timeout=10):
        """
        :param edge_system_name: EdgeSystem's name for auto-generation of topic
        :param url: MQTT Broker URL or IP
        :param port: MQTT Broker Port
        :param tls_conf: TLSConf object
        :param identity: Identity object
        :param qos_details: QoSDetails object
        :param client_id: Client ID
        :param clean_session: Connect with Clean session or not
        :param userdata: userdata is user defined data of any type that is passed as the "userdata"
                         parameter to callbacks.

        :param protocol: allows explicit setting of the MQTT version to use for this client
        :param transport: Set transport to "websockets" to use WebSockets as the transport
                          mechanism. Set to "tcp" to use raw TCP, which is the default.

        :param keep_alive: KeepAliveInterval
        :param mqtt_msg_attr: MqttMessagingAttributes object or None.
                            In case of None, topics will be auto-generated. User provided topic will be used otherwise.
        :param enable_authentication: Enable user-name password authentication or not
        :param conn_disconn_timeout: Connect-Disconnect-Timeout
        """

        self.client_id = client_id

        if mqtt_msg_attr is None:
            #  pub-topic and sub-topic will be auto-generated
            log.info("pub-topic and sub-topic is auto-generated")
            self.msg_attr = MqttMessagingAttributes(edge_system_name)
            if self.client_id is None or self.client_id == "":
                #  local_uuid generated will be the client ID
                self.client_id = systemUUID().get_uuid(edge_system_name)
                log.info("generated local uuid will be the client ID")
            else:
                log.info("Client ID is provided by user")
            # Storing edge_system name and generated local_uuid which will be used in auto-generation of pub-sub topic
            store_edge_system_uuid(entity_name=edge_system_name,
                                   entity_id=self.client_id,
                                   reg_entity_id=None)
        elif isinstance(mqtt_msg_attr, MqttMessagingAttributes):
            log.info("User configured pub-topic and sub-topic")
            self.msg_attr = mqtt_msg_attr
        else:
            log.error(
                "mqtt_mess_attr should either be None or of type MqttMessagingAttributes"
            )
            raise TypeError(
                "mqtt_mess_attr should either be None or of type MqttMessagingAttributes"
            )

        self.url = url
        self.port = port
        self.identity = identity
        self.tls_conf = tls_conf
        self.qos_details = qos_details
        self.clean_session = clean_session
        self.userdata = Queue.Queue()
        self.protocol = protocol
        self.transport = transport
        self.keep_alive = keep_alive
        self.enable_authentication = enable_authentication
        self.conn_disconn_timeout = conn_disconn_timeout
        self._connect()
Beispiel #5
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(entity_obj.entity_type,
                                              entity_obj.name,
                                              self.reg_entity_id)
                store_edge_system_uuid(entity_name=entity_obj.name,
                                       entity_id=entity_obj.entity_id,
                                       reg_entity_id=self.reg_entity_id)
                with self.file_ops_lock:
                    self.store_reg_entity_attributes("EdgeSystem",
                                                     entity_obj.name,
                                                     self.reg_entity_id, None,
                                                     None)
            else:
                self.store_reg_entity_details(entity_obj.entity_type,
                                              entity_obj.name,
                                              self.reg_entity_id)
                # 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)