0

我尝试使用 IoT 网关从外部 MQTT 代理主题接收简单的文本值。

为此,我简化了现有脚本 ( extensions/mqtt/custom_mqtt_uplink_converter.py):

from thingsboard_gateway.connectors.mqtt.mqtt_uplink_converter import MqttUplinkConverter, log

class CustomMqttUplinkConverter(MqttUplinkConverter):
    def __init__(self, config):
        self.__config = config.get('converter')
        self.dict_result = {}

    def convert(self, topic, body):
        try:
            log.debug("New data received: %s: %s" % (topic,body))
            # if topic = '/devices/buzzer/controls/volume' device name will be 'buzzer'.
            self.dict_result["deviceName"] = topic.split("/")[2]
            # just hardcode this
            self.dict_result["deviceType"] = "buzzer"
            self.dict_result["telemetry"] = {"data": body}
            log.debug("Result: %s" % (self.dict_result))
            return self.dict_result

        except ...

当我启动网关时,我在他的日志中看到他成功连接并读取了值:

INFO ... MQTT Broker Connector connected to 10.1.1.2:1883 - successfully.'
DEBUG ... Client <paho.mqtt.client.Client object at 0x7fb42d19dd68>, userdata None, flags {'session present': 0}, extra_params ()'

DEBUG ... <module 'CustomMqttUplinkConverter' from '/var/lib/thingsboard_gateway/extensions/mqtt/custom_mqtt_uplink_converter.py'>'
DEBUG ... Import CustomMqttUplinkConverter from /var/lib/thingsboard_gateway/extensions/mqtt.'
DEBUG ... Converter CustomMqttUplinkConverter for topic /devices/buzzer/controls/volume - found!'
INFO ... Connector "MQTT Broker Connector" subscribe to /devices/buzzer/controls/volume'
DEBUG ... Received data: {}'
DEBUG ... (None,)'
INFO ... "MQTT Broker Connector" subscription success to topic /devices/buzzer/controls/volume, subscription message id = 1'
DEBUG ... New data received: /devices/buzzer/controls/volume: 66'
DEBUG ... Result: {'deviceName': 'buzzer', 'deviceType': 'buzzer', 'telemetry': {'data': 66}}'

但这个值是他能读取的最后一个值。如果我更改volume一个代理,新值既不会出现在日志中,也不会出现在 TB UI 中。(我用 控制更新mosquitto_sub。)

似乎在网关重新启动之前,此转换器将永远不会再次调用。它是正确的行为吗?

如果我没有看到结果,如何确保我的代码是正确的?

4

1 回答 1

2

嗨,我已经尝试过您的自定义转换器版本,它不起作用,但是当我更改
self.dict_result["telemetry"] = {"data": body}

self.dict_result["telemetry"] = [{"data": body}]
它正确发送数据时。

网关需要来自转换器的一系列属性遥测。

于 2020-05-06T14:26:43.020 回答