开发你自己的 IoT Edge 模块Develop your own IoT Edge modules

Azure IoT Edge 模块可以与其他 Azure 服务连接,帮助你实现更大的云数据管道。Azure IoT Edge modules can connect with other Azure services and contribute to your larger cloud data pipeline. 本文介绍如何开发模块以与 IoT Edge 运行时和 IoT 中心通信,并进而与 Azure 云的其他服务通信。This article describes how you can develop modules to communicate with the IoT Edge runtime and IoT Hub, and therefore the rest of the Azure cloud.

IoT Edge 运行时环境IoT Edge runtime environment

IoT Edge 运行时提供基础结构可集成多个 IoT Edge 模块的功能并将其部署到 IoT Edge 设备上。The IoT Edge runtime provides the infrastructure to integrate the functionality of multiple IoT Edge modules and to deploy them onto IoT Edge devices. 任意程序都可打包为 IoT Edge 模块。Any program can be packaged as an IoT Edge module. 若要充分利用 IoT Edge 通信和管理功能,在模块中运行的程序可以使用 Azure IoT 设备 SDK 连接到本地 IoT Edge 中心。To take full advantage of IoT Edge communication and management functionalities, a program running in a module can use the Azure IoT Device SDK to connect to the local IoT Edge hub.

使用 IoT Edge 中心Using the IoT Edge hub

IoT Edge 中心提供两种主要功能:连接到 IoT 中心的代理和本地通信。The IoT Edge hub provides two main functionalities: proxy to IoT Hub, and local communications.

IoT 中心基元IoT Hub primitives

IoT 中心在以下意义上将模块实例视为与设备类似:IoT Hub sees a module instance analogously to a device, in the sense that:

当前,模块不能接收云到设备消息,也不能使用文件上传功能。Currently, modules can't receive cloud-to-device messages or use the file upload feature.

编写模块时,可使用 Azure IoT 设备 SDK 连接到 IoT Edge 中心,使用上述将 IoT 中心用于设备应用程序时的功能。When writing a module, you can use the Azure IoT Device SDK to connect to the IoT Edge hub and use the above functionality as you would when using IoT Hub with a device application. IoT Edge 模块和 IoT 设备应用程序之间的唯一区别在于,你必须引用模块标识而非设备标识。The only difference between IoT Edge modules and IoT device applications is that you have to refer to the module identity instead of the device identity.

设备到云的消息Device-to-cloud messages

若要启用设备到云消息的复杂处理,IoT Edge 中心需要提供模块之间以及模块与 IoT 中心之间消息的声明性路由。To enable complex processing of device-to-cloud messages, IoT Edge hub provides declarative routing of messages between modules, and between modules and IoT Hub. 声明性路由允许模块拦截并处理其他模块发送的消息,并将它们传播到复杂管道。Declarative routing allows modules to intercept and process messages sent by other modules and propagate them into complex pipelines. 有关详细信息,请参阅在 IoT Edge 中部署模块和建立路由For more information, see deploy modules and establish routes in IoT Edge.

与常规 IoT 中心设备应用程序不同,IoT Edge 模块可以接收其本地 IoT Edge 中心所代理的设备到云消息,以便进行处理。An IoT Edge module, as opposed to a normal IoT Hub device application, can receive device-to-cloud messages that are being proxied by its local IoT Edge hub to process them.

IoT Edge 中心基于部署清单中介绍的声明性路由将消息传播到模块。IoT Edge hub propagates the messages to your module based on declarative routes described in the deployment manifest. 开发 IoT Edge 模块时,可以通过设置消息处理程序来接收这些消息。When developing an IoT Edge module, you can receive these messages by setting message handlers.

若要简化路由的创建,IoT Edge 要添加模块输入输出终结点的概念。To simplify the creation of routes, IoT Edge adds the concept of module input and output endpoints. 无需指定任意输入,模块即可接收所有路由给它的设备到云消息,并且无需指定任意输出,模块即可发送设备到云消息。A module can receive all device-to-cloud messages routed to it without specifying any input, and can send device-to-cloud messages without specifying any output. 然而使用显式输入和输出使得路由规则更容易理解。Using explicit inputs and outputs, though, makes routing rules simpler to understand.

