C# How to update desired twin property of an Azure IoT Hub device

前端 未结 3 729
孤独总比滥情好
孤独总比滥情好 2021-01-23 17:48

I have registered the devices in IoT and the client application (device) can update reported twin properties. Now, I have to update desired twin properties from back end applica

相关标签:
3条回答
  • 2021-01-23 18:18

    Here's a sample on GitHub. And here's a tutorial.

    Here is the relevant piece of code:

    public async Task UpdateDesiredProperties(string deviceId)
    {
        var twin = await _registryManager.GetTwinAsync(deviceId);
    
        var patch =
            @"{
            properties: {
                desired: {
                  customKey: 'customValue'
                }
            }
        }";
    
        await _registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);
    }
    
    0 讨论(0)
  • 2021-01-23 18:34

    Just found the way to update desired tags.

    RegistryManager registryManager = RegistryManager.CreateFromConnectionString(connectionString);
    var twin = await registryManager.GetTwinAsync(device.Id);
    var patch = "{ \"properties\": { \"desired\": { \"configVersion\" : 3.1 } } }"; //json string
    await registryManager.UpdateTwinAsync(device.Id, tags, twin.ETag);
    
    0 讨论(0)
  • 2021-01-23 18:39

    Another way of doing this is by updating the desired TwinCollection directly.

    using (var manager = RegistryManager.CreateFromConnectionString("Your IoT Hub ConnectionString"))
    {
       var twin = await manager.GetTwinAsync("your device id");
       twin.Properties.Desired["YourProperty"] = "some value";
       await manager.UpdateTwinAsync(twin.DeviceId, twin, twin.ETag);
    }
    
    0 讨论(0)
提交回复
热议问题