我正在尝试设置一个小的IoT Hub应用程序来测试平台是否能够满足我的要求。我需要将数据从设备上传到云,并将命令从云下载到设备。我正在关注这个简单的例子:
Creating Azure IoT Edge Custom Modules in C# – send and receive data from IoT Hub
我从GitHub (linked in the previous tutorial)下载了源代码并修改了.Net solution。我创建了一个免费的(F1)Iot Hub并添加了一个设备。这些是我对源代码所做的更改:
以下是完整的源代码:
DotNetSensorModule.cs
public class DotNetSensorModule : IGatewayModule, IGatewayModuleStart
{
private Broker broker;
private string configuration;
public void Create(Broker broker, byte[] configuration)
{
this.broker = broker;
this.configuration = Encoding.UTF8.GetString(configuration);
}
public void Start()
{
Thread oThread = new Thread(new ThreadStart(this.threadBody));
// Start the thread
oThread.Start();
}
public void Destroy() { }
public void Receive(Message received_message)
{
string content = Encoding.UTF8.GetString(received_message.Content, 0, received_message.Content.Length);
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Somewhere to Sensor message. Content: {content}");
if (received_message.Properties.Count > 0)
{
Console.WriteLine(" Properties:");
foreach (KeyValuePair<string, string> property in received_message.Properties)
Console.WriteLine($" {property.Key}: {property.Value}");
}
}
public void threadBody()
{
Random r = new Random();
int n = r.Next();
while (true)
{
Dictionary<string, string> thisIsMyProperty = new Dictionary<string, string>();
thisIsMyProperty.Add("source", "mapping");
thisIsMyProperty.Add("deviceName", "my-device-name");
thisIsMyProperty.Add("deviceKey", "my-device-key");
Message messageToPublish = new Message("SensorData: " + n, thisIsMyProperty);
this.broker.Publish(messageToPublish);
//Publish a message every 5 minutes.
Thread.Sleep(300000);
n = r.Next();
}
}
}
DotNetPrinterModule.cs
public class DotNetPrinterModule : IGatewayModule
{
private string configuration;
public void Create(Broker broker, byte[] configuration)
{
this.configuration = Encoding.UTF8.GetString(configuration);
}
public void Destroy() { }
public void Receive(Message received_message)
{
string content = Encoding.UTF8.GetString(received_message.Content, 0, received_message.Content.Length);
if (received_message.Properties["source"] == "mapping")
{
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Sensor to Printer message. Content: {content}");
}
else
{
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Somewhere to Printer message. Content: {content}");
}
if (received_message.Properties.Count > 0)
{
Console.WriteLine(" Properties:");
foreach (KeyValuePair<string, string> property in received_message.Properties)
Console.WriteLine($" {property.Key}: {property.Value}");
}
}
}
module_dev_sample.json
{
"modules": [
{
"name": "IotHub",
"loader": {
"name": "native",
"entrypoint": {
"module.path": "iothub.dll"
}
},
"args": {
"IoTHubName": "my-free-iot-hub",
"IoTHubSuffix": "azure-devices.net",
"Transport": "HTTP"
}
},
{
"name": "dotnet_sensor_module",
"loader": {
"name": "dotnet",
"entrypoint": {
"assembly.name": "DotNetModuleSample",
"entry.type": "SensorModule.DotNetSensorModule"
}
},
"args": "module configuration"
},
{
"name": "dotnet_printer_module",
"loader": {
"name": "dotnet",
"entrypoint": {
"assembly.name": "DotNetModuleSample",
"entry.type": "PrinterModule.DotNetPrinterModule"
}
},
"args": "module configuration"
}
],
"links": [
{
"source": "dotnet_sensor_module",
"sink": "dotnet_printer_module"
},
{
"source": "dotnet_sensor_module",
"sink": "IotHub"
},
{
"source": "IotHub",
"sink": "dotnet_sensor_module"
}
]
}
当我运行应用程序时,我可以读取发送的消息。此外,我可以在Azure门户上看到IoT Hub的消息计数器是如何递增的。但是当我使用设备资源管理器发送消息时,我没有看到消息已收到。 有时会在10分钟,25分钟或50分钟后收到消息,有时会丢失消息。如果我在邮件中添加任何属性,则可以确定它不会被收到。你知道为什么会这样吗?
谢谢。
修改
我注意到如果我从Device Explorer连续发送10条消息,每次运行应用程序时,都会立即收到其中一条消息。我不知道为什么。