thingsboard / thingsboard-gateway

Open-source IoT Gateway - integrates devices connected to legacy and third-party systems with ThingsBoard IoT Platform using Modbus, CAN bus, BACnet, BLE, OPC-UA, MQTT, ODBC and REST protocols
https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
Apache License 2.0
1.74k stars 844 forks source link

[BUG] Requesting multiple attributes over MQTT does not work #801

Closed jelacious closed 2 years ago

jelacious commented 2 years ago

Describe the bug Note: I have not tested this with ThingsBoard gateway itself, but seeing as the MQTT code is identical to the one found in the Python Client SDK I do not see why it would behave any differently.

Trying to request more than one key over MQTT results in getting no value field. This is due to tb_device_mqtt.py#L305 not assembling correct messages nor spitting out proper errors upon receiving malformed input data. This forces you to have to make a list with only one string attribute, since then the function behaves like advertised and returns a message in the callback with a value field, resulting in numerous extra requests having to be made.

This issue has been raised since August last year: https://github.com/thingsboard/thingsboard-python-client-sdk/issues/11

Here is an example of code that would not work as expected:

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

import logging
import time

from tb_gateway_mqtt import TBGatewayMqttClient
logging.basicConfig(level=logging.DEBUG)

def callback(client, result, exception):
    client.stop()
    if exception is not None:
        print("Exception: " + str(exception))
    else:
        print(result)

def main():
    gateway = TBGatewayMqttClient("127.0.0.1", "SOME_TEST_KEY")
    gateway.connect()
    gateway.gw_request_client_attributes("I7LDT_2", ["valueOne", "valueTwo"], callback)

    while not gateway.stopped:
        time.sleep(1)

if __name__ == '__main__':
    main()

Connector name (If bug in the some connector):

Error traceback (If available):

Versions (please complete the following information):

samson0v commented 2 years ago

Hi @jelacious, we will test and fixed it. Thanks for your interest in ThingsBoard IoT Gateway!