ThingsBoard 物联网网关不更新 MQTT 值
ThingsBoard IoT Gateway doesn't update MQTT values
我尝试使用 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
控制更新。)
似乎在网关重新启动之前,此转换器将永远不会再次调用。这是正确的行为吗?
如果我看不到结果,如何确保我的代码是正确的?
您好,我已经尝试过您的自定义转换器版本,它没有用,但是当我更改后
self.dict_result["telemetry"] = {"data": body}
到
self.dict_result["telemetry"] = [{"data": body}]
它正确发送数据。
网关需要来自转换器的属性遥测数组。
我尝试使用 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
控制更新。)
似乎在网关重新启动之前,此转换器将永远不会再次调用。这是正确的行为吗?
如果我看不到结果,如何确保我的代码是正确的?
您好,我已经尝试过您的自定义转换器版本,它没有用,但是当我更改后
self.dict_result["telemetry"] = {"data": body}
到
self.dict_result["telemetry"] = [{"data": body}]
它正确发送数据。
网关需要来自转换器的属性遥测数组。