Azure Device Twin所需和报告的属性同步 [英] Azure Device Twin desired and reported properties synchronization

查看:236
本文介绍了Azure Device Twin所需和报告的属性同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


同步所需和报告属性的最合适方法是什么。

What is the most appropriate way to synchronize desired and reported properties.


目前我认为它应该如何be:

Currently how I think it should be:



  1. 在Azure门户设置路由中,"所需属性更新"事件到IotHub。

  2. 创建实现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;
 }
}

还有什么好主意吗?有什么用Microsoft.Azure.Devices.JobClient左右?

Any better idea? Anything with Microsoft.Azure.Devices.JobClient or so?

推荐答案

你好Ruslan,

Hello Ruslan,

您是否看到此文档?:  https:// docs .microsoft.com / zh-CN / azure / iot-hub / iot-hub-devguide-device-twins

Did you see this doc?: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins

具体请参见"接收双胞胎通知"

"此操作允许在修改双胞胎时通知解决方案后端。为此,您的物联网解决方案需要创建
a路线,并将数据源设置为 
twinChangeEvents
默认情况下,没有此类路由预先存在,因此不会发送双重通知。
如果更改率太高,或者出于其他原因(如内部故障),IoT Hub可能只发送一个包含所有更改的通知。
因此,如果您的应用程序需要可靠的审计和所有中间状态的记录,那么您应该使用设备到云的消息。双重通知消息包含属性和正文"

"This operation allows the solution back end to be notified when the twin is modified. To do so, your IoT solution needs to create a route and to set the Data Source equal to twinChangeEvents. By default, no such routes pre-exist, so no twin notifications are sent. If the rate of change is too high, or for other reasons such as internal failures, the IoT Hub might send only one notification that contains all changes. Therefore, if your application needs reliable auditing and logging of all intermediate states, you should use device-to-cloud messages. The twin notification message includes properties and body"

您在上面共享的代码中使用了D2C,这是因为您需要可靠的审计?在您的代码中,您似乎正在从服务端更新报告的属性,这是不可能的...报告的属性只能由设备更新。

You are using D2C in the code you shared above, that's because you need reliable auditing? In your code looks like you are updating a reported property from the Service side, that's not possible... Reported properties can be updated only by the device.




阅读更多

here


  • 所需属性
    与报告的属性一起使用以同步设备配置或条件。
    解决方案后端可以设置所需的属性
    ,设备应用程序可以读取它们。设备应用还可以接收所需属性更改的通知。
  • 报告的属性。与所需属性一起使用以同步设备配置或条件。
    设备应用可以设置报告的属性,解决方案后端可以读取和查询它们。
  • Desired properties. Used along with reported properties to synchronize device configuration or conditions. The solution back end can set desired properties, and the device app can read them. The device app can also receive notifications of changes in the desired properties.
  • Reported properties. Used along with desired properties to synchronize device configuration or conditions. The device app can set reported properties, and the solution back end can read and query them.

谢谢!


这篇关于Azure Device Twin所需和报告的属性同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