使用 Azure 门户自动管理 IoT 设备和模块Automatic IoT device and module management using the Azure portal

Azure IoT 中心的自动设备管理功能可自动完成许多复杂且重复性的大型设备阵列管理任务。Automatic device management in Azure IoT Hub automates many of the repetitive and complex tasks of managing large device fleets. 使用自动设备管理,可以根据设备的属性将一组设备指定为目标、定义所需的配置,然后在设备进入管理范畴时让 IoT 中心更新这些设备。With automatic device management, you can target a set of devices based on their properties, define a desired configuration, and then let IoT Hub update the devices when they come into scope. 此更新是使用自动设备配置或自动模块配置执行的。使用此项功能还能汇总完整度与合规性、处理合并与冲突,以及分阶段推出配置 。This update is done using an automatic device configuration or automatic module configuration, which lets you summarize completion and compliance, handle merging and conflicts, and roll out configurations in a phased approach.

备注

本文介绍的功能仅在 IoT 中心的标准层中可用。The features described in this article are available only in the standard tier of IoT Hub. 有关基本和标准/免费 IoT 中心层的详细信息,请参阅选择合适的 IoT 中心层For more information about the basic and standard/free IoT Hub tiers, see Choose the right IoT Hub tier.

自动设备管理的工作原理是使用所需属性更新一组设备孪生或模块孪生,并报告基于孪生报告属性的摘要。Automatic device management works by updating a set of device twins or module twins with desired properties and reporting a summary that's based on twin reported properties. 其中引入了一个新类以及名为 Configuration 的 JSON 文档,其中包含三个组成部分:It introduces a new class and JSON document called a Configuration that has three parts:

  • 目标条件定义要更新的设备孪生或模块孪生的范围。The target condition defines the scope of device twins or module twins to be updated. 目标条件在孪生标记和/或报告属性中指定为查询。The target condition is specified as a query on twin tags and/or reported properties.

  • 目标内容定义要在目标设备孪生或模块孪生中添加或更新的所需属性。The target content defines the desired properties to be added or updated in the targeted device twins or module twins. 内容包括要更改的所需属性节的路径。The content includes a path to the section of desired properties to be changed.

  • 指标 定义各种配置状态(例如“成功”、“正在进行中”和“错误”)的摘要计数。 The metrics define the summary counts of various configuration states such as Success, In Progress, and Error. 自定义指标指定为孪生报告属性中的查询。Custom metrics are specified as queries on twin reported properties. 系统指标是度量孪生更新状态的默认指标,例如,针对的孪生数,以及已成功更新的孪生数。System metrics are the default metrics that measure twin update status, such as the number of twins that are targeted and the number of twins that have been successfully updated.

自动配置在配置创建不久后就首次运行,然后按五分钟间隔运行。Automatic configurations run for the first time shortly after the configuration is created and then at five minute intervals. 每次自动配置运行时,都会运行指标查询。Metrics queries run each time the automatic configuration runs.

实现孪生Implement twins

自动设备配置需要使用设备孪生来同步云与设备之间的状态。Automatic device configurations require the use of device twins to synchronize state between the cloud and devices. 有关详细信息,请参阅了解并在 IoT 中心内使用设备孪生For more information, see Understand and use device twins in IoT Hub.

自动模块配置需要使用模块孪生来同步云与模块之间的状态。Automatic module configurations require the use of module twins to synchronize state between the cloud and modules. 有关详细信息,请参阅了解并在 IoT 中心内使用模块孪生For more information, see Understand and use module twins in IoT Hub.

使用标记设置孪生目标Use tags to target twins

创建配置之前,必须指定想要影响的设备或模块。Before you create a configuration, you must specify which devices or modules you want to affect. Azure IoT 中心使用设备孪生中的标记来标识设备,使用模块孪生中的标记来标识模块。Azure IoT Hub identifies devices and using tags in the device twin, and identifies modules using tags in the module twin. 每个设备或模块均可以拥有多个标记,你可以用适合解决方案的任何方式定义它们。Each device or modules can have multiple tags, and you can define them any way that makes sense for your solution. 例如,在管理不同位置中的设备时,请将以下标记添加到设备孪生:For example, if you manage devices in different locations, add the following tags to a device twin:

