def test_publish_topic_alias(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK with topic alias 5 ('s2c', b'\x20\x06\x00\x00\x03\x22\x00\x05'), # PUBLISH to set alias ( 'c2s', b'\x30\x2c\x00\x12/test/mqttools/foo\x03\x23\x00\x01' b'sets-alias-in-broker' ), # PUBLISH using alias ('c2s', b'\x30\x1a\x00\x00\x03\x23\x00\x01published-with-alias'), # PUBLISH without alias ('c2s', b'\x30\x24\x00\x12/test/mqttools/fie\x00not-using-alias'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', topic_aliases=[ '/test/mqttools/foo' ], topic_alias_maximum=0, keep_alive_s=0) self.run_until_complete(client.start()) client.publish('/test/mqttools/foo', b'sets-alias-in-broker') client.publish('/test/mqttools/foo', b'published-with-alias') client.publish('/test/mqttools/fie', b'not-using-alias') self.run_until_complete(client.stop())
async def publisher(): client = mqttools.Client('localhost', 1883) await client.start() client.publish('/test/mqttools/foo', b'bar') await client.stop() print("Successfully published b'bar' on /test/mqttools/foo.")
def test_subscribe_retain_handling_values(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # SUBSCRIBE retain handling 0 ('c2s', b'\x82\n\x00\x01\x00\x00\x04/a/b\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x01\x00\x00'), # SUBSCRIBE retain handling 1 ('c2s', b'\x82\n\x00\x02\x00\x00\x04/a/c\x10'), # SUBACK ('s2c', b'\x90\x04\x00\x02\x00\x00'), # SUBSCRIBE retain handling 2 ('c2s', b'\x82\n\x00\x03\x00\x00\x04/a/d\x20'), # SUBACK ('s2c', b'\x90\x04\x00\x03\x00\x00'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0) self.run_until_complete(client.start()) self.run_until_complete(client.subscribe('/a/b', 0)) self.run_until_complete(client.subscribe('/a/c', 1)) self.run_until_complete(client.subscribe('/a/d', 2)) self.run_until_complete(client.stop())
async def publisher_doorbell_main(): print('running publisher doorbell') async with mqttools.Client(BROKER_ADDRESS, BROKER_PORT) as client: while True: ding = MSG_DING.encode('ascii') client.publish(TOPIC_DOORBELL, ding) await asyncio.sleep(10)
def test_use_all_topic_aliases(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK with topic alias 1 ('s2c', b'\x20\x06\x00\x00\x03\x22\x00\x01'), # PUBLISH to set alias ('c2s', b'\x30\x0d\x00\x04/foo\x03\x23\x00\x01apa'), # PUBLISH, no alias available ('c2s', b'\x30\x0a\x00\x04/bar\x00cat'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', topic_aliases=[ '/foo' ], topic_alias_maximum=0, keep_alive_s=0) self.run_until_complete(client.start()) client.publish('/foo', b'apa') client.publish('/bar', b'cat') self.run_until_complete(client.stop())
async def resume_session(): client = mqttools.Client('localhost', 1883, client_id='mqttools-{}'.format(uuid1().node), session_expiry_interval=15) try: await client.start(resume_session=True) print('Session resumed.') except mqttools.SessionResumeError: print('No session to resume. Subscribing to topics.') # Subscribe to three topics in parallel. print('Subscribing to topics.') await asyncio.gather(client.subscribe('$SYS/#'), client.subscribe('/test/mqttools/foo')) print('Waiting for messages.') while True: topic, message = await client.messages.get() if topic is None: print('Broker connection lost!') break print(f'Topic: {topic}') print(f'Message: {message}')
async def start_stop_clients(): subscriber = mqttools.Client('localhost', BROKER_PORT) publisher = mqttools.Client('localhost', BROKER_PORT) await subscriber.start() await subscriber.subscribe('/apa') await publisher.start() publisher.publish('/apa', b'halloj') await publisher.stop() message = await subscriber.messages.get() if message != ('/apa', b'halloj'): raise Exception('Wrong message {}'.format(message)) await subscriber.stop()
async def will(): client = mqttools.Client('localhost', 1883, will_topic='/my/will/topic', will_message=b'my-will-message', will_qos=0) await client.start() await client.stop() print("Successfully connected with will.")
async def reconnector(): client = mqttools.Client('localhost', 1883, subscriptions=['foobar'], connect_delays=[1, 2, 4, 8]) while True: await client.start() await handle_messages(client) await client.stop()
async def start_client(): client = mqttools.Client('localhost', BROKER_PORT) while True: try: await client.start() break except: print('Client start failed. Retrying...') await asyncio.sleep(0.2) return client
async def client(self): client = mqttools.Client(self._host, self._port, 'goo', response_timeout=1, topic_alias_maximum=0, keep_alive_s=1) for _ in range(6): await client.start() self.messages.append(await client.messages.get()) await client.stop()
async def subscriber_phone_main(): print('running subscriber phone') async with mqttools.Client(BROKER_ADDRESS, BROKER_PORT) as client: # subscribe to a topic await asyncio.gather(client.subscribe(TOPIC_DOORBELL)) while True: topic, message = await client.messages.get() print(f'Phone: Got {message} on {topic}.') if topic is None: print('Echo client connection lost.') break
async def publish_to_self(): client = mqttools.Client('localhost', 1883) await client.start() await client.subscribe('/test/mqttools/foo') client.publish('/test/mqttools/foo', b'publish_to_self message') topic, message = await client.messages.get() if topic is None: print('Broker connection lost!') else: print(f'Topic: {topic}') print(f'Message: {message}')
async def publisher(): client = mqttools.Client('localhost', 1883, topic_aliases=[ '/test/mqttools/foo' ]) await client.start() client.publish('/test/mqttools/foo', b'sets-alias-in-broker') client.publish('/test/mqttools/foo', b'published-with-alias') client.publish('/test/mqttools/fie', b'not-using-alias') client.publish('/test/mqttools/fie', b'not-using-alias') await client.stop() print("Successfully published b'bar' on /test/mqttools/foo.")
def test_receive_topic_alias(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT with topic alias 5 ( 'c2s', b'\x10\x13\x00\x04MQTT\x05\x02\x00\x00\x03\x22\x00\x05\x00\x03bar' ), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # SUBSCRIBE ('c2s', b'\x82\x18\x00\x01\x00\x00\x12/test/mqttools/foo\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x01\x00\x00'), # PUBLISH using an unknown alias 1 ( 's2c', b'\x30\x22\x00\x00\x03\x23\x00\x01published-with-unknown-alias' ), # PUBLISH using alias an invalid alias 6 ( 's2c', b'\x30\x34\x00\x12/test/mqttools/foo\x03\x23\x00\x06' b'sets-invalid-alias-in-client' ), # PUBLISH to set alias ( 's2c', b'\x30\x2c\x00\x12/test/mqttools/foo\x03\x23\x00\x01' b'sets-alias-in-client' ), # PUBLISH using alias ('s2c', b'\x30\x1a\x00\x00\x03\x23\x00\x01published-with-alias'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', topic_alias_maximum=5, keep_alive_s=0) self.run_until_complete(client.start()) self.run_until_complete(client.subscribe('/test/mqttools/foo')) topic, message = self.run_until_complete(client.messages.get()) self.assertEqual(topic, '/test/mqttools/foo') self.assertEqual(message, b'sets-alias-in-client') topic, message = self.run_until_complete(client.messages.get()) self.assertEqual(topic, '/test/mqttools/foo') self.assertEqual(message, b'published-with-alias') self.run_until_complete(client.stop())
def test_start_stop(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0) self.run_until_complete(client.start()) self.run_until_complete(client.stop())
def test_connack_unspecified_error(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK with unspecified error ('s2c', b'\x20\x03\x00\x80\x00') ] client = mqttools.Client(*self.broker.address, 'bar', topic_alias_maximum=0, connect_delays=[], keep_alive_s=0) with self.assertRaises(mqttools.ConnectError) as cm: self.run_until_complete(client.start()) self.assertEqual(str(cm.exception), 'UNSPECIFIED_ERROR(128)')
def test_publish_qos_0(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # PUBLISH ('c2s', b'\x30\x0a\x00\x04/a/b\x00apa'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0) self.run_until_complete(client.start()) client.publish('/a/b', b'apa') self.run_until_complete(client.stop())
async def subscriber(): client = mqttools.Client('localhost', 1883) await client.start() # Subscribe to two topics in parallel. await asyncio.gather(client.subscribe('$SYS/#'), client.subscribe('/test/mqttools/foo')) print('Waiting for messages.') while True: topic, message = await client.messages.get() if topic is None: print('Broker connection lost!') break print(f'Topic: {topic}') print(f'Message: {message}')
def test_subscribe(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # SUBSCRIBE ('c2s', b'\x82\n\x00\x01\x00\x00\x04/a/b\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x01\x00\x00'), # SUBSCRIBE ('c2s', b'\x82\n\x00\x02\x00\x00\x04/a/c\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x02\x00\x00'), # SUBSCRIBE with invalid topic ('c2s', b'\x82\x09\x00\x03\x00\x00\x03/a#\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x03\x00\xa2'), # PUBLISH QoS 0 ('s2c', b'\x30\x0a\x00\x04/a/b\x00apa'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0) self.run_until_complete(client.start()) self.run_until_complete(client.subscribe('/a/b')) self.run_until_complete(client.subscribe('/a/c')) with self.assertRaises(mqttools.SubscribeError) as cm: self.run_until_complete(client.subscribe('/a#')) self.assertEqual(cm.exception.reason, mqttools.SubackReasonCode.WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED) topic, message = self.run_until_complete(client.messages.get()) self.assertEqual(topic, '/a/b') self.assertEqual(message, b'apa') self.run_until_complete(client.stop())
async def main(): client = mqttools.Client(HOST, PORT) await client.start() print(f'Connected to {HOST}:{PORT}.') await client.subscribe('/mqttools/incrementer/value/request') print('Subscribed to topic /mqttools/incrementer/value/request.') while True: topic, message = await client.messages.get() if topic is None: print('Broker connection lost!') break count = int(message) print(f'Request count: {count}') count += 1 print(f'Response count: {count}') client.publish('/mqttools/counter-client/value/response', str(count).encode('ascii'))
def test_resume_session(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT with clean session 0 (to resume) and session # expiry interval 120. ( 'c2s', b'\x10\x15\x00\x04MQTT\x05\x00\x00\x00\x05\x11\x00\x00\x00\x78' b'\x00\x03bar' ), # CONNACK with no session present ('s2c', b'\x20\x03\x00\x00\x00'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00'), # CONNECT with clean session 0 (to resume) and session # expiry interval 120. ( 'c2s', b'\x10\x15\x00\x04MQTT\x05\x00\x00\x00\x05\x11\x00\x00\x00\x78' b'\x00\x03bar' ), # CONNACK with session present ('s2c', b'\x20\x03\x01\x00\x00'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', session_expiry_interval=120, topic_alias_maximum=0, connect_delays=[], keep_alive_s=0) with self.assertRaises(mqttools.SessionResumeError): self.run_until_complete(client.start(resume_session=True)) self.run_until_complete(client.stop()) self.broker.wait_for_client_closed() self.run_until_complete(client.start(resume_session=True)) self.run_until_complete(client.stop())
async def unsubscriber(): client = mqttools.Client('localhost', 1883, keep_alive_s=5) await client.start() print('Subscribing to /test/mqttools/foo.') await client.subscribe('/test/mqttools/foo') topic, message = await client.messages.get() print(f'Topic: {topic}') print(f'Message: {message}') print('Unsubscribing from /test/mqttools/foo.') await client.unsubscribe('/test/mqttools/foo') # Should only return when the broker connection is lost. topic, message = await client.messages.get() print(f'Topic: {topic}') print(f'Message: {message}')
def test_unsubscribe(self): Broker.EXPECTED_DATA_STREAM = [ # CONNECT ('c2s', b'\x10\x10\x00\x04MQTT\x05\x02\x00\x00\x00\x00\x03bar'), # CONNACK ('s2c', b'\x20\x03\x00\x00\x00'), # SUBSCRIBE ('c2s', b'\x82\n\x00\x01\x00\x00\x04/a/b\x00'), # SUBACK ('s2c', b'\x90\x04\x00\x01\x00\x00'), # UNSUBSCRIBE ('c2s', b'\xa2\x09\x00\x02\x00\x00\x04/a/b'), # UNSUBACK ('s2c', b'\xb0\x04\x00\x02\x00\x00'), # UNSUBSCRIBE from non-subscribed topic ('c2s', b'\xa2\x09\x00\x03\x00\x00\x04/a/d'), # UNSUBACK ('s2c', b'\xb0\x04\x00\x03\x00\x11'), # DISCONNECT ('c2s', b'\xe0\x02\x00\x00') ] client = mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0) self.run_until_complete(client.start()) self.run_until_complete(client.subscribe('/a/b')) self.run_until_complete(client.unsubscribe('/a/b')) with self.assertRaises(mqttools.UnsubscribeError) as cm: self.run_until_complete(client.unsubscribe('/a/d')) self.assertEqual(cm.exception.reason, mqttools.UnsubackReasonCode.NO_SUBSCRIPTION_EXISTED) self.run_until_complete(client.stop())
async def manager(): async with mqttools.Client(*self.broker.address, 'bar', keep_alive_s=0, topic_alias_maximum=0): pass
async def start_client(): client = mqttools.Client('localhost', BROKER_PORT, connect_delays=[0.1]) await client.start() return client
async def publisher(): async with mqttools.Client('localhost', 1883) as client: client.publish('/test/mqttools/foo', b'bar') print("Successfully published b'bar' on /test/mqttools/foo.")