Ejemplo n.º 1
0
async def mqtt_record(server: str,
                      output: str = None,
                      append: bool = False) -> None:
    """Record MQTT messages"""
    mqtt = MQTTClient()
    await mqtt.connect(server)
    await mqtt.subscribe(TOPICS)
    if (output is not None and append is False):
        output_file = open(output, 'wt')
        output_file.close()
    while True:
        message = await mqtt.deliver_message()
        if output is not None:
            output_file = open(output, 'a+')
        else:
            output_file = sys.stdout
        record = {
            'time': time.time(),
            'qos': message.qos,
            'retain': message.retain,
            'topic': message.topic,
            'msg_b64': base64.urlsafe_b64encode(message.data).decode()
        }
        print(json.dumps(record), file=output_file)
        if output is not None:
            output_file.close()
            logger.debug(json.dumps(record))
Ejemplo n.º 2
0
async def main(color, value, topic):
    """Starter
    """
    sub_topic = 'hb/{}'.format(topic)

    mqtt = MQTTClient()
    await mqtt.connect('mqtt://localhost')
    await mqtt.subscribe([(sub_topic, QOS_1)])

    leds = cmd.Leds()
    setattr(leds, color, value)
    await mqtt.publish(topic, ns.marshall(leds))

    try:
        while True:
            message = await mqtt.deliver_message()
            payload = message.publish_packet.payload.data
            if ns.is_protobuf(payload):
                obj = ns.unmarshall(payload)
                if ns.is_ack(obj):
                    break
            else:
                LOG.debug(">> %s", payload)

        await mqtt.unsubscribe([sub_topic])
        await mqtt.disconnect()

    except ClientException as cli_exc:
        LOG.error("Client exception: %s", cli_exc)
Ejemplo n.º 3
0
async def mqtt_replay(server: str,
                      input: str = None,
                      delay: int = 0,
                      realtime: bool = False,
                      scale: float = 1,
                      repeat: bool = False) -> None:
    """Replay MQTT messages"""
    mqtt = MQTTClient()
    await mqtt.connect(server)
    await mqtt.subscribe(TOPICS)
    if input is not None:
        input_file = open(input, 'rt')
    else:
        input_file = sys.stdin
    if delay > 0:
        static_delay_s = delay / 1000
    else:
        static_delay_s = 0
    last_timestamp = None

    if repeat:
        while True:
            await process_input_file(input_file, last_timestamp, mqtt,
                                     realtime, scale, static_delay_s)
            if input is not None:
                input_file = open(input, 'rt')
    else:
        await process_input_file(input_file, last_timestamp, mqtt, realtime,
                                 scale, static_delay_s)
Ejemplo n.º 4
0
        def test_coro():
            try:
                broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())
                sub_client = MQTTClient()
                yield from sub_client.connect('mqtt://localhost', cleansession=False)
                ret = yield from sub_client.subscribe([('/qos0', QOS_0), ('/qos1', QOS_1), ('/qos2', QOS_2)])
                self.assertEquals(ret, [QOS_0, QOS_1, QOS_2])
                yield from sub_client.disconnect()
                yield from asyncio.sleep(0.1)

                yield from self._client_publish('/qos0', b'data', QOS_0, retain=True)
                yield from self._client_publish('/qos1', b'data', QOS_1, retain=True)
                yield from self._client_publish('/qos2', b'data', QOS_2, retain=True)
                yield from sub_client.reconnect()
                for qos in [QOS_0, QOS_1, QOS_2]:
                    log.debug("TEST QOS: %d" % qos)
                    message = yield from sub_client.deliver_message()
                    log.debug("Message: " + repr(message.publish_packet))
                    self.assertIsNotNone(message)
                    self.assertEquals(message.topic, '/qos%s' % qos)
                    self.assertEquals(message.data, b'data')
                    self.assertEquals(message.qos, qos)
                yield from sub_client.disconnect()
                yield from asyncio.sleep(0.1)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
async def emulate(args: argparse.Namespace) -> None:
    """Run broker and client and publish values."""

    # Parse data into a dict
    mqtt_data = {}
    with open(args.filename, "rt") as fp:
        for line in fp:
            item_topic, item_payload = line.strip().split(",", 1)
            mqtt_data[item_topic] = json.loads(item_payload)

    # Run Broker
    broker = Broker(BROKER_CONFIG)
    await broker.start()

    # Run Client
    client = MQTTClient()
    await client.connect("mqtt://localhost")

    # Publish all topics from the provided dump file
    for topic, data in mqtt_data.items():
        payload = json.dumps(data).encode()
        await client.publish(topic, payload, retain=True)

    # Subscribe to command topic and start listening for commands
    await client.subscribe([("OpenZWave/1/command/#", QOS_0)])
    try:
        await process_messages(client, mqtt_data)
    except asyncio.CancelledError:
        await client.disconnect()
        broker.shutdown()