最后,Edge 中心处理的设备到云消息标有以下系统属性:Finally, device-to-cloud messages handled by the Edge hub are stamped with the following system properties:

属性Property 说明Description
$connectionDeviceId$connectionDeviceId 发送消息的客户端的设备 IDThe device ID of the client that sent the message
$connectionModuleId$connectionModuleId 发送消息的模块的模块 IDThe module ID of the module that sent the message
$inputName$inputName 接收此消息的输入。The input that received this message. 可以为空。Can be empty.
$outputName$outputName 用来发送消息的输出。The output used to send the message. 可以为空。Can be empty.

从模块连接到 IoT Edge 中心Connecting to IoT Edge hub from a module

从模块连接到本地 IoT Edge 中心涉及两个步骤:Connecting to the local IoT Edge hub from a module involves two steps:

  1. 在应用程序中创建 ModuleClient 实例。Create a ModuleClient instance in your application.
  2. 确保应用程序接受该设备上 IoT Edge 中心提供的证书。Make sure your application accepts the certificate presented by the IoT Edge hub on that device.

创建一个 ModuleClient 实例,将模块连接到设备上运行的 IoT Edge 中心,其方式与 DeviceClient 实例将 IoT 设备连接到 IoT 中心类似。Create a ModuleClient instance to connect your module to the IoT Edge hub running on the device, similar to how DeviceClient instances connect IoT devices to IoT Hub. 有关 ModuleClient 类及其通信方法的更多信息,请参阅首选 SDK 语言的 API 参考:C#CPythonJavaNode.jsFor more information about the ModuleClient class and its communication methods, see the API reference for your preferred SDK language: C#, C, Python, Java, or Node.js.

语言和体系结构支持Language and architecture support

IoT Edge 支持多种操作系统、设备体系结构和开发语言,因此你可以构建满足你的需求的方案。IoT Edge supports multiple operating systems, device architectures, and development languages so that you can build the scenario that matches your needs. 使用此部分来了解用于开发自定义 IoT Edge 模块的选项。Use this section to understand your options for developing custom IoT Edge modules. 可以在为 IoT Edge 准备开发和测试环境中详细了解每种语言的工具支持和要求。You can learn more about tooling support and requirements for each language in Prepare your development and test environment for IoT Edge.

LinuxLinux

对于下表中的所有语言,IoT Edge 支持 AMD64 和 ARM32 Linux 设备的开发。For all languages in the following table, IoT Edge supports development for AMD64 and ARM32 Linux devices.

开发语言Development language 开发工具Development tools
CC Visual Studio CodeVisual Studio Code
Visual Studio 2017/2019Visual Studio 2017/2019
C#C# Visual Studio CodeVisual Studio Code
Visual Studio 2017/2019Visual Studio 2017/2019
JavaJava Visual Studio CodeVisual Studio Code
Node.jsNode.js Visual Studio CodeVisual Studio Code
PythonPython Visual Studio CodeVisual Studio Code

备注

公共预览版中提供了对 ARM64 Linux 设备的开发和调试支持。Develop and debugging support for ARM64 Linux devices is in public preview. 有关详细信息,请参阅在 Visual Studio Code(预览版)中开发和调试 ARM64 IoT Edge 模块For more information, see Develop and debug ARM64 IoT Edge modules in Visual Studio Code (preview).

WindowsWindows

对于下表中的所有语言,IoT Edge 支持 AMD64 Windows 设备的开发。For all languages in the following table, IoT Edge supports development for AMD64 Windows devices.

开发语言Development language 开发工具Development tools
CC Visual Studio 2017/2019Visual Studio 2017/2019
C#C# Visual Studio Code(无调试功能)Visual Studio Code (no debugging capabilities)
Visual Studio 2017/2019Visual Studio 2017/2019

后续步骤Next steps

为 IoT Edge 准备开发和测试环境Prepare your development and test environment for IoT Edge

使用 Visual Studio 开发适用于 IoT Edge 的 C# 模块Use Visual Studio to develop C# modules for IoT Edge

使用 Visual Studio Code 开发适用于 IoT Edge 的模块Use Visual Studio Code to develop modules for IoT Edge

了解和使用 Azure IoT 中心 SDKUnderstand and use Azure IoT Hub SDKs