我点击了此链接To write custom modules。在本教程中,名为 tempSensor 的模块将数据发送到另一个模块 CSharpModule 。就教程而言,我已经成功实现了。
我想做的是:从 IoTDevice 到 IoTEdge Device 接收遥测数据。
体系结构:与IoT设备和IoTEdge设备连接的Azure IoTHub
我曾尝试过的事情:我试图从通过connectionString连接到ioTEdge的模拟设备发送遥测数据。
发送数据的代码在这里
//DeviceClient connected to IoTEdge
s_deviceClient = DeviceClient.CreateFromConnectionString(s_connectionString);
private static async void SendDeviceToCloudMessagesAsync()
{
// Initial telemetry values
double minTemperature = 20;
double minHumidity = 60;
Random rand = new Random();
while (true)
{
double currentTemperature = minTemperature + rand.NextDouble() * 15;
double currentHumidity = minHumidity + rand.NextDouble() * 20;
// Create JSON message
var telemetryDataPoint = new
{
temperature = currentTemperature,
humidity = currentHumidity
};
var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
// Add a custom application property to the message.
message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");
// Send the tlemetry message to endpoint output1
await s_deviceClient.SendEventAsync("ouput1",message);
//await s_deviceClient.SendEventAsync(message);
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
await Task.Delay(s_telemetryInterval * 10000);
}
}
IoTEdge自定义模块代码的接收端在这里...
static async Task Init()
{
AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
ITransportSettings[] settings = { amqpSetting };
// Open a connection to the Edge runtime
ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
await ioTHubModuleClient.OpenAsync();
Console.WriteLine("IoT Hub module client initialized.");
// Register a callback for messages that are received by the module.
// await ioTHubModuleClient.SetImputMessageHandlerAsync("input1", PipeMessage, iotHubModuleClient);
// Read the TemperatureThreshold value from the module twin's desired properties
var moduleTwin = await ioTHubModuleClient.GetTwinAsync();
var moduleTwinCollection = moduleTwin.Properties.Desired;
try {
temperatureThreshold = moduleTwinCollection["iothub-connection-device-id"];
} catch(ArgumentOutOfRangeException e) {
Console.WriteLine($"Property TemperatureThreshold not exist: {e.Message}");
}
// Attach a callback for updates to the module twin's desired properties.
await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null);
// Register a callback for messages that are received by the module
await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
}
来自自定义模块的deployment.template.json文件的路由信息如下。
"routes": {
"aggregationModuleToIoTHub": "FROM /messages/modules/aggregationModule/outputs/* INTO $upstream"
}
但是问题是从未调用过回调 PipeMessage 。我的理解是IoTEdge端点 messages / input1 上没有消息。