Ejemplo n.º 6
0
def main():
    if sys.version_info[:2] < (3, 4):
        logger.fatal("Error: Python 3.4+ is required")
        sys.exit(-1)

    config = None
    config = read_yaml_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default_client.yaml'))
    logger.debug("Using default configuration")
    
    loop = asyncio.get_event_loop()

    client_id = "mqtt_publisher_exp"
    
    client = MQTTClient(client_id=client_id, config=config, loop=loop)
    try:
        logger.info("%s Connecting to broker" % client.client_id)

        loop.run_until_complete(client.connect(uri=BROKER_URL))
        qos = QOS_1
        topic = "topic_1"

        for _, row in data.iterrows():
            row['TIMING_client_request_timestamp'] = time()
            message = row.to_json().encode(encoding='utf-8')
            logger.info("%s Publishing to '%s'" % (client.client_id, topic))
            loop.run_until_complete(client.publish(topic, message, qos))
            sleep(0.1)
    except KeyboardInterrupt:
        loop.run_until_complete(client.disconnect())
        logger.info("%s Disconnected from broker" % client.client_id)
    except ConnectException as ce:
        logger.fatal("connection to '%s' failed: %r" % (BROKER_URL, ce))
    except asyncio.CancelledError as cae:
        logger.fatal("Publish canceled due to prvious error")
Ejemplo n.º 7
0
    def _connect(self):
        """MQTT connection helper function."""

        config = self._build_client_config()

        self._log(logging.DEBUG, "MQTT client ID: {}".format(self._client_id))
        self._log(logging.DEBUG, "MQTT client config: {}".format(config))

        hbmqtt_client = MQTTClient(client_id=self._client_id, config=config)

        self._log(
            logging.INFO,
            "Connecting MQTT client to broker: {}".format(self._broker_url))

        ack_con = yield hbmqtt_client.connect(self._broker_url,
                                              cleansession=False)

        if ack_con != MQTTCodesACK.CON_OK:
            raise ConnectException(
                "Error code in connection ACK: {}".format(ack_con))

        if self._mqtt_handler.topics:
            self._log(logging.DEBUG,
                      "Subscribing to: {}".format(self._mqtt_handler.topics))
            ack_sub = yield hbmqtt_client.subscribe(self._mqtt_handler.topics)

            if MQTTCodesACK.SUB_ERROR in ack_sub:
                raise ConnectException(
                    "Error code in subscription ACK: {}".format(ack_sub))

        self._client = hbmqtt_client
Ejemplo n.º 8
0
async def test_client_subscribe_twice(broker, mock_plugin_manager):
    client = MQTTClient()
    ret = await client.connect("mqtt://127.0.0.1/")
    assert ret == 0
    await client.subscribe([("/topic", QOS_0)])

    # Test if the client test client subscription is registered
    assert "/topic" in broker._subscriptions
    subs = broker._subscriptions["/topic"]
    assert len(subs) == 1
    (s, qos) = subs[0]
    assert s == client.session
    assert qos == QOS_0

    await client.subscribe([("/topic", QOS_0)])
    assert len(subs) == 1
    (s, qos) = subs[0]
    assert s == client.session
    assert qos == QOS_0

    await client.disconnect()
    await asyncio.sleep(0.1)

    mock_plugin_manager.assert_has_calls(
        [
            call().fire_event(
                EVENT_BROKER_CLIENT_SUBSCRIBED,
                client_id=client.session.client_id,
                topic="/topic",
                qos=QOS_0,
            )
        ],
        any_order=True,
    )
Ejemplo n.º 9
0
 async def start(self):
     self._client = MQTTClient(config={'reconnect_retries': 500})
     try:
         await self._client.connect('mqtt://%s:%s/' %
                                    (self._hostname, self._port))
     except ClientException as ce:
         logger.error("Client exception: %s" % ce)
