c# - 如何在 IoT Edge 模块中读取模块孪生?
问题描述
如何从设备孪生设置中获取设置到我的 c#/.net 核心 Azure IoT Edge 模块中?
解决方案
您可以在官方教程中找到一个很好的示例,说明如何对模块孪生的 Desired Property 更改做出反应:
await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null);
static Task OnDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext)
{
try
{
Console.WriteLine("Desired property change:");
Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
if (desiredProperties["TemperatureThreshold"]!=null)
temperatureThreshold = desiredProperties["TemperatureThreshold"];
}
catch (AggregateException ex)
{
foreach (Exception exception in ex.InnerExceptions)
{
Console.WriteLine();
Console.WriteLine("Error when receiving desired property: {0}", exception);
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error when receiving desired property: {0}", ex.Message);
}
return Task.CompletedTask;
}