本文介绍了Azure IoTHub DeviceMessage和路由筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 python paho.mqtt 将消息发送到云我设置了端点和路由.当我将查询字符串设置为 true 时,一切正常

I use python and paho.mqtt for sending messages to cloudI set up endpoint and route. When I set query string to true, everything works fine

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 ,那么我不会收到任何消息.

But if I set the query string to $body.id = 1, then I don't receive any messages.

有什么主意吗?

推荐答案

该路由不起作用,因为未设置内容编码类型.您代码中的所有"systemProperties"实际上都是消息主体,而不是系统属性.用此方法设置的内容编码类型无效.

The route not working because the content encoding type is not set. All the "systemProperties" in your code actually as message body not system properties. Content encoding type set by this method doesn't take effect.

在主题中添加"$ .ct = application%2Fjson& $.ce = utf-8".然后将如下所示:

Add "$.ct=application%2Fjson&$.ce=utf-8" to the topic. Then it will look like this:

devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8

但是要使路由查询适用于您的消息,您需要使用以下查询字符串:$ body.message.body.id = 1

But to make the route query works on your message you need use this query string: $body.message.body.id = 1

要进行两次

首先,将body = '{id:1}'更改为body = {"id":1}以使id为字符串.

First, change body = '{id:1}' to body = {"id":1} to make the id as a string.

第二,将topicName值更改为此:

devices/{yourDeviceId}/messages/events/$.ct=application%2Fjson&$.ce=utf-8

如果可能的话,建议使用 Azure IoT SDK for Python 与Azure IoT中心进行通信.

If possible, it is suggesting to use Azure IoT SDK for Python to communicate with Azure IoT Hub.

这篇关于Azure IoTHub DeviceMessage和路由筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 13:00