Ejemplo n.º 10
0
def uptime_coro():

    try:
        while True:
            C = MQTTClient()

            yield from C.connect('mqtt://127.0.0.1:8080')
            # Subscribe to '$SYS/broker/uptime' with QOS=1
            yield from C.subscribe([
                ('bd/#', QOS_1),

            ])
            logger.info("Subscribed")
            message = yield from C.deliver_message()
            packet = message.publish_packet
            get_request = packet.payload.data
            header = packet.variable_header.topic_name.split('/')
            if  get_request == b'bot':
                print("%d: %s => %s" % (1, packet.variable_header.topic_name, str))
                db_maybe.bot_session(C, packet.variable_header.topic_name, packet.payload.data)
            if header[1] == 'two':
                print("%d: %s => %s" % (1, packet.variable_header.topic_name, str))
                db_maybe.two_players_session(C, packet.variable_header.topic_name, packet.payload.data)

        yield from C.unsubscribe(['$SYS/broker/uptime', '$SYS/broker/load/#'])
        logger.info("UnSubscribed")
        yield from C.disconnect()
    except ClientException as ce:
        logger.error("Client exception: %s" % ce)

    yield from C.unsubscribe(['$SYS/broker/uptime', '$SYS/broker/load/#'])
    logger.info("UnSubscribed")

    yield from C.disconnect()
Ejemplo n.º 11
0
    async def start_helper_bot(self):

        try:
            if self.Client is None:
                self.Client = MQTTClient(client_id=self.client_id,
                                         config={"check_hostname": False})

            await self.Client.connect(
                "mqtts://{}:{}/".format(self.address[0], self.address[1]),
                cafile=bumper.ca_cert,
            )
            await self.Client.subscribe([
                ("iot/p2p/+/+/+/+/helper1/bumper/helper1/+/+/+", QOS_0),
                ("iot/p2p/+", QOS_0),
                ("iot/atr/+", QOS_0),
            ])

            asyncio.create_task(self.get_msg())

        except ConnectionRefusedError as e:
            helperbotlog.Error(e)
            pass

        except asyncio.CancelledError as e:
            pass

        except hbmqtt.client.ConnectException as e:
            helperbotlog.Error(e)
            pass

        except Exception as e:
            helperbotlog.exception("{}".format(e))
Ejemplo n.º 12
0
    def __init__(self,cbpi):

        self.config = {
            'listeners': {
                'default': {
                    'type': 'tcp',
                    'bind': '0.0.0.0:1885',
                },
                'ws': {
                    'bind': '0.0.0.0:8081',
                    'type': 'ws'
                }
            },
            'sys_interval': 10,
            'topic-check': {
                'enabled': True,
                'plugins': [
                    'topic_taboo'
                ]
            },
            'auth': {
                'allow-anonymous': True,
                'password-file': '/Users/manuelfritsch/github/aio_sample.cbpi/user.txt'
            }
        }

        self.cbpi = cbpi
        self.broker = Broker(self.config, plugin_namespace="hbmqtt.broker.plugins")
        self.client = MQTTClient()
        self.matcher = MQTTMatcher()
        self.mqtt_methods = {"test": self.ok_msg, "$SYS/broker/#": self.sysmsg}
        self.cbpi.app.on_startup.append(self.start_broker)
        self.count = 0
Ejemplo n.º 13
0
 def _client_publish(self, topic, data, qos, retain=False):
     pub_client = MQTTClient()
     ret = yield from pub_client.connect('mqtt://127.0.0.1/')
     self.assertEqual(ret, 0)
     ret = yield from pub_client.publish(topic, data, qos, retain)
     yield from pub_client.disconnect()
     return ret
Ejemplo n.º 14
0
 def test_coro():
     try:
         broker = Broker(test_config,
                         plugin_namespace="hbmqtt.test.plugins")
         yield from broker.start()
         self.assertTrue(broker.transitions.is_started())
         client = MQTTClient()
         ret = yield from client.connect('mqtt://127.0.0.1/')
         self.assertEqual(ret, 0)
         self.assertIn(client.session.client_id, broker._sessions)
         yield from client.disconnect()
         yield from asyncio.sleep(0.1)
         yield from broker.shutdown()
         self.assertTrue(broker.transitions.is_stopped())
         self.assertDictEqual(broker._sessions, {})
         MockPluginManager.assert_has_calls([
             call().fire_event(EVENT_BROKER_CLIENT_CONNECTED,
                               client_id=client.session.client_id),
             call().fire_event(EVENT_BROKER_CLIENT_DISCONNECTED,
                               client_id=client.session.client_id)
         ],
                                            any_order=True)
         future.set_result(True)
     except Exception as ae:
         future.set_exception(ae)
