了解有关 IoT Edge 设备、模块和子设备的扩展脱机功能Understand extended offline capabilities for IoT Edge devices, modules, and child devices

Azure IoT Edge 支持 IoT Edge 设备上的扩展脱机操作,同时在非 IoT Edge 子设备上启用脱机操作。Azure IoT Edge supports extended offline operations on your IoT Edge devices, and enables offline operations on non-IoT Edge child devices too. 只要 IoT Edge 设备有机会连接到 IoT 中心,它和任何子设备就可以在间歇性或无 Internet 连接的情况下继续运作。As long as an IoT Edge device has had one opportunity to connect to IoT Hub, it and any child devices can continue to function with intermittent or no internet connection.

工作原理How it works

当 IoT Edge 设备进入脱机模式,IoT Edge 中心将扮演三个角色。When an IoT Edge device goes into offline mode, the IoT Edge hub takes on three roles. 首先,它将存储任何向上游发送的消息并保存它们,直到设备重新连接。First, it stores any messages that would go upstream and saves them until the device reconnects. 其次,它代表 IoT 中心对模块和子设备进行身份验证,以便它们可以继续运行。Second, it acts on behalf of IoT Hub to authenticate modules and child devices so that they can continue to operate. 第三,它会在子设备之间启用通常通过 IoT 中心的通信。Third, it enables communication between child devices that normally would go through IoT Hub.

下面的示例展示了 IoT Edge 方案如何在脱机模式下运行:The following example shows how an IoT Edge scenario operates in offline mode:

  1. 配置设备Configure devices

    IoT Edge 设备自动启用脱机功能。IoT Edge devices automatically have offline capabilities enabled. 若要将此功能扩展到其他 IoT 设备,需要在 IoT 中心声明设备之间的父子关系。To extend that capability to other IoT devices, you need to declare a parent-child relationship between the devices in IoT Hub. 然后,将子设备配置为信任分配给它们的父设备,并通过父设备(用作网关)路由设备到云的通信。Then, you configure the child devices to trust their assigned parent device and route the device-to-cloud communications through the parent as a gateway.

  2. 与 IoT 中心同步Sync with IoT Hub

    在安装 IoT Edge 运行时后,IoT Edge 设备至少要有一次处于联机状态,以便与 IoT 中心同步。At least once after installation of the IoT Edge runtime, the IoT Edge device needs to be online to sync with IoT Hub. 在此同步中,IoT Edge 设备将获取有关任何分配给它的子设备的详细信息。In this sync, the IoT Edge device gets details about any child devices assigned to it. IoT Edge 设备还可以安全更新本地缓存以启用脱机操作,并检索本地存储遥测消息的设置。The IoT Edge device also securely updates its local cache to enable offline operations and retrieves settings for local storage of telemetry messages.

  3. 脱机Go offline

    从 IoT 中心断开连接时,IoT Edge 设备及其部署模块和任何 IoT 子设备都可以无限期运行。While disconnected from IoT Hub, the IoT Edge device, its deployed modules, and any children IoT devices can operate indefinitely. 模块和子设备可以在脱机状态下通过在 IoT Edge 中心进行身份验证来启动和重新启动。Modules and child devices can start and restart by authenticating with the IoT Edge hub while offline. 上游绑定到 IoT 中心的遥测存储在本地。Telemetry bound upstream to IoT Hub is stored locally. 模块之间或 loT 子设备之间的通信通过直接方法或消息来维护。Communication between modules or between child IoT devices is maintained through direct methods or messages.

  4. 与 IoT 中心重新连接和重新同步Reconnect and resync with IoT Hub

    一旦还原与 IoT 中心的连接,IoT Edge 设备会再次同步。Once the connection with IoT Hub is restored, the IoT Edge device syncs again. 本地存储的消息按照它们存储的相同顺序传递。Locally stored messages are delivered in the same order in which they were stored. 模块和设备的所需属性和报告属性之间的差异已得到协调。Any differences between the desired and reported properties of the modules and devices are reconciled. IoT Edge 设备更新对其分配的 IoT 子设备集所做的任何更改。The IoT Edge device updates any changes to its set of assigned child IoT devices.

约束和限制Restrictions and limits

