使用内置直接方法来与 edgeAgent 通信Communicate with edgeAgent using built-in direct methods

使用 IoT Edge 代理模块中包含的直接方法监视和管理 IoT Edge 部署。Monitor and manage IoT Edge deployments by using the direct methods included in the IoT Edge agent module. 直接方法在设备上实现,然后可从云中调用。Direct methods are implemented on the device, and then can be invoked from the cloud. IoT Edge 代理包含一些可以帮助你远程监视和管理 IoT Edge 设备的直接方法。The IoT Edge agent includes direct methods that help you monitor and manage your IoT Edge devices remotely.

有关直接方法及其用法,以及如何在自己的模块中实现这些方法的详细信息,请参阅了解直接方法以及从 IoT 中心调用直接方法For more information about direct methods, how to use them, and how to implement them in your own modules, see Understand and invoke direct methods from IoT Hub.

处理这些直接方法时,其名称不区分大小写。The names of these direct methods are handled case-insensitive.

PingPing

ping 方法可用于检查 IoT Edge 是否在设备上运行,或者设备是否与 IoT 中心建立了连接。The ping method is useful for checking whether IoT Edge is running on a device, or whether the device has an open connection to IoT Hub. 使用此直接方法可以 ping IoT Edge 代理并获取其状态。Use this direct method to ping the IoT Edge agent and get its status. 成功的 ping 将返回空有效负载和“状态”: 200。A successful ping returns an empty payload and "status": 200.

例如:For example:

az iot hub invoke-module-method --method-name 'ping' -n <hub name> -d <device name> -m '$edgeAgent'

在 Azure 门户中,调用名为 ping、包含空 JSON 有效负载 {} 的方法。In the Azure portal, invoke the method with the method name ping and an empty JSON payload {}.

在 Azure 门户中调用直接方法“ping”

重启模块Restart module

使用 RestartModule 方法可以远程管理 IoT Edge 设备上运行的模块。 The RestartModule method allows for remote management of modules running on an IoT Edge device. 如果某个模块报告故障状态或其他不正常行为,你可以触发 IoT Edge 代理来重启该模块。If a module is reporting a failed state or other unhealthy behavior, you can trigger the IoT Edge agent to restart it. 成功的 restart 命令将返回空有效负载和“状态”: 200。A successful restart command returns an empty payload and "status": 200.

RestartModule 方法可在 IoT Edge 1.0.9 和更高版本中使用。The RestartModule method is available in IoT Edge version 1.0.9 and later.

可对 IoT Edge 设备上运行的任何模块(包括 edgeAgent 模块本身)使用 RestartModule 直接方法。You can use the RestartModule direct method on any module running on an IoT Edge device, including the edgeAgent module itself. 但是,如果使用此直接方法来关闭 edgeAgent,将不会收到成功结果,因为在模块重启时会中断连接。However, if you use this direct method to shut down the edgeAgent, you won't receive a success result since the connection is disrupted while the module restarts.

例如:For example:

az iot hub invoke-module-method --method-name 'RestartModule' -n <hub name> -d <device name> -m '$edgeAgent' --method-payload \
'
    {
        "schemaVersion": "1.0",
        "id": "<module name>"
    }
'

在 Azure 门户中,调用名为 RestartModule、包含以下 JSON 有效负载的方法:In the Azure portal, invoke the method with the method name RestartModule and the following JSON payload:

{
    "schemaVersion": "1.0",
    "id": "<module name>"
}

在 Azure 门户中调用直接方法“RestartModule”

诊断直接方法Diagnostic direct methods

  • GetModuleLogs:检索直接方法的响应中内联的模块日志。GetModuleLogs: Retrieve module logs inline in the response of the direct method.
  • UploadModuleLogs:检索模块日志,并将其上传到 Azure Blob 存储。UploadModuleLogs: Retrieve module logs and upload them to Azure Blob Storage.
  • UploadSupportBundle:使用支持捆绑检索模块日志,并将 zip 文件上传到 Azure Blob 存储。UploadSupportBundle: Retrieve module logs using a support bundle and upload a zip file to Azure Blob Storage.
  • GetTaskStatus:检查上传日志请求或支持捆绑请求的状态。GetTaskStatus: Check on the status of an upload logs or support bundle request.

从 1.0.10 版本起提供这些诊断直接方法。These diagnostic direct methods are available as of the 1.0.10 release.

后续步骤Next steps

IoT Edge 代理和 IoT Edge 中心模块孪生的属性Properties of the IoT Edge agent and IoT Edge hub module twins