我使用python和paho.mqtt将消息发送到云 我设置了端点和路线。当我将查询字符串设置为 true 时,一切正常
messageDict = {}
systemPropertiesDict = {"contentType": "application/json", "contentEncoding": "utf-8", "iothub-message-source": "deviceMessages", "iothub-enqueuedtime": "2017-05-08T18:55:31.8514657Z"}
messageDict = {"systemProperties": systemPropertiesDict}
messageDict["appProperties"] = {}
body = '{id:1}'
messageDict["body"] = body
root = {"message":messageDict}
msg = json.dumps(root, indent=2).encode('utf-8')
print("Message to send", msg)
self.client.publish(topicName, msg)
但是如果我将查询字符串设置为 $body.id = 1,那么我不会收到任何消息。
大家有什么想法吗?
最佳答案
由于未设置内容编码类型,路由不起作用。代码中的所有“systemProperties”实际上都是消息正文而不是系统属性。该方法设置的内容编码类型不生效。
将“$.ct=application%2Fjson&$.ce=utf-8”添加到主题。然后它会看起来像这样:
devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8
但是要使路由查询适用于您的消息,您需要使用以下查询字符串:$body.message.body.id = 1
要进行两项编辑:
首先,将 body = '{id:1}'
更改为 body = {"id":1}
,使 id 成为字符串。
其次,将 topicName
值更改为此:
devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8
如果可能的话,建议使用Azure IoT SDK for Python与 Azure IoT 中心通信。
关于python - Azure IoT 中心设备消息和路由筛选器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160000/