Skip to content

Commit

Permalink
review twin init
Browse files Browse the repository at this point in the history
  • Loading branch information
ridomin committed Oct 1, 2022
1 parent 6501385 commit 58c9b0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 12 additions & 2 deletions samples/mqtt-device/Device.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using dtmi_com_example_devicetemplate;
using MQTTnet.Extensions.MultiCloud;
using MQTTnet.Extensions.MultiCloud.AzureIoTClient;
using MQTTnet.Extensions.MultiCloud.BrokerIoTClient;
using MQTTnet.Extensions.MultiCloud.Connections;

namespace mqtt_device;
Expand Down Expand Up @@ -33,8 +35,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

await client.Property_sdkInfo.SendMessageAsync(ClientFactory.NuGetPackageVersion);

//await client.Property_interval.InitPropertyAsync(client.InitialState, default_interval, stoppingToken);
//await client.Property_interval.ReportPropertyAsync(stoppingToken);
if (client is HubMqttClient)
{
HubMqttClient hubClient = (HubMqttClient)client;
client.InitialState = await hubClient.GetTwinAsync();
await TwinInitializer.InitPropertyAsync(client.Connection, client.InitialState, client.Property_interval, "interval", default_interval);
}
else
{
await PropertyInitializer.InitPropertyAsync(client.Property_interval, default_interval);
}

double lastTemp = 21;
while (!stoppingToken.IsCancellationRequested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TwinInitializer
public static async Task InitPropertyAsync<T>(IMqttClient client, string twin, IWritableProperty<T> prop, string propName, T defaultValue)
{
var ack = InitFromTwin(twin, propName, defaultValue);
prop.Version = ack.Version;
Ack<T> acceptedAck;
if (prop.OnMessage != null)
{
Expand All @@ -18,11 +19,9 @@ public static async Task InitPropertyAsync<T>(IMqttClient client, string twin, I
else
{
acceptedAck = ack;
acceptedAck.Description = "Init from default value";
acceptedAck.Status = 203;
}
acceptedAck.Status = 203;
acceptedAck.Value = ack.Value;
acceptedAck.Version = 0;
acceptedAck.Description = "Init from default value";
var roBinder = new ReadOnlyProperty<Ack<T>>(client, propName);
await roBinder.SendMessageAsync(acceptedAck);
}
Expand Down

0 comments on commit 58c9b0a

Please sign in to comment.