2

I have a problem updating a property attribute with DeviceTwin in c #.

The code I execute is the following:

            var registryManager = RegistryManager.CreateFromConnectionString(AppSettings.KeyIoT);
            var twin = await registryManager.GetTwinAsync(dto.DeviceIdorId);

            TwinCollection s = twin.Properties.Reported["Power"];

            Power toRet = JsonConvert.DeserializeObject<Power>(s.ToJson());
            toRet.MaximumAvailable = 50;

           var patch =
            @"{
            properties: {
                reported:{
                  Power:" + JsonConvert.SerializeObject(toRet) +
                @"}
                }
            }";

            await registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);

I tried also with the following code :

            var registryManager = RegistryManager.CreateFromConnectionString(AppSettings.KeyIoT);
            var twin = await registryManager.GetTwinAsync(dto.DeviceIdorId);

            TwinCollection s = twin.Properties.Reported["Power"];

            Power toRet = JsonConvert.DeserializeObject<Power>(s.ToJson());
            toRet.MaximumAvailable = 50;

            twin.Properties.Reported["Power"] = JsonConvert.SerializeObject(toRet);

            Twin updatedTwin = await registryManager.UpdateTwinAsync(twin.DeviceId, twin, twin.ETag);

The problem is that after the update, if I do a reread, the property MaximumAvailable is not updated. What am I doing wrong? Thanks.

Simone Spagna
  • 626
  • 7
  • 27

1 Answers1

1

The device twin reported properties are readonly on the service-facing side. See more details here

Roman Kiss
  • 7,925
  • 1
  • 8
  • 21
  • If I change reported with desidered I get the following error : **"Additional text found in JSON string after finishing deserializing object. Path 'properties.desidered', line 4, position 112."}"** What am I doing wrong? Thanks. – Simone Spagna Nov 27 '19 at 10:57
  • 1
    your code with a patch implementation (the first one) is working well. Did you change a *reported* to the *desired* in the patch json formatted text, too? – Roman Kiss Nov 27 '19 at 12:02
  • Yes, I changed a reported to the desidered in the patch Json formatted text and I got the error. – Simone Spagna Nov 27 '19 at 12:54