thingsboard-python-client-sdk icon indicating copy to clipboard operation
thingsboard-python-client-sdk copied to clipboard

Connection must be established before sending any message

Open bodiroga opened this issue 6 years ago • 0 comments

Hi guys!

I have been playing with the library (many thanks for your work!) and I have discovered that the provided examples do not work well because the library send messages too fast.

This is the code I'm using:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
from tb_device_mqtt import TBDeviceMqttClient, TBPublishInfo

client = TBDeviceMqttClient("10.15.181.58", "xxxxxxxxxxxxxxxxxxxxxxxx")

client.connect()
# while not client._TBDeviceMqttClient__is_connected:
#     time.sleep(0.1)

result = client.send_telemetry({"temperature": 42.2})

client.disconnect()

And here you have what the Thingboard server reports:

2019-07-05 10:37:07,321 [nioEventLoopGroup-13-12] INFO  o.t.s.t.mqtt.MqttTransportHandler - [74b542dd-1890-4ab7-acae-99503d5b3847] Processing connect msg for client: !
2019-07-05 10:37:07,322 [nioEventLoopGroup-13-12] INFO  o.t.s.t.mqtt.MqttTransportHandler - [74b542dd-1890-4ab7-acae-99503d5b3847] Processing connect msg for client with user name: xxxxxxxxxxxxxxxxxxxxxxx!
2019-07-05 10:37:07,323 [nioEventLoopGroup-13-12] INFO  o.t.s.t.mqtt.MqttTransportHandler - [74b542dd-1890-4ab7-acae-99503d5b3847] Closing current session due to invalid msg order: MqttPublishMessage[fixedHeader=MqttFixedHeader[messageType=PUBLISH, isDup=false, qosLevel=AT_LEAST_ONCE, isRetain=false, remainingLength=50], variableHeader=MqttPublishVariableHeader[topicName=v1/devices/me/telemetry, packetId=1], payload=PooledSlicedByteBuf(ridx: 0, widx: 23, cap: 23/23, unwrapped: PooledUnsafeDirectByteBuf(ridx: 88, widx: 90, cap: 1024))]
2019-07-05 10:37:07,324 [nioEventLoopGroup-13-12] INFO  o.t.s.t.mqtt.MqttTransportHandler - [74b542dd-1890-4ab7-acae-99503d5b3847] Closing current session due to invalid msg order: MqttMessage[fixedHeader=MqttFixedHeader[messageType=DISCONNECT, isDup=false, qosLevel=AT_MOST_ONCE, isRetain=false, remainingLength=0], variableHeader=, payload=]
2019-07-05 10:37:07,327 [ForkJoinPool-9-worker-4] INFO  o.t.s.t.mqtt.MqttTransportHandler - [74b542dd-1890-4ab7-acae-99503d5b3847] Client connected!

As you can see, the session is closed before the client is connected, so the telemetry messages are not processed by the server.

Everything works fine after adding the while not client._TBDeviceMqttClient__is_connected: time.sleep(0.1), but that is just a hack and not a permanent solution. The first option is to expose the connection status with a getter function (client.is_connected()), but, otherwise, I am not sure if making the "connect()" function a blocking function is the right approach. How about buffering the messages until connection is established?

Thank you for your attention and for developing this project and best regards,

Aitor

bodiroga avatar Jul 05 '19 10:07 bodiroga