IoT 解决方案中设备配置的最佳做法Best practices for device configuration within an IoT solution

Azure IoT 中心内的自动设备管理功能可将许多复杂且重复性的任务自动化,包括在大型设备阵列的整个生命周期内对其进行管理。Automatic device management in Azure IoT Hub automates many repetitive and complex tasks of managing large device fleets over the entirety of their lifecycles. 本文定义在开发和操作 IoT 解决方案时涉及到的各种角色的多种最佳做法。This article defines many of the best practices for the various roles involved in developing and operating an IoT solution.

  • IoT 硬件制造商/集成商: IoT 硬件的制造商、组装来自各个制造商的硬件的集成商,或针对由其他供应商制造或集成的 IoT 部署提供硬件的供应商。IoT hardware manufacturer/integrator: Manufacturers of IoT hardware, integrators assembling hardware from various manufacturers, or suppliers providing hardware for an IoT deployment manufactured or integrated by other suppliers. 参与固件、嵌入式操作系统和嵌入式软件的开发与集成。Involved in development and integration of firmware, embedded operating systems, and embedded software.
  • IoT 解决方案开发人员: IoT 解决方案的开发通常由解决方案开发人员完成。IoT solution developer: The development of an IoT solution is typically done by a solution developer. 此开发人员可能是内部团队成员或专门从事此活动的系统集成商。This developer may be part of an in-house team or a system integrator specializing in this activity. IoT 解决方案开发人员可从头开始开发 IoT 解决方案的各个组件、集成各种标准组件或开源组件。The IoT solution developer can develop various components of the IoT solution from scratch, integrate various standard or open-source components.
  • IoT 解决方案操作员: 在部署后,IoT 解决方案需要长期的操作、监视、升级和维护。IoT solution operator: After the IoT solution is deployed, it requires long-term operations, monitoring, upgrades, and maintenance. 这些任务可由内部团队来完成,该团队由信息技术专业人员、硬件操作和维护团队,以及负责监督整体 IoT 基础结构行为是否正常的领域专业人员组成。These tasks can be done by an in-house team that consists of information technology specialists, hardware operations and maintenance teams, and domain specialists who monitor the correct behavior of the overall IoT infrastructure.

了解大规模配置 IoT 设备时的自动设备管理Understand automatic device management for configuring IoT devices at scale

自动设备管理包括设备孪生模块孪生提供的、在云与设备之间同步所需状态和报告状态的诸多好处。Automatic device management includes the many benefits of device twins and module twins to synchronize desired and reported states between the cloud and devices. [自动设备配置][lnk-auto-device-config]会自动更新大量的孪生,并汇总进度与合规性。[Automatic device configurations][lnk-auto-device-config] automatically update large sets of twins and summerize progress and compliance. 以下概要步骤描述如何开发和使用自动设备管理:The following high-level steps describe how automatic device management is developed and used:

  • IoT 硬件制造商/系统集成商使用设备孪生在嵌入式应用程序中实施设备管理功能。The IoT hardware manufacturer/integrator implements device management features within an embedded application using device twins. 这些功能可能包括固件更新、软件安装和更新,以及设置管理。These features could include firmware updates, software installation and update, and settings management.

  • IoT 解决方案开发人员使用设备孪生自动设备配置实施设备管理操作的管理层。The IoT solution developer implements the management layer of device management operations using device twins and automatic device configurations. 该解决方案应该定义操作员界面来执行设备管理任务。The solution should include defining an operator interface to perform device management tasks.

  • IoT 解决方案操作员使用 IoT 解决方案执行设备管理任务,具体而言,包括将设备分组在一起、启动配置更改(例如固件更新)、监视进度,以及解决出现的问题。The IoT solution operator uses the IoT solution to perform device management tasks, particularly to group devices together, initiate configuration changes like firmware updates, monitor progress, and troubleshoot issues that arise.

IoT 硬件制造商/集成商IoT hardware manufacturer/integrator