Ejemplo n.º 15
0
        def test_coro():
            try:
                broker = Broker(test_config,
                                plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())
                sub_client = MQTTClient()
                yield from sub_client.connect('mqtt://127.0.0.1')
                ret = yield from sub_client.subscribe([('/qos0', QOS_0),
                                                       ('/qos1', QOS_1),
                                                       ('/qos2', QOS_2)])
                self.assertEqual(ret, [QOS_0, QOS_1, QOS_2])

                yield from self._client_publish('/qos0', b'data', QOS_0)
                yield from self._client_publish('/qos1', b'data', QOS_1)
                yield from self._client_publish('/qos2', b'data', QOS_2)
                yield from asyncio.sleep(0.1)
                for qos in [QOS_0, QOS_1, QOS_2]:
                    message = yield from sub_client.deliver_message()
                    self.assertIsNotNone(message)
                    self.assertEqual(message.topic, '/qos%s' % qos)
                    self.assertEqual(message.data, b'data')
                    self.assertEqual(message.qos, qos)
                yield from sub_client.disconnect()
                yield from asyncio.sleep(0.1)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
Ejemplo n.º 16
0
        def test_coro():
            try:
                broker = Broker(test_config,
                                plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())
                sub_client = MQTTClient()
                yield from sub_client.connect('mqtt://127.0.0.1')
                ret = yield from sub_client.subscribe([('+/monitor/Clients',
                                                        QOS_0)])
                self.assertEqual(ret, [QOS_0])

                yield from self._client_publish('/test/monitor/Clients',
                                                b'data', QOS_0)
                message = yield from sub_client.deliver_message()
                self.assertIsNotNone(message)

                yield from self._client_publish('$SYS/monitor/Clients',
                                                b'data', QOS_0)
                yield from asyncio.sleep(0.1)
                message = None
                try:
                    message = yield from sub_client.deliver_message(timeout=2)
                except Exception as e:
                    pass
                self.assertIsNone(message)
                yield from sub_client.disconnect()
                yield from asyncio.sleep(0.1)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
Ejemplo n.º 17
0
        def test_coro():
            try:
                broker = Broker(test_config,
                                plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())

                pub_client = MQTTClient()
                ret = yield from pub_client.connect('mqtt://127.0.0.1/')
                self.assertEqual(ret, 0)
                yield from pub_client.publish('/topic',
                                              b'data',
                                              QOS_0,
                                              retain=True)
                yield from pub_client.disconnect()
                yield from asyncio.sleep(0.1)
                self.assertIn('/topic', broker._retained_messages)
                retained_message = broker._retained_messages['/topic']
                self.assertEqual(retained_message.source_session,
                                 pub_client.session)
                self.assertEqual(retained_message.topic, '/topic')
                self.assertEqual(retained_message.data, b'data')
                self.assertEqual(retained_message.qos, QOS_0)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
Ejemplo n.º 18
0
        def test_coro():
            try:
                broker = Broker(test_config,
                                plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())
                pub_client = MQTTClient()
                ret = yield from pub_client.connect('mqtt://127.0.0.1/')
                self.assertEqual(ret, 0)

                ret_message = yield from pub_client.publish(
                    '/topic', bytearray(b'\x99' * 256 * 1024), QOS_2)
                yield from pub_client.disconnect()
                self.assertEqual(broker._retained_messages, {})

                yield from asyncio.sleep(0.1)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                MockPluginManager.assert_has_calls([
                    call().fire_event(EVENT_BROKER_MESSAGE_RECEIVED,
                                      client_id=pub_client.session.client_id,
                                      message=ret_message),
                ],
                                                   any_order=True)
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
Ejemplo n.º 19
0
        def test_coro():
            try:
                broker = Broker(test_config,
                                plugin_namespace="hbmqtt.test.plugins")
                yield from broker.start()
                self.assertTrue(broker.transitions.is_started())
                client = MQTTClient()
                ret = yield from client.connect('mqtt://127.0.0.1/')
                self.assertEqual(ret, 0)
                yield from client.subscribe([('/topic', QOS_0)])

                # Test if the client test client subscription is registered
                self.assertIn('/topic', broker._subscriptions)
                subs = broker._subscriptions['/topic']
                self.assertEqual(len(subs), 1)
                (s, qos) = subs[0]
                self.assertEqual(s, client.session)
                self.assertEqual(qos, QOS_0)

                yield from client.disconnect()
                yield from asyncio.sleep(0.1)
                yield from broker.shutdown()
                self.assertTrue(broker.transitions.is_stopped())
                MockPluginManager.assert_has_calls([
                    call().fire_event(EVENT_BROKER_CLIENT_SUBSCRIBED,
                                      client_id=client.session.client_id,
                                      topic='/topic',
                                      qos=QOS_0)
                ],
                                                   any_order=True)
                future.set_result(True)
            except Exception as ae:
                future.set_exception(ae)
