2015-06-27 19:38:04 +00:00
|
|
|
import logging
|
|
|
|
import asyncio
|
2015-06-18 17:41:12 +00:00
|
|
|
|
2015-08-12 16:46:56 +00:00
|
|
|
from hbmqtt.client import MQTTClient, ConnectException
|
2015-11-11 21:03:32 +00:00
|
|
|
from hbmqtt.mqtt.constants import *
|
2015-07-07 19:55:17 +00:00
|
|
|
|
2015-07-06 20:19:31 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# This sample shows how to publish messages to broker using different QOS
|
|
|
|
# Debug outputs shows the message flows
|
|
|
|
#
|
|
|
|
|
2015-06-28 20:48:07 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-21 19:41:46 +00:00
|
|
|
config = {
|
|
|
|
'will': {
|
|
|
|
'topic': '/will/client',
|
|
|
|
'message': b'Dead or alive',
|
|
|
|
'qos': 0x01,
|
|
|
|
'retain': True
|
|
|
|
}
|
|
|
|
}
|
2015-08-11 20:20:03 +00:00
|
|
|
|
|
|
|
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-11-11 21:03:32 +00:00
|
|
|
C = MQTTClient()
|
|
|
|
yield from C.connect('mqtt://test.mosquitto.org/')
|
2015-06-28 20:48:07 +00:00
|
|
|
tasks = [
|
2015-10-16 20:13:37 +00:00
|
|
|
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_0')),
|
2015-11-11 21:03:32 +00:00
|
|
|
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-06-28 20:48:07 +00:00
|
|
|
]
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from asyncio.wait(tasks)
|
2015-06-28 20:48:07 +00:00
|
|
|
logger.info("messages published")
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from C.disconnect()
|
2015-06-27 19:38:04 +00:00
|
|
|
|
|
|
|
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro2():
|
2015-08-12 16:46:56 +00:00
|
|
|
try:
|
2015-11-11 21:03:32 +00:00
|
|
|
C = MQTTClient()
|
|
|
|
ret = yield from C.connect('mqtt://test.mosquitto.org:1883/')
|
2015-11-01 14:58:20 +00:00
|
|
|
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_0', qos=0x00)
|
|
|
|
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=0x01)
|
|
|
|
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=0x02)
|
2015-09-10 20:41:11 +00:00
|
|
|
#print(message)
|
2015-08-12 16:46:56 +00:00
|
|
|
logger.info("messages published")
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from C.disconnect()
|
2015-08-12 16:46:56 +00:00
|
|
|
except ConnectException as ce:
|
|
|
|
logger.error("Connection failed: %s" % ce)
|
|
|
|
asyncio.get_event_loop().stop()
|
2015-08-11 20:20:03 +00:00
|
|
|
|
|
|
|
|
2015-06-27 19:38:04 +00:00
|
|
|
if __name__ == '__main__':
|
2015-08-20 19:42:56 +00:00
|
|
|
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
2015-11-11 21:03:32 +00:00
|
|
|
formatter = "%(message)s"
|
2015-07-25 21:21:25 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
2015-11-11 21:03:32 +00:00
|
|
|
asyncio.get_event_loop().run_until_complete(test_coro())
|
|
|
|
asyncio.get_event_loop().run_until_complete(test_coro2())
|