async def main(broker_host, broker_port, token): # this message will be published by broker after client disconnects with "bad" code after 10 sec will_message = gmqtt.Message('TEST/WILL/42', "I'm dead finally", will_delay_interval=10) will_client = gmqtt.Client("clientgonnadie", will_message=will_message) assign_callbacks_to_client(will_client) will_client.set_auth_credentials(token, None) await will_client.connect(broker_host, broker_port) another_client = gmqtt.Client("clientgonnalisten") assign_callbacks_to_client(another_client) another_client.set_auth_credentials(token, None) await another_client.connect(broker_host, broker_port) another_client.subscribe('TEST/#') # reason code 4 - Disconnect with Will Message await will_client.disconnect(reason_code=4, reason_string="Smth went wrong") await STOP.wait() await another_client.disconnect()
async def main(broker_host, broker_port, token): # create client instance, kwargs (session expiry interval and maximum packet size) # will be send as properties in connect packet sub_client = gmqtt.Client("NCAP-req", session_expiry_interval=600, maximum_packet_size=65535) sub_client.set_auth_credentials(username='******', password='******') assign_callbacks_to_client(sub_client) sub_client.set_auth_credentials(token, None) await sub_client.connect(broker_host, broker_port) # two overlapping subscriptions with different subscription identifiers sub_client.subscribe('/PRISM/NCAP/1451OPE', qos=1, subscription_identifier=1) pub_client = gmqtt.Client("NCAP-ack") pub_client.set_auth_credentials(username='******', password='******') assign_callbacks_to_client(pub_client) pub_client.set_auth_credentials(token, None) await pub_client.connect(broker_host, broker_port) await STOP.wait() await pub_client.disconnect() await sub_client.disconnect(session_expiry_interval=0)
async def main(broker_host, broker_port, token): # initiate first client subscribed to TEST/SHARED/# in group mytestgroup sub_clientA = gmqtt.Client("clientgonnasubA") assign_callbacks_to_client(sub_clientA) sub_clientA.set_auth_credentials(token, None) await sub_clientA.connect(broker_host, broker_port) sub_clientA.subscribe('$share/mytestgroup/TEST/SHARED/#') # initiate second client subscribed to TEST/SHARED/# in group mytestgroup sub_clientB = gmqtt.Client("clientgonnasubB") assign_callbacks_to_client(sub_clientB) sub_clientB.set_auth_credentials(token, None) await sub_clientB.connect(broker_host, broker_port) sub_clientB.subscribe('$share/mytestgroup/TEST/SHARED/#') # this client will publish messages to TEST/SHARED/... topics pub_client = gmqtt.Client("clientgonnapub") assign_callbacks_to_client(pub_client) pub_client.set_auth_credentials(token, None) await pub_client.connect(broker_host, broker_port) # some of this messages will be received by client sub_clientA, # and another part by client sub_clientB, approximately 50/50 for i in range(100): pub_client.publish('TEST/SHARED/{}'.format(i), i, user_property=('time', str(time.time()))) await STOP.wait() await pub_client.disconnect() await sub_clientA.disconnect(session_expiry_interval=0) await sub_clientB.disconnect(session_expiry_interval=0)
async def main(broker_host, broker_port, token): # create client instance, kwargs (session expiry interval and maximum packet size) # will be send as properties in connect packet sub_client = gmqtt.Client("clientgonnasub", session_expiry_interval=600, maximum_packet_size=65535) assign_callbacks_to_client(sub_client) sub_client.set_auth_credentials(token, None) await sub_client.connect(broker_host, broker_port) # two overlapping subscriptions with different subscription identifiers sub_client.subscribe('TEST/PROPS/#', qos=1, subscription_identifier=1) sub_client.subscribe('TEST/#', qos=0, subscription_identifier=2) pub_client = gmqtt.Client("clientgonnapub") assign_callbacks_to_client(pub_client) pub_client.set_auth_credentials(token, None) await pub_client.connect(broker_host, broker_port) # this message received by sub_client will have two subscription identifiers pub_client.publish('TEST/PROPS/42', '42 is the answer', qos=1, content_type='utf-8', message_expiry_interval=60, user_property=('time', str(time.time()))) # just another way to publish same message msg = gmqtt.Message('TEST/PROPS/42', '42 is the answer', qos=1, content_type='utf-8', message_expiry_interval=60, user_property=('time', str(time.time()))) pub_client.publish(msg) pub_client.publish('TEST/42', {42: 'is the answer'}, qos=1, content_type='json', message_expiry_interval=60, user_property=('time', str(time.time()))) await STOP.wait() await pub_client.disconnect() await sub_client.disconnect(session_expiry_interval=0)
async def main(broker_host, broker_port, token): # create client instance, kwargs (session expiry interval and maximum packet size) # will be send as properties in connect packet sub_client = gmqtt.Client("service-res", session_expiry_interval=600, maximum_packet_size=65535) assign_callbacks_to_client(sub_client) sub_client.set_auth_credentials(token, None) await sub_client.connect(broker_host, broker_port) # two overlapping subscriptions with different subscription identifiers sub_client.subscribe(restopic, qos=1, subscription_identifier=1) pub_client = gmqtt.Client("service-req") assign_callbacks_to_client(pub_client) pub_client.set_auth_credentials(token, None) await pub_client.connect(broker_host, broker_port) pub_client.publish(reqtopic, args.name, qos=1, content_type='utf-8', message_expiry_interval=60, response_topic=restopic, user_property=('time', str(time.time()))) await STOP.wait() await pub_client.disconnect() await sub_client.disconnect(session_expiry_interval=0)
async def main(broker_host, token): client = gmqtt.Client("consumer") client = await init_client(client, broker_host, token, on_message=on_message) client.subscribe(ALL_WORKERS_RESULT_TOPIC, qos=1) await STOP.wait() await client.disconnect()
async def test_async_on_message(init_clients): # redelivery on reconnect. When a QoS 1 or 2 exchange has not been completed, the server should retry the # appropriate MQTT packets messages = [] async def on_message(client, topic, payload, qos, properties): print('MSG', (topic, payload, qos, properties)) await asyncio.sleep(0.5) messages.append((topic, payload, qos, properties)) return 131 aclient, callback, bclient, callback2 = init_clients disconnect_client = gmqtt.Client('myclientid3', optimistic_acknowledgement=False, clean_session=False, session_expiry_interval=99999) disconnect_client.set_config({'reconnect_retries': 0}) disconnect_client.on_message = on_message disconnect_client.set_auth_credentials(username) await disconnect_client.connect(host=host, port=port) disconnect_client.subscribe(WILDTOPICS[6], 2) await asyncio.sleep(1) await aclient.connect(host, port) await asyncio.sleep(1) aclient.publish(TOPICS[1], b"", 1, retain=False) aclient.publish(TOPICS[3], b"", 2, retain=False) await asyncio.sleep(2) messages = [] await disconnect_client.reconnect() await asyncio.sleep(2) assert len(messages) == 2 await disconnect_client.disconnect()
async def cleanup(host, port=1883, username=None, password=None, client_ids=None, prefix=None): # clean all client state print("clean up starting") client_ids = client_ids or (prefix + "myclientid", prefix + "myclientid2", prefix + "myclientid3") for clientid in client_ids: curclient = gmqtt.Client(clientid.encode("utf-8"), clean_session=True) curclient.set_auth_credentials(username=username, password=password) await curclient.connect(host=host, port=port) time.sleep(.1) await curclient.disconnect() time.sleep(.1) # clean retained messages await clean_retained(host, port, username, password=password, prefix=prefix) print("clean up finished")
async def connect(self): self._client = mqtt.Client(client_id=self._cfg["mqtt"].get("client_id", "mqtt-cmd")) self._client.on_connect = self.on_connect self._client.on_message = self.on_message self._client.on_disconnect = self.on_disconnect await self._client.connect(self._cfg["mqtt"]["host"], self._cfg["mqtt"].get("port", 1883), keepalive=60) logging.debug(f"Connected to broker {self._cfg['mqtt']['host']}")
async def initialize_telematics_connection(self): will_message = gmqtt.Message( config.TELEMATICS_MQTT_APPLICATION_ALERTS_TOPIC, "Unexpected Exit.", will_delay_interval=10, qos=1, retain=True, ) self.telematics_client = gmqtt.Client( client_id="{}-{}".format(self.__class__.__name__, self.telematics_application_id), clean_session=True, optimistic_acknowledgement=True, will_message=will_message, ) Telematics._assign_callbacks_to_client_telematics( self.telematics_client) if self.telematics_auth_token: self.telematics_client.set_auth_credentials( self.telematics_auth_token, None) await self.telematics_client.connect( self.telematics_broker_host, self.telematics_port, ssl=self.telematics_ssl_enabled, version=MQTTv311, raise_exc=True, keepalive=60, )
async def main(args=None): opts = open_config_file() options_mqtt = opts.brokers.northbound options_alert_digester = opts.alert_digester options_db = opts.local_storage options_tracer = opts.tracer options_alert_digester['client_id'] = uuid.uuid4() client = gmqtt.Client( f"{__service_name__}_{options_alert_digester['client_id']}", clean_session=True) await client.connect(host=options_mqtt['host'], port=options_mqtt['port'], version=4) logger = AlertDigester(options_alert_digester) logger.register_for_client(client) logger.set_up_local_storage(options_db) if options_tracer['enabled'] is True: options_tracer['service'] = __service_name__ logging.info(f'Enabling tracing for service "{__service_name__}"') logger.set_up_tracer(options_tracer) logger.set_topics([options_alert_digester['listen']]) logger.subscribe_to_topics() await STOP.wait() await client.disconnect()
async def main(broker_host, token): client_id = f"producer-{uuid.uuid4().hex}" client = gmqtt.Client(client_id) client = await init_client(client, broker_host, token) asyncio.ensure_future(send_messages(client)) await STOP.wait() await client.disconnect()
async def init_clients(): await cleanup(host, port, username) a_client = gmqtt.Client("myclientid", clean_session=True) a_client.set_auth_credentials(username) callback = Callbacks() callback.register_for_client(a_client) b_client = gmqtt.Client("myclientid2", clean_session=True) b_client.set_auth_credentials(username) callback2 = Callbacks() callback2.register_for_client(b_client) yield a_client, callback, b_client, callback2 await a_client.disconnect() await b_client.disconnect()
async def test_assigned_clientid(): noidclient = gmqtt.Client("", clean_session=True) noidclient.set_auth_credentials(username) callback = Callbacks() callback.register_for_client(noidclient) await noidclient.connect(host=host, port=port) await noidclient.disconnect() assert callback.connack[2]['assigned_client_identifier'][0] != ""
def __init__(self, broker_url, port, user_name, password, topics, on_message): self._broker_url = broker_url self._port = port self._client = gmqtt.Client("mqtt-client") self._client.set_auth_credentials(user_name, password) self._client.on_connect = self._on_connect self._client.on_message = on_message self._topics = topics
def __init__(self, address): if not HAS_MQTT: raise RuntimeError( "Not MQTT client installed! Please install paho.mqtt") super().__init__() self._result_fut = None self.address = address self._client = mqtt.Client(self.CLIENT_ID) self.loop.create_task(self._connect())
async def init_clients(): await cleanup(host, port, username) a_client = gmqtt.Client('%s_chariot_southbound_dispatcher' % uuid.uuid4(), clean_session=True) a_client.set_auth_credentials(username) b_client = gmqtt.Client('%s_chariot_southbound_dispatcher' % uuid.uuid4(), clean_session=True) b_client.set_auth_credentials(username) callback = LogDigester(options_dispatcher) callback.register_for_client(a_client) callback_b = Callbacks() callback_b.register_for_client(b_client) yield a_client, callback, b_client, callback_b await a_client.disconnect() await b_client.disconnect()
async def main(broker_host, token): client = gmqtt.Client("balancer") client = await init_client(client, broker_host, token, on_message=on_message) client.subscribe(BALANCER_TOPIC, qos=1) client.subscribe(WORKER_REGISTRATION_TOPIC, qos=1) client.subscribe(WORKER_UNREGISTER_TOPIC, qos=1) asyncio.ensure_future(send_messages(client)) await STOP.wait() await client.disconnect()
async def clean_retained(host, port, username, password=None, prefix=None): def on_message(client, topic, payload, qos, properties): curclient.publish(topic, b"", qos=0, retain=True) curclient = gmqtt.Client(prefix + "cleanretained", clean_session=True) curclient.set_auth_credentials(username, password) curclient.on_message = on_message await curclient.connect(host=host, port=port) topic = '#' if not prefix else prefix + '#' curclient.subscribe(topic) await asyncio.sleep(10) # wait for all retained messages to arrive await curclient.disconnect() time.sleep(.1)
async def create_connection(self): self.client = mqtt.Client("") self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_disconnect = self.on_disconnect self.client.on_subscribe = self.on_subscribe self.client.set_auth_credentials(self.MQTT_USER, self.MQTT_PWD) await self.client.connect(self.MQTT_HOST, self.MQTT_PORT) await self.STOP.wait() self.on_close() await self.client.disconnect()
async def test_shared_subscriptions(init_clients): aclient, callback, bclient, callback2 = init_clients shared_sub_topic = '$share/sharename/{}x'.format(PREFIX) shared_pub_topic = PREFIX + 'x' await aclient.connect(host=host, port=port) aclient.subscribe(shared_sub_topic) aclient.subscribe(TOPICS[0]) await bclient.connect(host=host, port=port) bclient.subscribe(shared_sub_topic) bclient.subscribe(TOPICS[0]) pubclient = gmqtt.Client(PREFIX + "myclient3", clean_session=True) pubclient.set_auth_credentials(username) await pubclient.connect(host, port) count = 10 for i in range(count): pubclient.publish(TOPICS[0], "message " + str(i), 0) j = 0 while len(callback.messages) + len( callback2.messages) < 2 * count and j < 20: await asyncio.sleep(1) j += 1 await asyncio.sleep(1) assert len(callback.messages) == count assert len(callback2.messages) == count callback.clear() callback2.clear() count = 10 for i in range(count): pubclient.publish(shared_pub_topic, "message " + str(i), 0) j = 0 while len(callback.messages) + len(callback2.messages) < count and j < 20: await asyncio.sleep(1) j += 1 await asyncio.sleep(1) # Each message should only be received once assert len(callback.messages) + len(callback2.messages) == count assert len(callback.messages) > 0 assert len(callback2.messages) > 0 await pubclient.disconnect()
async def test_will_message(init_clients): aclient, callback, bclient, callback2 = init_clients # re-initialize aclient with will message will_message = gmqtt.Message(TOPICS[2], "I'm dead finally") aclient = gmqtt.Client("myclientid3", clean_session=True, will_message=will_message) aclient.set_auth_credentials(username) await aclient.connect(host, port=port) await bclient.connect(host=host, port=port) bclient.subscribe(TOPICS[2]) await asyncio.sleep(1) await aclient.disconnect(reason_code=4) await asyncio.sleep(1) assert len(callback2.messages) == 1
async def main(broker_host, token): will_message = gmqtt.Message(WORKER_UNREGISTER_TOPIC, worker.worker_hex, will_delay_interval=2) client = gmqtt.Client(f'worker-{worker.worker_hex}', will_message=will_message) client = await init_client(client, broker_host, token, on_message=on_message) client.publish(WORKER_REGISTRATION_TOPIC, worker.worker_hex, qos=1) client.subscribe(WORKER_REGISTRED_TOPIC, qos=1) await STOP.wait() await client.disconnect(reason_code=4 ) # reason code 4 - disconnect with Will Message
async def main(): client_id = '%s_chariot_southbound_dispatcher' % uuid.uuid4() broker = '192.168.174.130' port = '1883' client = gmqtt.Client(client_id, clean_session=True) await client.connect(host=broker, port=port, version=4) log = {'d': {'temperature': -10.0, 'humidity': 40.0}} callback = MessageGenerator() callback.register_for_client(client) callback.publish("dispatcher/device%s" % uuid.uuid4(), json.dumps(log)) await asyncio.sleep(1)
async def main(broker_host): will_message = gmqtt.Message(status_topic, msg_dead, retain=True, will_delay_interval=will_delay) client = gmqtt.Client(client_id, will_message=will_message) assign_callbacks_to_client(client) await client.connect(broker_host) print('Publish message indicating current status...') client.publish(status_topic, msg_on, retain=True) print('Subscribe topic to receive request messages...') client.subscribe(teds_topic, no_local=True) await STOP.wait() client.publish(status_topic, msg_off, retain=True) await client.disconnect()
def define_mqtt_client(self, client_id): will_message = gmqtt.Message( "brokers/{}/alerts/service/{}/disconnected".format( client_id, self.__class__.__name__ ), "Unexpected Exit.", will_delay_interval=10, qos=1, retain=False, ) self.mqtt_client = gmqtt.Client( client_id=client_id, clean_session=True, optimistic_acknowledgement=True, will_message=will_message, ) self.mqtt_client.on_connect = self.on_connect self.mqtt_client.on_message = self.on_message self.mqtt_client.on_disconnect = self.on_disconnect self.mqtt_client.on_subscribe = self.on_subscribe
async def create_client(options, postfix='_client'): """ Create a new GMQTT client :param options: Options for client initialization :para postfix: Unique postfix for the client """ client_id = '%s%s' % (uuid.uuid4(), postfix) client = gmqtt.Client(client_id, session_expiry_interval=60, clean_session=True) client.on_connect = on_connect client.on_disconnect = on_disconnect client.set_auth_credentials(options.get('username', '')) await client.connect(host=options['host'], port=options['port'], keepalive=60, version=MQTTv311) return client
async def main(): print('start') if sys.version_info[0] != 3: print("Version 3 is required") print('MQTT setup') node = uuid.getnode() mac = uuid.UUID(int=node) addr = mac.hex[-12:] print('Client ID=' + addr) client = gmqtt.Client(addr) # default is MQTTv5, Client ID is required await client.connect(host, port, keepalive=60) print('ALPS setup') alpsarray = [] n = 1 while True: keyname = 'alpsmodule' + str(n) if keyname in confdata: if confdata[keyname] is not None: print("No.%d : %s" % (n, confdata[keyname])) alpsarray.append(AlpsSensor(confdata[keyname])) n += 1 else: break for i, a in enumerate(alpsarray): a.setDelegate(NtfyDelegate(btle.DefaultDelegate, i + 1, client)) print("node:", i + 1) #Hybrid MAG ACC8G 100ms / Other 1s # code - meaning a.writeCharacteristic(0x0013, struct.pack('<bb', 0x01, 0x00), True) # Custom1 Notify Enable a.writeCharacteristic(0x0016, struct.pack('<bb', 0x01, 0x00), True) # Custom2 Notify Enable a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x2F, 0x03, 0x03), True) # (不揮発)保存内容の初期化 a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x01, 0x03, 0x7F), True) # 地磁気、加速度,気圧,温度,湿度,UV,照度を有効 a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x04, 0x03, 0x04), True) # Hybrid Mode # a.writeCharacteristic(0x0018, struct.pack('<bbbb', 0x06, 0x04, 0x64, 0x00), True) # Fast 100msec (地磁気,加速度) fastsample = confdata.get('fastsample') if fastsample is not None: faststime = abs(int(fastsample)) if (faststime > 999): faststime = 999 fstimel = faststime % 128 fstimeh = int(faststime / 128) fstimelstr = format(fstimel, '08x') fstimehstr = format(fstimeh, '08x') fsstrl = int(fstimelstr[-2:], 16) fsstrh = int(fstimehstr[-2:], 16) a.writeCharacteristic( 0x0018, struct.pack('<bbbb', 0x06, 0x04, fsstrl, fsstrh), True) else: a.writeCharacteristic(0x0018, struct.pack('<bbbb', 0x06, 0x04, 0x7A, 0x01), True) # Fast 250msec (地磁気,加速度) slowsample = confdata.get('slowsample') if slowsample is not None: slowstime = abs(int(slowsample)) if slowstime > 255: slowstime = 255 a.writeCharacteristic( 0x0018, struct.pack('<bbbb', 0x05, 0x04, slowstime, 0x00), True) else: a.writeCharacteristic(0x0018, struct.pack('<bbbb', 0x05, 0x04, 0x01, 0x00), True) # Slow 1sec (気圧,温度,湿度,UV,照度) a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x02, 0x03, 0x01), True) # 加速度±4G a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x2F, 0x03, 0x01), True) # 設定内容保存 a.writeCharacteristic(0x0018, struct.pack('<bbb', 0x20, 0x03, 0x01), True) # センサ計測開始 # Main loop -------- while True: for i, a in enumerate(alpsarray): if a.waitForNotifications(1.0): # handleNotification() was called continue print("Waiting...", i) # Perhaps do something else here client.disconnect()
import signal import gmqtt from metlog import MqttClient, Sun, ask_exit, init_db if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument("mqtt", help="MQTT broker address") parser.add_argument("db_file", help="Database file") parser.add_argument("--init", action="store_true", help="Initialise database") args = parser.parse_args() if args.init: init_db(args.db_file) sun = Sun(51.0, -1.6) mqtt = gmqtt.Client('metlog') mqtt_client = MqttClient(mqtt, args.db_file, sun) loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGINT, ask_exit) loop.add_signal_handler(signal.SIGTERM, ask_exit) loop.run_until_complete(mqtt_client.main(args.mqtt))