我正在尝试创建一个示例,在该示例中,客户端向某事物发出消息,该事物回复给客户端。这个东西是通过MQTT连接连接到Eclipse Ditto的,而客户端是通过curl命令模拟的。为了做到这一点,我从以下两个教程中摘录了一些内容:https://www.eclipse.org/ditto/2018-12-05-example-command-and-control.html和https://github.com/eclipse/ditto-examples/tree/master/mqtt-bidirectional。
除了Ditto没有以非常可靠的方式将消息从客户端路由到事物之外(我想说的是,三分之一的消息没有传递给客户端),即使超时值非常高,客户端也不能从事物接收响应消息。
这是我的Python代码,它的作用是:
import logging
import time
import random
import json
import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
response = json.dumps({
"topic": "org.eclipse.ditto/teapot/things/live/messages/brew",
"headers": {
"content-type": "application/json",
"correlation-id": "command-and-control"
},
"path": "/inbox/messages/brew",
"value": {
"eta": 58
},
"status": 200
})
client.publish(outTopic, response)
inTopic = "ditto-tutorial/org.eclipse.ditto:teapot/#";
outTopic = "ditto-tutorial/";
thingId = "teapot";
interval = 10
broker_address = "test.mosquitto.org"
client = mqtt.Client(thingId) #create new instance
client.on_message = on_message #attach function to callback
client.connect(broker_address) #connect to broker
client.loop_start() #start the loop
client.subscribe(inTopic)
while True:
time.sleep(interval)这是我模拟客户端的curl命令:
curl -i -X POST 'http://localhost:8080/api/2/things/org.eclipse.ditto:teapot/inbox/messages/brew?timeout=60' \
-u ditto:ditto \
-H 'x-correlation-id: command-and-control' \
-d '{"targetTemperature":85}'一开始,我认为我在Ditto协议上做错了什么,但我认为事实并非如此,因为请求和响应中的correlation-id都是相同的,而其他字段似乎都没问题。
发布于 2020-02-05 14:56:15
我认为问题可能是您对命令和响应使用了固定的关联id。尝试使用随机数,例如UUID。
https://stackoverflow.com/questions/60019706
复制相似问题