Ejemplo n.º 20
0
async def test_client_publish_retain_delete(broker):
    pub_client = MQTTClient()
    ret = await pub_client.connect("mqtt://127.0.0.1/")
    assert ret == 0
    await pub_client.publish("/topic", b"", QOS_0, retain=True)
    await pub_client.disconnect()
    await asyncio.sleep(0.1)
    assert "/topic" not in broker._retained_messages
Ejemplo n.º 21
0
 def __init__(self, config):
     self.config = config
     self.client = MQTTClient(
         client_id=config.mqtt_client_id,
         config={
             'keep_alive': config.mqtt_keep_alive,
         },
     )
Ejemplo n.º 22
0
 async def __aenter__(self):
     self._cli = MQTTClient(client_id=self._client_id)
     with ir_path("eufy_security.mqtt", "eufy.crt") as crt:
         await self._cli.connect(
             f"mqtts://{self._username}:{self._password}@security-mqtt.eufylife.com:8789",
             cafile=str(crt))
     await self._cli.subscribe([(self._topic, QOS_1)])
     return self
Ejemplo n.º 23
0
 def __init__(self, on_message_cb, port=MQTT_PORT, max_time=MAX_TIME):
     # on_message_cb = send_to_broker method in gateway application
     self._on_message_cb = on_message_cb
     self.port = port
     self.max_time = max_time
     self.nodes = {}
     self.mqtt_client = MQTTClient()
     asyncio.get_event_loop().create_task(self.start())
Ejemplo n.º 24
0
async def test_connect_ws():
    broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
    await broker.start()
    client = MQTTClient()
    await client.connect("ws://127.0.0.1:8080/")
    assert client.session is not None
    await client.disconnect()
    await broker.shutdown()
Ejemplo n.º 25
0
 def __init__(self, pin, ipAddress, networkId, asyncio_loop):
     self.loop = asyncio_loop
     self.pin = pin
     self.networkId = networkId
     self.ipAddress = ipAddress
     self.topic = 'devices/io-expander/{id}/digital-input/{pin}'.format(
         id=self.networkId, pin=self.pin)
     self.client = MQTTClient()
Ejemplo n.º 26
0
 async def test_coro():
     try:
         client = MQTTClient()
         await client.connect("mqtt://test.mosquitto.org/")
         self.assertIsNotNone(client.session)
         await client.disconnect()
         future.set_result(True)
     except Exception as ae:
         future.set_exception(ae)
Ejemplo n.º 27
0
 def test_coro():
     try:
         client = MQTTClient()
         yield from client.connect('mqtt://test.mosquitto.org/')
         self.assertIsNotNone(client.session)
         yield from client.disconnect()
         future.set_result(True)
     except Exception as ae:
         future.set_exception(ae)
Ejemplo n.º 28
0
def publisher():
    C = MQTTClient()
    ret = C.connect('mqtt://127.0.0.1:1883/')
    message = C.publish('/slack_in', b'TEST MESSAGE WITH QOS_0', qos=QOS_0)
    message = C.publish('/slack_in', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)
    message = C.publish('/slack_in', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)
    print(message)
    print("messages published")
    C.disconnect()
Ejemplo n.º 29
0
async def pool(n, url):
    clients = [MQTTClient() for _ in range(n)]
    try:
        await asyncio.gather(*[client.connect(url) for client in clients])
        yield clients
    except ConnectException:
        logging.exception(f"Could not connect to {url}")
    finally:
        await asyncio.gather(*[client.disconnect() for client in clients])
Ejemplo n.º 30
0
async def connect(url):
    client = MQTTClient()
    try:
        await client.connect(url)
        yield client
    except ConnectException:
        logging.exception(f"Could not connect to {url}")
    finally:
        await client.disconnect()