none
How to update desired device twin properities in C#? RRS feed

  • Question

  • In the Device Twins example it uses Node which I'm not familiar with for the device and .Net for the client.  I'm trying to implement the device in c#.

    When a device receives it's twin and it has some desired properities set, how do you clear them?  In the Node example, it has:

    var completeConfigChange =  function(twin) {
         var currentTelemetryConfig = twin.properties.reported.telemetryConfig;
         currentTelemetryConfig.configId = currentTelemetryConfig.pendingConfig.configId;
         currentTelemetryConfig.sendFrequency = currentTelemetryConfig.pendingConfig.sendFrequency;
         currentTelemetryConfig.status = "Success";
         delete currentTelemetryConfig.pendingConfig;
    
         var patch = {
             telemetryConfig: currentTelemetryConfig
         };
         patch.telemetryConfig.pendingConfig = null;
    
         twin.properties.reported.update(patch, function(err) {
             if (err) {
                 console.error('Error reporting properties: ' + err);
             } else {
                 console.log('Reported completed config change: ' + JSON.stringify(patch));
             }
         });
    

    How do I implement this in C#?

    In the C# example:

                TwinCollection reportedProperties = new TwinCollection();
                reportedProperties["configId"] = "something";
                reportedProperties["sendFrequency"] = "5m";

                Client.UpdateReportedPropertiesAsync(reportedProperties);

    This sets the reported property but how do I signal from the device to the service that the update has completed successfully?  Seems I need something like reportedProperties["status"] = "Success";  

    However adding this doesn't remove the desired tags from the twin.  How do I properly notify the service that the desired property has been updated using C#?

    Thursday, January 19, 2017 8:46 AM

All replies

  • Hi IOTEnthus,

    Based on the current release of the Device Twin, there is a built-in eventing only on the device side, where the Device Twin Reported properties have been changed. As you know, the Device Twin represents a state of the device (thing) in the IoT infrastructure.

    This device state is divided into two states such as device (Reported properties) and service (Desired properties) state. Additionally the device twin holding about both states internal data such as $metadata and $version numbering. 

    In other words, each side can change only own state, therefore to synchronize the state between the sides (device vs service) over the internet, we need to be performed some "state machine process".

    The following screen snippet shows an example of this state processing:

    as you can see, the state process have 6 steps:

    1. the device updated own state represented by Reported properties

    2. the device will send a non-telemetry event to the Custom Endpoint based on the Azure IoT Hub Routes rule

    3. the Azure Function is triggered by Service Bus Entity (Queue/Topic) message

    4. The Azure Function perform a business logic with Device Twin and synchronize states (updating own (service) state based on the device state)

    5. The device is notified about the step 4. and finalize the state processing as a last step (for instance, state=successful) in the own (Reported) properties.

    It will be nice, if Microsoft will incorporated the step 2. into the Azure IoT Hub, when the Device Twin Reported properties have been changed and generated an event to the Custom Endpoint.

    Thanks

    Roman



    • Edited by Roman Kiss Thursday, January 19, 2017 7:14 PM
    Thursday, January 19, 2017 4:58 PM
  • Once the device receives the desired property and changes the value in reported properties - what are steps that need to be executed on the C# side?  Is there a code example for

    "2. the device will send a non-telemetry event to the Custom Endpoint based on the Azure IoT Hub Routes rule"

    Thursday, January 19, 2017 9:12 PM
  • step 2. Send non-telemetry event to the Azure IoT Hub backend (Azure Function).

    the following code snippet is an example for event forwarded to the Custom Endpoint (Azure Function) via an Azure IoT Hub Routes rule:

    var non-telemetryMessage = new Message(Encoding.ASCII.GetBytes("this is a Json payload for Azure Function logic"));
    non-telemetryMessage.Properties["location"]="BA";  // this is an example!
    non-telemetryMessage.MessageId = msgId;
    await deviceClient.SendEventAsync(non-telemetryMessage);

    Note, that the Azure IoT Hub Routes must be configured for your custom endpoint, for example a Service Bus Queue/Topic and for a specific rule with an expression location = "BA" 

    The following screen snippet shows this part from the Azure portal:

    The message with this property and value will be forwarded to the custom endpoint instead of the Default Endpoint such as streaming endpoint (telemetry events). So, the payload can be completely different from the telemetry payload and in this case it should be payload related to changes in the device state.

    Thanks

    Roman 



    • Edited by Roman Kiss Thursday, January 19, 2017 10:23 PM
    Thursday, January 19, 2017 10:20 PM
  • Thanks for the response - I'm still confused here.  I need to configure a new endpoint just to acknowledge desired properties are taken care of?  This doesn't seem to be right... The device twin example doesn't seem to do this.

    What I'm asking is for a C# equivalent of this node.js example for the DEVICE:

    https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-node-twin-getstarted

    In this example there are 2 functions initConfigChange and completeConfigChange.  I am assuming after completeConfigChange that the desired property will be removed.  Is that not the case?

    Not sure why if reported=desired property that the necessary updates are not removed.  I think messages already have confirmation and return - twin device updates you would think would be the same.

    Thursday, January 19, 2017 11:11 PM
  • Q1: How to remove property in the device twin?

    Note, that each side such as device and service can remove only own state (properties).

    From the MSDN Document Device twins

    "Properties set to null are removed"

    Q2: How the cloud back-end service is notified by device twin reported state changes?

    The current version of the Device Twin allows only notification of the desired changes on the device side (device-facing endpoint).

    the example of the Create the simulated device app simulated this handshaking between the device and service by timer, see initConfigChange function:

    setTimeout(function() {completeConfigChange(twin);}, 60000);

    after 60 seconds, the simulated device will invoke a function completeConfigChange to finalize the device twin reported state. 

    In the real world (not Hello world), where the Device Twin is used actively with the cloud service back-end logic, the device needs a bidirectional communication with the Azure IoT Hub, in other words, the device need to fire a telemetry and non-telemetry event.

    So, that's was my succession how to create this non-telemetry channel in the cloud back-end used for any generic purposes such as device-to-device communications, device twin reported change notification, etc.

    Another way, less elegant is using a device twin query with a periodically polling all devices.

    I do recommend using a Device Explorer Twin tool with your device and simulate a cloud back-end process. It's very useful tinny tool.

    Thanks

    Roman

     




    • Edited by Roman Kiss Friday, January 20, 2017 10:08 AM
    Friday, January 20, 2017 9:55 AM