过滤查询后 Azure 路由未转发到端点

Azure route not forwarded to endpoint after filtering query

我开始使用 Azure IoT 中心,我配置了我的端点和服务中心以通过路由从中学习。 在此路由中,我指定当一条消息显示 level = critical 时,它将消息转发到我的端点,如下面的解释 link: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-process-d2c

不同之处在于,我将自己的代码用于带有编程 DTO 的温度传感器,并将其作为一条大 json 消息发送(参见代码)

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";
    }

}

消息发送:

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

        MessageCommands.SendMessage(Bmp280JsonMessage.ToString());

    }

消息示例: {"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"}

消息在 IOT 集线器中接收但未转发到端点。 我究竟做错了什么?您无法查询 json 对象消息吗?如果是这样的话,你该怎么做呢?我不想发送不必要的消息。

看来这是发送消息时编码的问题。

我将消息编码为 UTF8,而它需要以 ASCII 值的形式完成。 我还可以添加消息属性以使其更加明显。

所以如下:

        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");