python - Azure IoT 中心设备消息和路由筛选器

标签 python azure mqtt azure-iot-hub

我使用pythonpaho.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/

相关文章:

python - WebJob Python 错误 ImportError : Missing required dependencies ['numpy' ]

ios - 我在 ios 应用程序中使用 MQTTClient,我想在订阅主题时保存用户的数据

python - 在 python 中通过 RS232 到 USB 电缆读取秤数据

python - 如何将带有不带引号的键的字符串转换为 Python 中的字典

azure - 如何使用 Azure CLI 命令获取虚拟机的公共(public) IP 地址

docker - docker 中的 mqtt 服务器 - 一种从 DockerCompose.yaml 运行 <mosquitto_passwd -U> 的方法

java - 如何知道mqtt中订阅的主题

python - 使用 Python lxml 时出现错误 'failed to load external entity'

python - 将数据导入Django的好方法

azure - 如何在设备配置服务注册列表中映射 IoTHub? - 注册状态 "unassigned"