我开始使用Azure IoT中心,我配置了我的端点和servicehub,以便通过路由学习它。 在此路由中,我指定当消息显示level = critical时,它会将消息转发到我的端点,如以下链接中所述:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-process-d2c
不同之处在于我使用自己的代码为我的温度传感器编程了DTO,并将其作为一个大的json消息发送(参见代码)
DTO课程:
[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&# 34; pressureSensorValue":98792.11," altitudeSensorValue":213.046539," measurementTime":" 2017-02-23T05:34:00.4544388-08:00&#34 ;, " measurename":" LightSensor""电平":"临界"}
消息在IOT集线器中接收,但不会转发到端点。 我究竟做错了什么?你无法查询json对象消息吗?如果是这样的话你怎么做呢?我不想发送不必要的消息。
答案 0 :(得分:0)
发送邮件时,这似乎是编码问题。
我在UTF8中对消息进行编码,而需要在ASCII值中完成。 我还可以添加消息属性以使其更加明显。
如下:
AdditionalInfo><Code>GLW51</Code><Lang>GLW51</LangCode><TzCode>GLW51</TzCode></AdditionalInfo>