本文所述的扩展脱机功能可在 IoT Edge 1.0.7 版或更高版本中获得。The extended offline capabilities described in this article are available in IoT Edge version 1.0.7 or higher. 早期版本有一个脱机功能子集。Earlier versions have a subset of offline features. 不具备扩展脱机功能的现有 IoT Edge 设备不能通过更改运行时版本进行升级,但是必须用新的 IoT Edge 设备标识重新配置才能获得这些功能。Existing IoT Edge devices that don't have extended offline capabilities can't be upgraded by changing the runtime version, but must be reconfigured with a new IoT Edge device identity to gain these features.

只能添加非 IoT Edge 设备作为子设备。Only non-IoT Edge devices can be added as child devices.

IoT Edge 设备及其分配的子设备可以在初始一次性同步之后无限期脱机运行。但是,消息存储取决于生存时间 (TTL) 设置和存储消息的可用磁盘空间。IoT Edge devices and their assigned child devices can function indefinitely offline after the initial, one-time sync. However, storage of messages depends on the time to live (TTL) setting and the available disk space for storing the messages.

设置父设备和子设备Set up parent and child devices

对于将其扩展脱机功能扩展到 IoT 子设备的 IoT Edge 设备,需要完成两个步骤。For an IoT Edge device to extend its extended offline capabilities to child IoT devices, you need to complete two steps. 首先,在 Azure 门户中声明父子关系。First, declare the parent-child relationships in the Azure portal. 其次,在父设备与任何子设备之间建立信任关系,然后将设备到云的通信配置为通过父设备(用作网关)路由。Second, create a trust relationship between the parent device and any child devices, then configure device-to-cloud communications to go through the parent as a gateway.

分配子设备Assign child devices

子设备可以是注册到同一个 IoT 中心的任何非 IoT Edge 设备。Child devices can be any non-IoT Edge device registered to the same IoT Hub. 父设备可以有多个子设备,但一个子设备只能有一个父设备。Parent devices can have multiple child devices, but a child device only has one parent. 可以使用三个选项在 Edge 设备中设置子设备:使用 Azure 门户、Azure CLI 或 IoT 中心服务 SDK。There are three options to set child devices to an edge device: through the Azure portal, using the Azure CLI, or using the IoT Hub service SDK.

以下部分举例说明如何在 IoT 中心为现有的 IoT 设备声明父/子关系。The following sections provide examples of how you can declare the parent/child relationship in IoT Hub for existing IoT devices. 若要为子设备创建新的设备标识,请参阅在 Azure IoT 中心对下游设备进行身份验证了解详细信息。If you're creating new device identities for your child devices, see Authenticate a downstream device to Azure IoT Hub for more information.

选项 1:IoT 中心门户Option 1: IoT Hub Portal

可以在创建新设备时声明父子关系。You can declare the parent-child relationship when creating a new device. 或者,对于现有的设备,可以从 IoT Edge 父设备或 IoT 子设备的设备详细信息页声明关系。Or for existing devices, you can declare the relationship from the device details page of either the parent IoT Edge device or the child IoT device.

从 IoT Edge 设备的详细信息页管理子设备

选项 2:使用 az 命令行工具Option 2: Use the az command-line tool

Azure 命令行接口IoT 扩展(v0.7.0 或更高版本)配合使用时,可以通过 device-identity 子命令管理父子关系。Using the Azure command-line interface with IoT extension (v0.7.0 or newer), you can manage parent child relationships with the device-identity subcommands. 以下示例使用一个查询将中心内的所有非 IoT Edge 设备分配为 IoT Edge 设备的子设备。The example below uses a query to assign all non-IoT Edge devices in the hub to be child devices of an IoT Edge device.

# Set IoT Edge parent device
egde_device="edge-device1"

# Get All IoT Devices
device_list=$(az iot hub query \
        --hub-name replace-with-hub-name \
        --subscription replace-with-sub-name \
        --resource-group replace-with-rg-name \
        -q "SELECT * FROM devices WHERE capabilities.iotEdge = false" \
        --query 'join(`, `, [].deviceId)' -o tsv)

# Add all IoT devices to IoT Edge (as child)
az iot hub device-identity add-children \
  --device-id $egde_device \
  --child-list $device_list \
  --hub-name replace-with-hub-name \
  --resource-group replace-with-rg-name \
  --subscription replace-with-sub-name 

