Silvester Schn.
Silvester Schn.

Reputation: 164

How to read Module twin in IoT Edge module?

How can I get a setting from Device Twin settings into my c# /.net core Azure IoT Edge module ?

Upvotes: 0

Views: 761

Answers (1)

silent
silent

Reputation: 16148

You find a good example on how to react the Desired Property changes of your module twin in the official tutorial:

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;
}

Upvotes: 1

Related Questions