"tags": {
    "location": {
        "state": "Washington",
        "city": "Tacoma"
    }
},

创建配置Create a configuration

  1. Azure 门户中,转到 IoT 中心。In the Azure portal, go to your IoT hub.

  2. 选择“IoT 设备配置” 。Select IoT device configuration.

  3. 选择“添加设备配置” 或“添加模块配置” 。Select Add device configuration or Add module configuration.

    添加设备配置或模块配置

创建配置需要执行五个步骤。There are five steps to create a configuration. 下列各节将引导完成每个步骤。The following sections walk through each one.

名称和标签Name and Label

  1. 为配置指定不超过 128 个小写字母的唯一名称。Give your configuration a unique name that is up to 128 lowercase letters. 避免空格和以下无效字符:& ^ [ ] { } \ | " < > /Avoid spaces and the following invalid characters: & ^ [ ] { } \ | " < > /.

  2. 添加标签以帮助跟踪配置。Add labels to help track your configurations. 标签是描述配置的 <名称, 值> 对。 Labels are Name, Value pairs that describe your configuration. 例如,HostPlatform, LinuxVersion, 3.0.1For example, HostPlatform, Linux or Version, 3.0.1.

  3. 选择“下一步”转到下一步。Select Next to move to the next step.

指定设置Specify Settings

本部分定义要在目标设备或模块孪生中设置的内容。This section defines the content to be set in targeted device or module twins. 需要为每组设置提供两项输入。There are two inputs for each set of settings. 第一项输入是孪生路径,即,要设置的孪生所需属性中 JSON 节的路径。The first is the twin path, which is the path to the JSON section within the twin desired properties that will be set. 第二项输入是要在该节中插入的 JSON 内容。The second is the JSON content to be inserted in that section.

例如,可以将孪生路径设置为 properties.desired.chiller-water,然后提供以下 JSON 内容:For example, you could set the twin path to properties.desired.chiller-water and then provide the following JSON content:

{
  "temperature": 66,
  "pressure": 28
}

设置孪生路径和内容

此外,可以通过指定完整孪生路径并提供值(不带括号),来配置各项设置。You can also set individual settings by specifying the entire twin path and providing the value with no brackets. 例如,对于孪生路径 properties.desired.chiller-water.temperature,请将内容设置为 66For example, with the twin path properties.desired.chiller-water.temperature, set the content to 66. 然后,为压力属性创建一个新的孪生设置。Then create a new twin setting for the pressure property.

如果两个或更多个配置以同一孪生路径为目标,将会应用最高优先级配置中的内容(优先级在步骤 4 中定义)。If two or more configurations target the same twin path, the content from the highest priority configuration will apply (priority is defined in step 4).

如果想要删除某个现有属性,请将属性值指定为 nullIf you wish to remove an existing property, specify the property value to null.

可以通过选择“添加设备孪生设置”或“添加模块孪生设置”来添加其他设置 。You can add additional settings by selecting Add Device Twin Setting or Add Module Twin Setting.

指定指标(可选)Specify Metrics (optional)

指标提供应用配置内容后设备或模块可能报告回来的各种状态的摘要计数。Metrics provide summary counts of the various states that a device or module may report back after applying configuration content. 例如,可以针对挂起的设置更改、错误和成功的设置更改各创建一个指标。For example, you may create a metric for pending settings changes, a metric for errors, and a metric for successful settings changes.

每个配置最多可以有五个自定义指标。Each configuration can have up to five custom metrics.

  1. 在“指标名称”中输入名称。 Enter a name for Metric Name.

  2. 在“指标条件”中输入查询。 Enter a query for Metric Criteria. 该查询基于设备孪生报告的属性。The query is based on device twin reported properties. 指标表示查询返回的行数。The metric represents the number of rows returned by the query.

例如:For example:

SELECT deviceId FROM devices 
  WHERE properties.reported.chillerWaterSettings.status='pending'

可以包含一个表示应用了配置的子句,例如:You can include a clause that the configuration was applied, for example:

/* Include the double brackets. */
SELECT deviceId FROM devices 
  WHERE configurations.[[yourconfigname]].status='Applied'

如果要生成用于报告已配置模块的指标,请从 devices.modules 中选择 moduleIdIf you're building a metric to report on configured modules, select moduleId from devices.modules. 例如:For example:

