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

◇◆丶佛笑我妖孽 提交于 2020-01-15 09:32:09

问题


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 application (in C#). Need help.


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/54186589/c-sharp-how-to-update-desired-twin-property-of-an-azure-iot-hub-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!