Search code examples
jsonazureendpointscustom-routes

Azure route not forwarded to endpoint after filtering query


I am starting to use Azure IoT hub and I configured my endpoints and servicehub to learn from it with a route. In this route I specify that when a message says level = critical that it forwards the message to my endpoint like explained in the following link: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-process-d2c

The difference is that I use my own code for my temperature sensor with a programmed DTO and that I send it as one big json message (see code)

DTO class:

[DataContract]
class Bmp280DTO
{
    [DataMember]
    public Guid guid { get; set; }
    [DataMember]
    public string deviceName { get; set; }
    [DataMember]
    public float tempSensorValue { get; set; }
    [DataMember]
    public float pressureSensorValue { get; set; }
    [DataMember]
    public float altitudeSensorValue { get; set; }
    [DataMember]
    public DateTime measurementTime { get; set; }
    [DataMember]
    public string measurename { get; set; }
    [DataMember]
    public string level { get; set; }

    public Bmp280DTO() { }

    public Bmp280DTO(Guid id, String Device, float TmpSensorValue, float PrSensorValue, float AlSensorValue)
    {
        this.guid = id;
        this.deviceName = Device;
        this.tempSensorValue = TmpSensorValue;
        this.pressureSensorValue = PrSensorValue;
        this.altitudeSensorValue = AlSensorValue;

        this.measurementTime = DateTime.Now;
        this.measurename = "LightSensor";
        this.level = DetermineMessageLevel(TmpSensorValue);
    }

    public string ToJson()
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Bmp280DTO));
        MemoryStream ms = new MemoryStream();
        ser.WriteObject(ms, this);
        string json = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);

        return json;

    }

    public string DetermineMessageLevel(float temperaturesensorvalue)
    {
        if(temperaturesensorvalue > 22)
        {
            return "critical";
        }
        return "normal";
    }

}

Message send:

    private void SendBmp280Data(Bmp280DTO AzureBmp280Data)
    {
        System.Text.StringBuilder Bmp280JsonMessage = new System.Text.StringBuilder();
        Bmp280JsonMessage.Append(AzureBmp280Data.ToJson());

        MessageCommands.SendMessage(Bmp280JsonMessage.ToString());

    }

Examples of message: {"guid":"xxxx","deviceName":"Bmp280Sensor","tempSensorValue":23.59763,"pressureSensorValue":98792.11,"altitudeSensorValue":213.046539,"measurementTime":"2017-02-23T05:34:00.4544388-08:00","measurename":"LightSensor","level":"critical"}

The message is received in the IOT hub but not forwarded to the endpoint. What am I doing wrong? Are you not able to query json object messages? And if that's the case how do you do it then? I don't want to send unnecessary messages.


Solution

  • It seems that this was a problem with the encoding while sending the message.

    I encoded the message in UTF8 while it needed to be done in ASCII value's. I also could add message-properties to make it more obvious.

    So as following:

            var messageString = JsonConvert.SerializeObject(AzureBmp280Data);
            Debug.WriteLine("Message Sent: {0}", messageString, null);
            var message = new Message(Encoding.ASCII.GetBytes(messageString));
            message.Properties.Add("level", "critical");