SELECT deviceId, moduleId FROM devices.modules
  WHERE properties.reported.lastDesiredStatus.code = 200

目标设备Target Devices

使用孪生中的标记属性将应接收此配置的特定设备或模块指定为目标。Use the tags property from your twins to target the specific devices or modules that should receive this configuration. 还可以将孪生报告的属性指定为目标。You can also target twin reported properties.

自动设备配置只能以设备孪生标记为目标,自动模块配置只能以模块孪生标记为目标。Automatic device configurations can only target device twin tags, and automatic module configurations can only target module twin tags.

由于多个配置可能以同一设备或模块为目标,因此,每个配置都需要一个优先级编号。Since multiple configurations may target the same device or module, each configuration needs a priority number. 如果存在冲突,则优先级最高的配置将会胜出。If there's ever a conflict, the configuration with the highest priority wins.

  1. 为配置的“优先级”输入一个正整数。Enter a positive integer for the configuration Priority. 最大数值被视为最高优先级。The highest numerical value is considered the highest priority. 如果两个配置的优先级编号相同,则最近创建的配置胜出。If two configurations have the same priority number, the one that was created most recently wins.

  2. 输入“目标条件”,确定此配置的目标设备或模块。Enter a Target condition to determine which devices or modules will be targeted with this configuration. 该条件基于孪生标记或孪生报告的属性,应与表达式格式相匹配。The condition is based on twin tags or twin reported properties and should match the expression format.

    对于自动设备配置,可以仅指定要用作目标的标记或报告的属性。For automatic device configuration, you can specify just the tag or reported property to target. 例如,tags.environment='test'properties.reported.chillerProperties.model='4000x'For example, tags.environment='test' or properties.reported.chillerProperties.model='4000x'. 可以指定 * 来定目标到所有设备。You can specify * to target all devices.

    对于自动模块配置,请使用查询来指定注册到 IoT 中心的模块中的标记或报告的属性。For automatic module configuration, use a query to specify tags or reported properties from the modules registered to the IoT hub. 例如,from devices.modules where tags.environment='test'from devices.modules where properties.reported.chillerProperties.model='4000x'For example, from devices.modules where tags.environment='test' or from devices.modules where properties.reported.chillerProperties.model='4000x'. 不能使用通配符来指定以所有模块为目标。The wildcard cannot be used to target all modules.

  3. 选择“下一步” ,进入到最后一步。Select Next to move on to the final step.

查看配置Review Configuration

查看配置信息,然后选择“提交” 。Review your configuration information, then select Submit.

监视配置Monitor a configuration

若要查看配置的详细信息并监视运行它的设备,请执行以下步骤:To view the details of a configuration and monitor the devices running it, use the following steps:

  1. Azure 门户中,转到 IoT 中心。In the Azure portal, go to your IoT hub.

  2. 选择“IoT 设备配置” 。Select IoT device configuration.

  3. 检查配置列表。Inspect the configuration list. 对于每个配置,可查看以下详细信息:For each configuration, you can view the following details:

    • ID - 配置的名称。ID - the name of the configuration.

    • 目标条件 - 用于定义目标设备或模块的查询。Target condition - the query used to define targeted devices or modules.

    • 优先级 - 分配给配置的优先级编号。Priority - the priority number assigned to the configuration.

    • 创建时间 - 开始创建配置的时间戳。Creation time - the timestamp from when the configuration was created. 当两个配置具有相同的优先级时,此时间戳用于消除它们的关系。This timestamp is used to break ties when two configurations have the same priority.

    • 系统指标 - 指标由 IoT 中心计算,开发人员无法对其进行自定义。System metrics - metrics that are calculated by IoT Hub and cannot be customized by developers. “目标数”指定与目标条件匹配的设备孪生数。Targeted specifies the number of device twins that match the target condition. “应用”指定已被配置修改的设备孪生数,这可能包括当某个不同的更高优先级配置同时做出更改时,所完成的部分修改。Applies specified the number of device twins that have been modified by the configuration, which can include partial modifications in the event that a separate, higher priority configuration also made changes.

    • 自定义指标 - 开发人员针对孪生报告属性指定为查询的指标。Custom metrics - metrics that have been specified by the developer as queries against twin reported properties. 对于每个配置,最多可以定义五个自定义指标。Up to five custom metrics can be defined per configuration.

  4. 选择要监视的配置。Select the configuration that you want to monitor.

  5. 检查配置详细信息。Inspect the configuration details. 可以使用选项卡查看有关收到配置的设备的详细信息。You can use tabs to view specific details about the devices that received the configuration.

    • 目标条件 - 与目标条件匹配的设备或模块。Target Condition - the devices or modules that match the target condition.

    • 指标 - 系统指标和自定义指标的列表。Metrics - a list of system metrics and custom metrics. 在下拉列表中选择指标,然后选择“查看设备”或“查看模块”,即可查看计入每个指标的设备或模块列表。 You can view a list of devices or modules that are counted for each metric by selecting the metric in the drop-down and then selecting View Devices or View Modules.

    • 设备孪生设置或模块孪生设置 - 由配置设置的孪生设置。Device Twin Settings or Module Twin Settings - the twin settings that are set by the configuration.

    • 配置标签 - 用于描述配置的键值对。Configuration Labels - key-value pairs used to describe a configuration. 标签不对功能造成影响。Labels have no impact on functionality.

