简体   繁体   English

路由查询在 Azure IoT 中心事件网格中不起作用

[英]Routing Query Not Working in Azure IoT Hub Event Grid

I created a device simulator with the following code:我使用以下代码创建了一个设备模拟器:

private static async void SendDeviceToCloudMessagesAsync()
        {
            while (true)
            {

                var tdsLevel = Rand.Next(10, 1000);
                var filterStatus = tdsLevel % 2 == 0 ? "Good" : "Bad";
                var waterUsage = Rand.Next(0, 500);
                var currentTemperature = Rand.Next(-30, 100);
                var motorStatus = currentTemperature >= 50 ? "Good" : "Bad";
                var telemetryDataPoint = new
                {
                    deviceId = DeviceId,
                    temperature = currentTemperature,
                    filter = filterStatus,
                    motor = motorStatus,
                    usage = waterUsage,
                    tds = tdsLevel
                };
                
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message = new Message(Encoding.UTF8.GetBytes(messageString));
                message.ContentType= "application/json";
                message.Properties.Add("Topic", "WaterUsage");

                await _deviceClient.SendEventAsync(message);
                Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                await Task.Delay(5000);
            }
        }

The output in Azure IoT Explorer is the following: Azure IoT Explorer 中的 output 如下:

  "body": {
    "deviceId": "MyFirstDevice",
    "temperature": 60,
    "filter": "Bad",
    "motor": "Good",
    "usage": 302,
    "tds": 457
  },
  "enqueuedTime": "Sun Jan 29 2023 13:55:51 GMT+0800 (Philippine Standard Time)",
  "properties": {
    "Topic": "WaterUsage"
  }
}

I know what to filter in the Azure IoT Hub Message Routing to only filter out temperatures >= 50. The routing query: $body.body.temperature >= 50 does not work as shown below.我知道在 Azure IoT 中心消息路由中要过滤什么以仅过滤掉温度 >= 50。路由查询:$body.body.temperature >= 50 不起作用,如下所示。 Any idea on what should be the query?关于查询应该是什么的任何想法?

在此处输入图像描述

I have used the following code which worked for me.我使用了以下对我有用的代码。 Instead of using Encoding.UTF8.GetBytes, I have used Encoding.ASCII.GetBytes and explicitly set the ContentEncoding to UTF8 using the below code.我没有使用 Encoding.UTF8.GetBytes,而是使用了 Encoding.ASCII.GetBytes 并使用以下代码将 ContentEncoding 显式设置为 UTF8。

var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
message.ContentEncoding = "utf-8"; 
message.ContentType = "application/json";
message.Properties.Add("Topic", "WaterUsage");

Even though the messages you notice in the Azure IoT explorer has the properties information, using Visual Studio Code's Start Monitoring Built-in end point option, you will notice the messages routed to the built in end point have a different format.即使您在 Azure IoT 浏览器中注意到的消息具有属性信息,但使用 Visual Studio Code 的“开始监视内置端点”选项,您会注意到路由到内置端点的消息具有不同的格式。 Please refer the below images for details.详情请参考以下图片。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I have used the routing query $body.temperature >= 50 to route the messages to an end point.我使用路由查询$body.temperature >= 50将消息路由到终点。 I could validate from the blob storage container end point that the messages received have the temperature greater than or equal to 50. Please find the below image of routed messages for reference我可以从 blob 存储容器端点验证收到的消息的温度是否大于或等于 50。请找到下面的路由消息图像以供参考

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM