What is the most appropriate way to synchronize desired and reported properties.
Currently how I think it should be:
- On the Azure portal setup routing of "desired property update" event to IotHub.
- Create class implementing IEventProcessor:
internal class LoggingEventProcessor:IEventProcessor { public Task OpenAsync(PartitionContext context) { Console.WriteLine($"LoggingEventProcessor opening, partition: {context.PartitionId}"); return Task.CompletedTask; }
public async Task CloseAsync(PartitionContext context, CloseReason reason)
{
Console.WriteLine($"LoggingEventProcessor closing, partition: {context.PartitionId}, reason: {reason}");
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var msg in messages)
{
string messageSource = (string)msg.SystemProperties["iothub-message-source"];
var deviceId = msg.SystemProperties["iothub-connection-device-id"];
var payload = Encoding.ASCII.GetString(msg.Body.Array,
msg.Body.Offset,
msg.Body.Count);
switch (messageSource)
{
case "deviceLifecycleEvents":
Twin tw = JsonConvert.DeserializeObject<Twin>(payload);
Console.WriteLine($"Events received on partition: {context.PartitionId}, deviceId: {deviceId}, payload: {payload}");
break;
case "twinChangeEvents":
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(connectionStringBuilder.ToString(), Microsoft.Azure.Devices.Client.TransportType.Amqp);
var props = new TwinCollection();
props["temperature"] = payload;
return deviceClient.UpdateReportedPropertiesAsync(props);
break;
default:
Console.WriteLine($"Message source '{messageSource}' not supported");
break;
}
}
return context.CheckpointAsync();
}
public Task ProcessErrorAsync(PartitionContext context, Exception error)
{
Console.WriteLine($"LoggingEventProcessor closing, partition: {context.PartitionId}, reason: {error.Message}");
return Task.CompletedTask;
}
}
Any better idea? Anything with Microsoft.Azure.Devices.JobClient or so?