开发自定义预置插件 - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

开发自定义预置插件

要开发自定义预置插件,请创建实现该 com.aws.greengrass.provisioning.DeviceIdentityInterface 接口的 Java 类。您可以在项目中包含 Greengrass Nucleus JAR 文件来访问此接口及其类。此接口定义了一种输入插件配置并输出预置配置的方法。预置配置定义了系统和 Greengrass Nucleus 组件的配置。Amazon IoT Greengrass Core 软件安装程序使用此预置配置在设备上配置 Amazon IoT Greengrass Core 软件。

开发自定义预置插件后,将其生成为 JAR 文件,您可以将其提供给 Amazon IoT Greengrass Core 软件安装程序,以便在安装期间运行您的插件。安装程序在其使用的同一 JVM 中运行您的自定义配置插件,因此您可以创建仅包含插件代码的 JAR。

注意

Amazon IoT 实例集预置插件在安装过程中实现 DeviceIdentityInterface,以使用实例集预置。实例集预置插件是开源的,因此您可以浏览其源代码,查看预置插件接口使用示例。有关更多信息,请参阅 GitHub 上的 Amazon IoT 实例集预置插件

要求

要开发自定义预置插件,必须创建满足以下要求的 Java 类:

  • 使用 com.aws.greengrass 程序包或 com.aws.greengrass 中的程序包。

  • 有一个不带任何参数的构造函数。

  • 实现 DeviceIdentityInterface 接口。有关更多信息,请参阅 实现 DeviceIdentityInterface 接口

实现 DeviceIdentityInterface 接口

要在自定义插件中使用该 com.aws.greengrass.provisioning.DeviceIdentityInterface 接口,请将 Greengrass Nucleus 作为依赖关系添加到您的项目中。

要在自定义预置插件项目中使用 DeviceIdentityInterface
  • 您可以将 Greengrass Nucleus JAR 文件添加为库,也可以将 Greengrass Nucleus 作为 Maven 依赖关系添加。请执行以下操作之一:

    • 要将 Greengrass Nucleus JAR 文件添加为库,请下载包含 Greengrass Nucleus JAR 的 Amazon IoT Greengrass Core 软件。您可以从以下位置下载最新版 Amazon IoT Greengrass Core 软件:

      您可以在 ZIP 文件的 lib 文件夹中找到 Greengrass Nucleus JAR 文件(Greengrass.jar)。将此 JAR 文件添加到您的项目中。

    • 要在 Maven 项目中使用 Greengrass Nucleus,请在 com.aws.greengrass 组中添加 nucleus 构件依赖关系。您还须添加 greengrass-common 存储库,因为 Greengrass Nucleus 在 Maven 中央存储库中不可用。

      <project ...> ... <repositories> <repository> <id>greengrass-common</id> <name>greengrass common</name> <url>https://d2jrmugq4soldf.cloudfront.net/snapshots</url> </repository> </repositories> ... <dependencies> <dependency> <groupId>com.aws.greengrass</groupId> <artifactId>nucleus</artifactId> <version>2.5.0-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> </project>

DeviceIdentityInterface 接口

com.aws.greengrass.provisioning.DeviceIdentityInterface 接口具有以下形态。

注意

您也可以在 GitHub 上的 Greengrass Nucleus 源代码com.aws.greengrass.provisioning 程序包中浏览这些类。

public interface com.aws.greengrass.provisioning.DeviceIdentityInterface { ProvisionConfiguration updateIdentityConfiguration(ProvisionContext context) throws RetryableProvisioningException, InterruptedException; // Return the name of the plugin. String name(); } com.aws.greengrass.provisioning.ProvisionConfiguration { SystemConfiguration systemConfiguration; NucleusConfiguration nucleusConfiguration } com.aws.greengrass.provisioning.ProvisionConfiguration.SystemConfiguration { String certificateFilePath; String privateKeyPath; String rootCAPath; String thingName; } com.aws.greengrass.provisioning.ProvisionConfiguration.NucleusConfiguration { String awsRegion; String iotCredentialsEndpoint; String iotDataEndpoint; String iotRoleAlias; } com.aws.greengrass.provisioning.ProvisioningContext { Map<String, Object> parameterMap; String provisioningPolicy; // The policy is always "PROVISION_IF_NOT_PROVISIONED". } com.aws.greengrass.provisioning.exceptions.RetryableProvisioningException {}

SystemConfigurationNucleusConfiguration 中的每个配置值都是安装 Amazon IoT Greengrass Core 软件所必需的,但您可以返回 null。如果自定义预置插件的任何配置值返回 null,则必须在创建提供给 Amazon IoT Greengrass Core 软件安装程序的 config.yaml 文件时,在系统或核心配置中提供该值。如果您的自定义预置插件为您在 config.yaml 中也定义的选项返回一个非空值,则安装程序会用插件返回的值替换 config.yaml 中的值。