

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

# 开发自定义预置插件
<a name="develop-custom-provisioning-plugins"></a>

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

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

**注意**  
[AWS IoT 实例集预置插件](fleet-provisioning.md)在安装过程中实现 `DeviceIdentityInterface`，以使用实例集预置。实例集预置插件是开源的，因此您可以浏览其源代码，查看预置插件接口使用示例。有关更多信息，请参阅上的[AWS IoT 舰队配置插件](https://github.com/aws-greengrass/aws-greengrass-fleet-provisioning-by-claim) GitHub。

**Topics**
+ [要求](#custom-provisioning-plugin-requirements)
+ [实现接 DeviceIdentityInterface 口](#implement-device-identity-interface)

## 要求
<a name="custom-provisioning-plugin-requirements"></a>

要开发自定义预置插件，必须创建满足以下要求的 Java 类：
+ 使用 `com.aws.greengrass` 程序包或 `com.aws.greengrass` 中的程序包。
+ 有一个不带任何参数的构造函数。
+ 实现 `DeviceIdentityInterface` 接口。有关更多信息，请参阅 [实现接 DeviceIdentityInterface 口](#implement-device-identity-interface)。

## 实现接 DeviceIdentityInterface 口
<a name="implement-device-identity-interface"></a>

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

**DeviceIdentityInterface 在自定义配置插件项目中使用**
+ 您可以将 Greengrass Nucleus JAR 文件添加为库，也可以将 Greengrass Nucleus 作为 Maven 依赖关系添加。请执行以下操作之一：
  + 要将 Greengrass nucleus JAR 文件添加为库，请下载包含 Greengrass nucleus JAR AWS IoT Greengrass 的核心软件。您可以从以下位置下载最新版本的 AWS IoT Greengrass Core 软件：
    + [https://d2s8p88vqu9w66.cloudfront。 net/releases/greengrass](https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-nucleus-latest.zip)-nucleus-latest.zip

    您可以在 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 接口
<a name="device-identity-interface-shape"></a>

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

**注意**  
[你也可以在 Greengrass nucleus 源代码的 [com.aws.greengrass.provisioning 包](https://github.com/aws-greengrass/aws-greengrass-nucleus/tree/main/src/main/java/com/aws/greengrass/provisioning)中探索这些类。](https://github.com/aws-greengrass/aws-greengrass-nucleus) GitHub

```
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 {}
```

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