Azure IoT Edge入门(9)云到边缘设备控制-Azure iot edge module direct method
本文介绍:
1. Azure IoT edge 从云到边缘设备的控制方式之一 direct method。
2. Edge-agent中内置的 direct method
本文参考:https://docs.microsoft.com/zh-cn/azure/iot-edge/how-to-edgeagent-direct-method
视频:
https://www.51azure.cloud/post/2020/11/14/azure-iot-edge9azure-iot-edge-module-direct-method
图文:
我们之前介绍过Azure IoT Hub 的云到设备控制的方法之一是direct method,参见《云到设备的控制-direct method》。
与此类似,Azure IoT Edge Module中也支持direct method,可以实现对在线module的控制及数据传输(通过payload)。
同时,Azure IoT Edge的系统Module之一的Edge-agent中也内置了几个direct method。
direct metod 功能通过 Module client 提供,各个语言的Module client 语法, 请参阅首选 SDK 语言的 API 参考:C#、C、Python、Java 或 Node.js。
例如C#语言的 direct method 相关的方法:
ModuleClient.SetMessageHandlerAsync Method
Definition
Namespace:
Microsoft.Azure.Devices.Client
Assembly:
Microsoft.Azure.Devices.Client.dll
Package:
Microsoft.Azure.Devices.Client v1.33.1
Overloads
SetMessageHandlerAsync(MessageHandler, Object) | Sets a new default delegate which applies to all endpoints. If a delegate is already associated with the input, it will be called, else the default delegate will be called. If a default delegate was set previously, it will be overwritten. |
SetMessageHandlerAsync(MessageHandler, Object, CancellationToken) | Sets a new default delegate which applies to all endpoints. If a delegate is already associated with the input, it will be called, else the default delegate will be called. If a default delegate was set previously, it will be overwritten. |
示例代码请参照:
https://github.com/Azure/iotedge/blob/master/edge-modules/SimulatedTemperatureSensor/src/Program.cs
相关逻辑请参考:
云端发送了“Reset” direct method请求,edge 设备的 module 执行了响应:
我们可以自行根据Module client 中的 SetMethodHandlerAsync 语法自行实现自己的 direct method:例如控制module重启,控制module 调整发送频率等等。
除了自行写代码实现这些功能,IoT Edge 中的 系统module之一的Edge-agnet 也内置了一些direct method:
https://docs.microsoft.com/zh-cn/azure/iot-edge/how-to-edgeagent-direct-method
PING
在IoT edge 中找到执行 direct method的设备,然后进入 edgeAgent module:
RestartModule
重启指定module:
在第八讲基础上运行的module,SensorModule会在重启后发送一条特殊的消息:Sensor Module Start
如下图所示:
接下来我们通过EdgeAgent的 RestartModule重启SensorModule,观察云端收到的数据
云端收到Module重启后发送的指令:
诊断相关的直接方法:
- GetModuleLogs:在直接方法的响应中检索内联模块日志。
- UploadModuleLogs:检索模块日志,并将其上传到 Azure Blob 存储。
- UploadSupportBundle:使用支持捆绑检索模块日志,并将 zip 文件上传到 Azure Blob 存储。
- GetTaskStatus:检查上传日志或支持捆绑请求的状态。
注意,IoT Edge Runtime 1.0.10 版本提供这些诊断直接方法。
声明:
点击可查阅本站文章目录 《文章分类目录》
本站所有内容仅代表个人观点,如与官文档冲突,请以官方文档为准。
可在本页面下方留言或通过下方联系方式联系我:
微信:wxyusz;邮箱:[email protected]
欢迎关注公众号“云计算实战”,接收最新文章推送。
本作品由Sean Yu 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章链接:https://www.51azure.cloud,且不得用于商业目的。