修改配置Modify a configuration

修改配置时,更改会立即复制到所有目标设备或模块。When you modify a configuration, the changes immediately replicate to all targeted devices or modules.

如果更新目标条件,将发生以下更新:If you update the target condition, the following updates occur:

  • 如果孪生不满足旧目标条件,但满足新目标条件,并且此配置是该孪生的最高优先级,则会应用此配置。If a twin didn't meet the old target condition, but meets the new target condition and this configuration is the highest priority for that twin, then this configuration is applied.

  • 如果当前运行此配置的孪生不再满足目标条件,则会删除配置中的设置,并且孪生将被下一个最高优先级配置修改。If a twin currently running this configuration no longer meets the target condition, the settings from the configuration will be removed and the twin will be modified by the next highest priority configuration.

  • 如果当前正在运行此配置的孪生不再满足目标条件,且不满足其他任何配置的目标条件,则会删除配置中的设置,并且不会在孪生中进行其他任何更改。If a twin currently running this configuration no longer meets the target condition and doesn't meet the target condition of any other configurations, then the settings from the configuration will be removed and no other changes will be made on the twin.

若要修改配置,请执行以下步骤:To modify a configuration, use the following steps:

  1. Azure 门户中,转到 IoT 中心。In the Azure portal, go to your IoT hub.

  2. 选择“IoT 设备配置” 。Select IoT device configuration.

  3. 选择要修改的配置。Select the configuration that you want to modify.

  4. 更新以下字段:Make updates to the following fields:

    • 目标条件Target condition
    • 标签Labels
    • 优先度Priority
    • 指标Metrics
  5. 选择“保存”。Select Save.

  6. 遵循 监视配置中的步骤来观察更改的实施。Follow the steps in Monitor a configuration to watch the changes roll out.

删除配置Delete a configuration

删除某个配置时,所有设备孪生将会采用下一个最高优先级的配置。When you delete a configuration, any device twins take on their next highest priority configuration. 如果设备孪生不满足其他任何配置的目标条件,则不会应用其他任何设置。If device twins don't meet the target condition of any other configuration, then no other settings are applied.

  1. Azure 门户中,转到 IoT 中心。In the Azure portal, go to your IoT hub.

  2. 选择“IoT 设备配置”。Select IoT device configuration.

  3. 使用复选框选择想要删除的配置。Use the checkbox to select the configuration that you want to delete.

  4. 选择“删除”。Select Delete.

  5. 系统会提示确认。A prompt will ask you to confirm.

后续步骤Next steps

本文已介绍如何大规模配置和监视 IoT 设备。In this article, you learned how to configure and monitor IoT devices at scale. 若要了解有关如何管理 Azure IoT 中心的详细信息,请参阅以下链接:Follow these links to learn more about managing Azure IoT Hub:

若要进一步探索 IoT 中心的功能,请参阅:To further explore the capabilities of IoT Hub, see:

若要了解如何使用 IoT 中心设备预配服务启用零接触实时预配,请参阅:To explore using the IoT Hub Device Provisioning Service to enable zero-touch, just-in-time provisioning, see: