我正在尝试创建一个示例,其中客户端向事物发出消息并且事物回复客户端。该事物通过 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-双向。
除了 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
request 和 responde 都是一样的,其他字段似乎没问题。