Я перешел по этой ссылке Чтобы написать собственные модули. В этом руководстве модуль с именем tempSensor отправляет данные другому модулю CSharpModule. Что касается учебника, я его успешно реализовал.
Я хочу: получать данные телеметрии от IoTDevice на IoTEdge Device.
Архитектура: Azure IoTHub подключен к устройствам Интернета вещей и устройству IoTEdge.
Что я пробовал: я попытался отправить данные телеметрии с моделируемого устройства, подключенного к ioTEdge с помощью connectionString.
Код для отправки данных здесь
//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 не поступает сообщение.