thingsboard / thingsboard-python-client-sdk

ThingsBoard client Python SDK
https://thingsboard.io
Other
111 stars 64 forks source link

Can't publish data with timestamp (ts) through gw_send_telemetry #38

Open zsamora opened 1 year ago

zsamora commented 1 year ago

Everything works fine if the timestamp is not included, but when I try to use the function from https://thingsboard.io/docs/reference/python-client-sdk/#using-gateway-apis it gives me an error in ThingsBoard platform logs (using sudo journalctl -u thingsboard.service)

Example that works: from time import time from tb_gateway_mqtt import TBGatewayMqttClient gateway = TBGatewayMqttClient("THINGSBOARD_PLATFORM_IP", 1883, "GATEWAY_ACCESS_TOKEN") gateway.connect() gateway.gw_send_telemetry("DEVICE_ID", {"telemetryKey": "telemetryValue"})

Example that doesn't work: from time import time from tb_gateway_mqtt import TBGatewayMqttClient gateway = TBGatewayMqttClient("THINGSBOARD_PLATFORM_IP", 1883, "GATEWAY_ACCESS_TOKEN")<br /> gateway.connect() gateway.gw_send_telemetry("DEVICE_ID", {"ts": int(round(time()*1000)), "values": {"telemetryKey": "telemetryValue"}})

When gw_send_telemetry is called in the second example, it gives me this in logs: Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: Exception in thread "MqttTransportContext-50-2" com.google.gson.JsonSyntaxException: Can't parse value: {"040100":{"ts":1677701139953,"testTelemetry":3}} Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler$3.onSuccess(GatewaySessionHandler.java:353) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler$3.onSuccess(GatewaySessionHandler.java:349) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1080) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) Mar 01 20:06:01 ip-172-31-34-70 thingsboard.jar[1750750]: at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)

In my opinion, the problem is the format of the message, because it doesn't add the "telemetry": needed for this kind of input value, but maybe it's another issue that I'm not seeing. Any help would be appreciated

zsamora commented 1 year ago

It worked while using: [{"ts": int(round(time()*1000)), "values": {"telemetryKey": "telemetryValue"}}]. So maybe you should correct the example in the link or removing the third condition in the function (telemetry.get("ts") is not None)

Makodan commented 4 months ago

Wow, I spent a lot of time resolving this. @zsamora Thank you, this is the solution. Array of dictionaries has to be used: sensor_data = [{"ts": 1714591946064, "values":{"node_rx_RSSI": 1, "gw_powerlevel": 2, "node_tx_RSSI": 3, "powerlevel": 4, "voltage": 5}}] Assignment: sensor_data[0]['ts'] = int(time.time())*1000 Sending the telemetry: gateway.gw_send_telemetry("some_device_name", sensor_data)