Ziggy
Ziggy

Reputation: 541

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:

  "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. Any idea on what should be the query?

enter image description here

Upvotes: 0

Views: 241

Answers (1)

LeelaRajesh_Sayana
LeelaRajesh_Sayana

Reputation: 650

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.

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. Please refer the below images for details.

enter image description here

enter image description here

enter image description here

I have used the routing query $body.temperature >= 50 to route the messages to an end point. 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

enter image description here

Upvotes: 1

Related Questions