kopia lustrzana https://github.com/Yakifo/amqtt
60 wiersze
1.7 KiB
Python
60 wiersze
1.7 KiB
Python
import logging
|
|
import asyncio
|
|
|
|
from amqtt.client import MQTTClient, ConnectException
|
|
from amqtt.mqtt.constants import QOS_1, QOS_2
|
|
|
|
|
|
#
|
|
# This sample shows how to publish messages to broker using different QOS
|
|
# Debug outputs shows the message flows
|
|
#
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
config = {
|
|
"will": {
|
|
"topic": "/will/client",
|
|
"message": b"Dead or alive",
|
|
"qos": 0x01,
|
|
"retain": True,
|
|
}
|
|
}
|
|
|
|
|
|
async def test_coro():
|
|
C = MQTTClient()
|
|
await C.connect("mqtt://test.mosquitto.org/")
|
|
tasks = [
|
|
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)),
|
|
]
|
|
await asyncio.wait(tasks)
|
|
logger.info("messages published")
|
|
await C.disconnect()
|
|
|
|
|
|
async def test_coro2():
|
|
try:
|
|
C = MQTTClient()
|
|
await C.connect("mqtt://test.mosquitto.org:1883/")
|
|
await C.publish("a/b", b"TEST MESSAGE WITH QOS_0", qos=0x00)
|
|
await C.publish("a/b", b"TEST MESSAGE WITH QOS_1", qos=0x01)
|
|
await C.publish("a/b", b"TEST MESSAGE WITH QOS_2", qos=0x02)
|
|
logger.info("messages published")
|
|
await C.disconnect()
|
|
except ConnectException as ce:
|
|
logger.error("Connection failed: %s" % ce)
|
|
asyncio.get_event_loop().stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
formatter = (
|
|
"[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
|
)
|
|
formatter = "%(message)s"
|
|
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
|
asyncio.get_event_loop().run_until_complete(test_coro())
|
|
asyncio.get_event_loop().run_until_complete(test_coro2())
|