本文整理汇总了Python中hbmqtt.client.MQTTClient类的典型用法代码示例。如果您正苦于以下问题:Python MQTTClient类的具体用法?Python MQTTClient怎么用?Python MQTTClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MQTTClient类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_coro
def test_coro():
try:
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
yield from broker.start()
client = MQTTClient()
yield from client.connect('mqtt://127.0.0.1/')
self.assertIsNotNone(client.session)
ret = yield from client.subscribe([
('test_topic', QOS_0),
])
self.assertEqual(ret[0], QOS_0)
client_pub = MQTTClient()
yield from client_pub.connect('mqtt://127.0.0.1/')
yield from client_pub.publish('test_topic', data, QOS_0)
yield from client_pub.disconnect()
message = yield from client.deliver_message()
self.assertIsNotNone(message)
self.assertIsNotNone(message.publish_packet)
self.assertEqual(message.data, data)
yield from client.unsubscribe(['$SYS/broker/uptime'])
yield from client.disconnect()
yield from broker.shutdown()
future.set_result(True)
except Exception as ae:
future.set_exception(ae)
示例2: test_coro
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)
示例3: _client_publish
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
示例4: test_coro
def test_coro():
C = MQTTClient()
yield from 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)),
]
yield from asyncio.wait(tasks)
logger.info("messages published")
yield from C.disconnect()
示例5: test_coro2
def test_coro2():
try:
C = MQTTClient()
yield from C.connect('mqtt://test.mosquitto.org:1883/')
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_0', qos=0x00)
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=0x01)
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=0x02)
logger.info("messages published")
yield from C.disconnect()
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
示例6: test_coro
def test_coro():
try:
client = MQTTClient()
yield from client.connect('mqtt://test.mosquitto.org/')
self.assertIsNotNone(client.session)
ret = yield from client.subscribe([
('$SYS/broker/uptime', QOS_0),
])
self.assertEquals(ret[0], QOS_0)
yield from client.unsubscribe(['$SYS/broker/uptime'])
yield from client.disconnect()
future.set_result(True)
except Exception as ae:
future.set_exception(ae)
示例7: test_coro
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)
示例8: test_coro
def test_coro():
try:
C = MQTTClient()
yield from C.connect('mqtt://0.0.0.0:1883')
yield from C.publish('data/classified', b'TOP SECRET', qos=0x01)
yield from C.publish('data/memes', b'REAL FUN', qos=0x01)
yield from C.publish('repositories/hbmqtt/master', b'NEW STABLE RELEASE', qos=0x01)
yield from C.publish('repositories/hbmqtt/devel', b'THIS NEEDS TO BE CHECKED', qos=0x01)
yield from C.publish('calendar/hbmqtt/releases', b'NEW RELEASE', qos=0x01)
logger.info("messages published")
yield from C.disconnect()
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
示例9: main
def main(self):
client = MQTTClient()
yield from client.connect('mqtt://localhost:1883/')
for key in self.__registry.keys():
yield from client.subscribe([{'filter': key, 'qos': 0x00}])
while True:
packet = yield from client.deliver_message()
log.debug("%s : %s" % (packet.variable_header.topic_name, str(packet.payload.data)))
try:
#yield from self.getSensor("pressure-main").setValue(float(packet.payload.data))
self.update(packet.variable_header.topic_name, packet.payload.data)
except Exception as exception:
log.exception(exception)
yield from client.acknowledge_delivery(packet.variable_header.packet_id)
for key in self.__registry.keys():
yield from client.unsubscribe([key])
yield from client.disconnect()
示例10: uptime_coro
def uptime_coro():
C = MQTTClient()
yield from C.connect('mqtt://localhost/')
yield from C.subscribe([
('sputnik/test', QOS_1),
])
try:
for i in range(1, 100):
message = yield from C.deliver_message()
packet = message.publish_packet
print("%d: %s => %s" % (i, packet.variable_header.topic_name, str(packet.payload.data)))
yield from C.unsubscribe(['sputnik/test'])
yield from C.disconnect()
except ClientException as ce:
logger.error("Client exception: %s" % ce)
示例11: uptime_coro
def uptime_coro():
C = MQTTClient()
yield from C.connect('mqtt://test.mosquitto.org/')
# Subscribe to '$SYS/broker/uptime' with QOS=1
yield from C.subscribe([
('$SYS/broker/uptime', QOS_1),
('$SYS/broker/load/#', QOS_2),
])
logger.info("Subscribed")
try:
for i in range(1, 100):
message = yield from C.deliver_message()
packet = message.publish_packet
print("%d: %s => %s" % (i, packet.variable_header.topic_name, str(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)
示例12: uptime_coro
def uptime_coro():
C = MQTTClient()
yield from C.connect('mqtt://test:[email protected]:1883')
# yield from C.connect('mqtt://0.0.0.0:1883')
# Subscribe to '$SYS/broker/uptime' with QOS=1
yield from C.subscribe([
('data/memes', QOS_1), # Topic allowed
('data/classified', QOS_1), # Topic forbidden
('repositories/hbmqtt/master', QOS_1), # Topic allowed
('repositories/hbmqtt/devel', QOS_1), # Topic forbidden
('calendar/hbmqtt/releases', QOS_1), # Topic allowed
])
logger.info("Subscribed")
try:
for i in range(1, 100):
message = yield from C.deliver_message()
packet = message.publish_packet
print("%d: %s => %s" % (i, packet.variable_header.topic_name, str(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)