可以修改查询,选择另一部分设备。You can modify the query to select a different subset of devices. 如果指定大的设备集,此命令可能需要数秒钟才能完成。The command may take several seconds if you specify a large set of devices.

选项 3:使用 IoT 中心服务 SDKOption 3: Use IoT Hub Service SDK

最后,可以使用 C#、Java 或 Node.js IoT 中心服务 SDK 以编程方式管理父子关系。Finally, you can manage parent child relationships programmatically using either C#, Java or Node.js IoT Hub Service SDK. 这是使用 C# SDK 分配子设备的示例Here is an example of assigning a child device using the C# SDK.

将父设备设为网关Set up the parent device as a gateway

可将父/子关系视为一种透明网关,其中的子设备在 IoT 中心具有自身的标识,但通过其父设备在云中通信。You can think of a parent/child relationship as a transparent gateway, where the child device has its own identity in IoT Hub but communicates through the cloud via its parent. 若要安全通信,子设备需能够验证父设备来自受信任的源。For secure communication, the child device needs to be able to verify that the parent device comes from a trusted source. 否则,第三方可能会设置恶意设备来模拟父设备并截获通信。Otherwise, third-parties could set up malicious devices to impersonate parents and intercept communications.

以下文章详细介绍了建立这种信任关系的一种方法:One way to create this trust relationship is described in detail in the following articles:

指定 DNS 服务器Specify DNS servers

为了提高可靠性,强烈建议指定在环境中使用的 DNS 服务器地址。To improve robustness, it is highly recommended you specify the DNS server addresses used in your environment. 若要为 IoT Edge 设置 DNS服务器,请参阅故障排除文章中 Edge 代理模块不断报告“空配置文件”且设备上没有模块启动的解决方案。To set your DNS server for IoT Edge, see the resolution for Edge Agent module continually reports 'empty config file' and no modules start on device in the troubleshooting article.

可选脱机设置Optional offline settings

如果设备已脱机,IoT Edge 父设备将一直存储所有设备到云的消息,直到重新建立连接为止。If your devices go offline, the IoT Edge parent device stores all device-to-cloud messages until the connection is reestablished. IoT Edge 中心模块将管理脱机消息的存储和转发。The IoT Edge hub module manages the storage and forwarding of offline messages. 对于长时间脱机的设备,可以通过配置两项 IoT Edge 中心设置来优化性能。For devices that may go offline for extended periods of time, optimize performance by configuring two IoT Edge hub settings.

首先,增大活动设置的时间,以便在设备重新建立连接之前,IoT Edge 中心将消息保留足够长的时间。First, increase the time to live setting so that the IoT Edge hub will keep messages long enough for your device to reconnect. 然后,为消息存储添加更多磁盘空间。Then, add additional disk space for message storage.

生存时间Time to live

生存时间设置是指在过期之前消息可以等待传递的时间量(以秒为单位)。The time to live setting is the amount of time (in seconds) that a message can wait to be delivered before it expires. 默认为 7200 秒(两个小时)。The default is 7200 seconds (two hours). 此最大值仅受整数变量的最大值(约为 20 亿)限制。The maximum value is only limited by the maximum value of an integer variable, which is around 2 billion.

此设置是 IoT Edge 中心的所需属性,它存储在模块孪生中。This setting is a desired property of the IoT Edge hub, which is stored in the module twin. 可以在 Azure 门户中或者直接在部署清单中配置此项设置。You can configure it in the Azure portal or directly in the deployment manifest.

"$edgeHub": {
    "properties.desired": {
        "schemaVersion": "1.0",
        "routes": {},
        "storeAndForwardConfiguration": {
            "timeToLiveSecs": 7200
        }
    }
}

系统模块的主机存储Host storage for system modules

默认情况下,消息和模块状态信息存储在 IoT Edge 中心的本地容器文件系统中。Messages and module state information are stored in the IoT Edge hub's local container filesystem by default. 若要改进可靠性,尤其是在脱机操作时改进可靠性,也可在主机 IoT Edge 设备上设置专用存储。For improved reliability, especially when operating offline, you can also dedicate storage on the host IoT Edge device.

后续步骤Next steps

详细了解如何为父/子设备连接设置透明网关:Learn more about how to set up a transparent gateway for your parent/child device connections: