Ejemplo n.º 1
0
 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,
     )
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
 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