Esempio n. 1
0
    def __init_listener(self):
        def on_connect(client, userdata, flags, rc):
            logger.info("MQTT client [name=%s] connected with result code %s.",
                        self.name, str(rc))
            self.is_connected = True

        def disconnect():
            self.client.loop_stop()
            self.client.disconnect()
            self.is_connected = False

        # Create MQTT client
        self.client = mqtt.Client()
        self.client.on_connect = on_connect
        tmp_base = utils.get_tmp_base_path_from_mkfile()
        dev_base_path = os.path.join(tmp_base, self.name)
        # Create device base path if it does not exist
        if not os.path.exists(dev_base_path):
            os.makedirs(dev_base_path)
        address = os.path.join(dev_base_path, 'mqtt_socket')
        # Ensure that socket does not exist
        try:
            os.unlink(address)
        except OSError:
            if os.path.exists(address):
                logger.exception('Could not remove socket file.')
        # Create listener
        listener = Listener(address)
        while True:
            conn = listener.accept()
            msg = conn.recv()
            if isinstance(msg, ConnectMessage):
                if msg.auth is not None:
                    username = msg.auth['username']
                    password = msg.auth['password']
                    if username is None:
                        logger.error(
                            "Could not authenticate MQTT client [name=%s], " +
                            "because username has no username has been provided.",
                            self.name)
                    else:
                        self.client.username_pw_set(username,
                                                    password=password)
                self.client.connect(msg.host, msg.port, msg.keepalive,
                                    msg.bind_address)
                self.client.loop_start()
            elif isinstance(msg, PublishMessage):
                self.client.publish(msg.topic, msg.payload, msg.qos,
                                    msg.retain)
            elif isinstance(msg, DisconnectMessage):
                disconnect()
            elif isinstance(msg, CloseMessage):
                disconnect()
                conn.close()
                break
        listener.close()
Esempio n. 2
0
 def __create_connection(self):
     tmp_base = utils.get_tmp_base_path_from_mkfile()
     plc_base_path = os.path.join(tmp_base, self.plc.name)
     address = os.path.join(plc_base_path, 'plc_socket')
     # Wait until listener is ready
     # TODO: Refactoring needed (anti-pattern)
     while not self.listener_ready:
         try:
             self.conn = Client(address)
             self.listener_ready = True
         except socket.error as e:
             # Check if listener is not yet ready
             if e.errno == errno.ENOENT:  # socket.error: [Errno 2] No such file or directory
                 sleep(1)
             else:
                 logger.exception("Unknown socket error occurred.")
Esempio n. 3
0
 def __create_connection(self):
     tmp_base = utils.get_tmp_base_path_from_mkfile()
     plc_base_path = os.path.join(tmp_base, self.name)
     address = os.path.join(plc_base_path, 'plc_socket')
     self.conn = Client(address)