下面是处理嵌入式软件开发的硬件制造商和系统集成商的最佳做法:The following are best practices for hardware manufacturers and integrators dealing with embedded software development:

  • 实施设备孪生 使用设备孪生可以从云同步所需配置,以及报告当前配置和设备属性。Implement device twins: Device twins enable synchronizing desired configuration from the cloud and for reporting current configuration and device properties. 最好是通过 Azure IoT SDK 在嵌入式应用程序内部实施设备孪生。The best way to implement device twins within embedded applications is through the Azure IoT SDKs. 设备孪生非常适合用于配置,因为它们:Device twins are best suited for configuration because they:

    • 支持双向通信。Support bi-directional communication.
    • 允许联网和离线设备状态。Allow for both connected and disconnected device states.
    • 遵循最终一致性的原则。Follow the principle of eventual consistency.
    • 在云中完全可查询。Are fully queriable in the cloud.
  • 为设备管理构建设备孪生: 构建设备孪生的方式应该是以逻辑方式将设备管理属性一起分组到节中。Structure the device twin for device management: The device twin should be structured such that device management properties are logically grouped together into sections. 这可以实现配置更改的隔离,且不影响孪生中的其他节。Doing so will enable configuration changes to be isolated without impacting other sections of the twin. 例如,在所需属性中为固件创建一个节,为软件创建另一个节,并为网络设置创建第三个节。For example, create a section within desired properties for firmware, another section for software, and a third section for network settings.

  • 报告对设备管理非常有用的设备特性: 物理设备制造商和型号、固件、操作系统、序列号和其他标识符等特性对报告非常有用,并用作将配置更改作为目标的参数。Report device attributes that are useful for device management: Attributes like physical device make and model, firmware, operating system, serial number, and other identifiers are useful for reporting and as parameters for targeting configuration changes.

  • 定义用于报告状态和进度的主要状态: 应枚举顶级状态,以便可以将其报告给操作员。Define the main states for reporting status and progress: Top-level states should be enumerated so that they can be reported to the operator. 例如,固件更新会将状态报告为“当前”、“正在下载”、“正在应用”、“正在进行”和“错误”。For example, a firmware update would report status as Current, Downloading, Applying, In Progress, and Error. 定义其他字段,以获取有关每种状态的详细信息。Define additional fields for more information on each state.

IoT 解决方案开发人员IoT solution developer

下面是构建基于 Azure 的系统的 IoT 解决方案开发人员的最佳做法:The following are best practices for IoT solution developers who are building systems based in Azure:

  • 实施设备孪生 使用设备孪生可以从云同步所需配置,以及报告当前配置和设备属性。Implement device twins: Device twins enable synchronizing desired configuration from the cloud and for reporting current configuration and device properties. 最好是通过 Azure IoT SDK 在云解决方案应用程序内部实施设备孪生。设备孪生最适用于该配置,因为它们:The best way to implement device twins within cloud solutions applications is through the Azure IoT SDKs Device twins are best suited for configuration because they:

    • 支持双向通信。Support bi-directional communication.
    • 允许联网和离线设备状态。Allow for both connected and disconnected device states.
    • 遵循最终一致性的原则。Follow the principle of eventual consistency.
    • 在云中完全可查询。Are fully queriable in the cloud.
  • 使用设备孪生标记组织设备: 解决方案应允许操作员根据不同的部署策略(例如 canary)定义质量环或其他设备集。Organize devices using device twin tags: The solution should allow the operator to define quality rings or other sets of devices based on various deployment strategies such as canary. 可以使用设备孪生标记和查询在解决方案内部实施设备组织。Device organization can be implemented within your solution using device twin tags and queries. 需要通过设备组织以安全准确的方式实现配置实施。Device organization is necessary to allow for configuration roll outs safely and accurately.

  • 实施自动设备配置 自动设备配置通过设备孪生对大型 IoT 设备集部署和监视配置更改。Implement automatic device configurations: Automatic device configurations deploy and monitor configuration changes to large sets of IoT devices via device twins.

    自动设备配置通过目标条件(针对设备孪生标记或报告属性执行的查询)将设备孪生集指定为目标。Automatic device configurations target sets of device twins via the target condition, which is a query on device twin tags or reported properties. 目标内容是要在目标设备孪生内部设置的所需属性集。The target content is the set of desired properties that will be set within the targeted device twins. 目标内容应与 IoT 硬件制造商/系统集成商定义的设备孪生结构相符。The target content should align with the device twin structure defined by the IoT hardware manufacturer/integrator. 指标是针对设备孪生报告的属性执行的查询,也应该与 IoT 硬件制造商/集成商定义的设备孪生结构相符。The metrics are queries on device twin reported properties and should also align with the device twin structure defined by the IoT hardware manufacturer/integrator.

    自动设备配置首次在配置创建后不久运行,然后每隔五分钟运行一次。Automatic device configurations run for the first time shortly after the configuration is created and then at five minute intervals. 这些配置还受益于 IoT 中心。IoT 中心以绝不会超过设备孪生读取和更新限制的速率执行设备孪生操作。They also benefit from the IoT Hub performing device twin operations at a rate that will never exceed the throttling limits for device twin reads and updates.

  • 使用设备预配服务 解决方案开发人员应使用设备预配服务将设备孪生标记分配到新设备,以便通过自动设备配置(目标为具有该标记的孪生)自动配置这些设备。Use the Device Provisioning Service: Solution developers should use the Device Provisioning Service to assign device twin tags to new devices, such that they will be automatically configured by automatic device configurations that are targeted at twins with that tag.

