2015-08-02 14:01:16 +00:00
|
|
|
import logging
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from hbmqtt.client import MQTTClient
|
2016-07-31 16:29:41 +00:00
|
|
|
from hbmqtt.mqtt.constants import QOS_1, QOS_2
|
2015-08-02 14:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# This sample shows how to publish messages to broker using different QOS
|
|
|
|
# Debug outputs shows the message flows
|
|
|
|
#
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
config = {
|
2021-03-14 21:16:51 +00:00
|
|
|
"will": {
|
|
|
|
"topic": "/will/client",
|
|
|
|
"message": b"Dead or alive",
|
|
|
|
"qos": 0x01,
|
|
|
|
"retain": True,
|
2015-08-06 19:08:22 +00:00
|
|
|
},
|
2021-03-14 21:16:51 +00:00
|
|
|
"capath": ".",
|
2015-08-02 14:01:16 +00:00
|
|
|
}
|
2015-08-06 19:08:22 +00:00
|
|
|
C = MQTTClient(config=config)
|
2021-03-14 21:16:51 +00:00
|
|
|
# C = MQTTClient()
|
2015-08-02 14:01:16 +00:00
|
|
|
|
2017-08-06 22:06:57 +00:00
|
|
|
|
2020-12-31 00:16:45 +00:00
|
|
|
async def test_coro():
|
2021-03-14 21:16:51 +00:00
|
|
|
await C.connect("wss://test.mosquitto.org:8081/", cafile="mosquitto.org.crt")
|
2015-08-02 14:01:16 +00:00
|
|
|
tasks = [
|
2021-03-14 21:16:51 +00:00
|
|
|
asyncio.ensure_future(C.publish("a/b", b"TEST MESSAGE WITH QOS_0")),
|
|
|
|
asyncio.ensure_future(C.publish("a/b", b"TEST MESSAGE WITH QOS_1", qos=QOS_1)),
|
|
|
|
asyncio.ensure_future(C.publish("a/b", b"TEST MESSAGE WITH QOS_2", qos=QOS_2)),
|
2015-08-02 14:01:16 +00:00
|
|
|
]
|
2020-12-31 00:16:45 +00:00
|
|
|
await asyncio.wait(tasks)
|
2015-08-02 14:01:16 +00:00
|
|
|
logger.info("messages published")
|
2020-12-31 00:16:45 +00:00
|
|
|
await C.disconnect()
|
2015-08-02 14:01:16 +00:00
|
|
|
|
|
|
|
|
2021-03-14 21:16:51 +00:00
|
|
|
if __name__ == "__main__":
|
2015-08-02 14:01:16 +00:00
|
|
|
formatter = "[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
2017-08-06 22:06:57 +00:00
|
|
|
asyncio.get_event_loop().run_until_complete(test_coro())
|