IoT 解决方案操作员IoT solution operator

下面是使用基于 Azure 的 IoT 解决方案的 IoT 解决方案操作员的最佳做法:The following are best practices for IoT solution operators who using an IoT solution built on Azure:

  • 组织设备以进行管理: IoT 解决方案应该定义或者允许根据不同的部署策略(例如 canary)创建质量环或其他设备集。Organize devices for management: The IoT solution should define or allow for the creation of quality rings or other sets of devices based on various deployment strategies such as canary. 设备集将用于实施配置更改,以及执行其他大规模的设备管理操作。The sets of devices will be used to roll out configuration changes and to perform other at-scale device management operations.

  • 使用分阶段推出执行配置更改: 分阶段推出是指操作员将更改部署到不断扩大的 IoT 设备集所遵循的整个过程。Perform configuration changes using a phased roll out: A phased roll out is an overall process whereby an operator deploys changes to a broadening set of IoT devices. 这样做的目的是逐渐进行更改,降低进行大规模重大更改的风险。The goal is to make changes gradually to reduce the risk of making wide scale breaking changes.  操作员应使用解决方案的界面来创建自动设备配置,目标条件应该针对最初的设备集(例如 canary 组)。  The operator should use the solution's interface to create an automatic device configuration and the targeting condition should target an initial set of devices (such as a canary group). 然后,操作员应验证最初设备集中的配置更改。The operator should then validate the configuration change in the initial set of devices.

    完成验证后,操作员将更新自动设备配置,以包含更大的设备集。Once validation is complete, the operator will update the automatic device configuration to include a larger set of devices. 操作员还应将配置的优先级设置为高于当前针对这些设备的配置。The operator should also set the priority for the configuration to be higher than other configurations currently targeted to those devices. 可以使用自动设备配置报告的指标来监视实施。The roll out can be monitored using the metrics reported by the automatic device configuration. 

  • 出错或配置不当时执行回滚: 可以通过更改目标条件,使设备不再符合目标条件,以回滚导致出错或配置不当的自动设备配置。Perform rollbacks in case of errors or misconfigurations: An automatic device configuration that causes errors or misconfigurations can be rolled back by changing the targeting condition so that the devices no longer meet the targeting condition. 确保优先级更低的另一自动设备配置仍然针对这些设备。Ensure that another automatic device configuration of lower priority is still targeted for those devices. 通过查看指标,验证回滚是否成功:回滚后的配置应该不再显示未作为目标的设备的状态,第二个配置的指标现在应该包含仍然作为目标的设备的计数。Verify that the rollback succeeded by viewing the metrics: The rolled-back configuration should no longer show status for untargeted devices, and the second configuration's metrics should now include counts for the devices that are still targeted.

后续步骤Next steps