

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Berinteraksilah dengan konfigurasi komponen
<a name="ipc-component-configuration"></a>

Layanan IPC konfigurasi komponen memungkinkan Anda melakukan hal berikut:
+ Dapatkan dan atur parameter konfigurasi komponen.
+ Berlangganan pembaruan konfigurasi komponen.
+ Validasi pembaruan konfigurasi komponen sebelum nukleus menerapkannya.

**Topics**
+ [SDK (Versi Minimum)](#ipc-component-configuration-sdk-versions)
+ [GetConfiguration](#ipc-operation-getconfiguration)
+ [UpdateConfiguration](#ipc-operation-updateconfiguration)
+ [SubscribeToConfigurationUpdate](#ipc-operation-subscribetoconfigurationupdate)
+ [SubscribeToValidateConfigurationUpdates](#ipc-operation-subscribetovalidateconfigurationupdates)
+ [SendConfigurationValidityReport](#ipc-operation-sendconfigurationvalidityreport)

## SDK (Versi Minimum)
<a name="ipc-component-configuration-sdk-versions"></a>

Tabel berikut mencantumkan versi minimum SDKs yang dapat Anda gunakan untuk berinteraksi dengan konfigurasi komponen.


| SDK | Versi minimum | 
| --- | --- | 
|  [AWS IoT Greengrass Komponen SDK (C, C \$1\$1, Karat)](https://github.com/aws-greengrass/aws-greengrass-component-sdk)  |  v1.0.0  | 
|  [AWS IoT Device SDK untuk Java v2](https://github.com/aws/aws-iot-device-sdk-java-v2)  |  v1.2.10  | 
|  [AWS IoT Device SDK untuk Python v2](https://github.com/aws/aws-iot-device-sdk-python-v2)  |  v1.5.3  | 
|  [AWS IoT Device SDK untuk C\$1\$1 v2](https://github.com/aws/aws-iot-device-sdk-cpp-v2)  |  v1.17.0  | 
|  [AWS IoT Device SDK untuk JavaScript v2](https://github.com/aws/aws-iot-device-sdk-js-v2)  |  v1.12.0  | 

## GetConfiguration
<a name="ipc-operation-getconfiguration"></a>

Dapatkan nilai konfigurasi untuk komponen pada perangkat inti. Anda menentukan jalur kunci untuk mendapatkan nilai konfigurasi.

### Permintaan
<a name="ipc-operation-getconfiguration-request"></a>

Permintaan operasi ini memiliki parameter berikut:

`componentName`(Python:) `component_name`  <a name="ipc-configuration-request-component-name"></a>
(Opsional) Nama komponen.  
Default untuk nama komponen yang membuat permintaan.

`keyPath`(Python:) `key_path`  
Path kunci untuk nilai konfigurasi. Tentukan daftar di mana setiap entri adalah kunci untuk satu tingkat dalam objek konfigurasi. Sebagai contoh, tentukan `["mqtt", "port"]` untuk mendapatkan nilai `port` dalam konfigurasi berikut.  

```
{
  "mqtt": {
    "port": 443
  }
}
```
Untuk mendapatkan konfigurasi lengkap komponen, tentukan daftar kosong.

### Respons
<a name="ipc-operation-getconfiguration-response"></a>

Tanggapan operasi ini memiliki informasi berikut:

`componentName`(Python:) `component_name`  <a name="ipc-configuration-response-component-name"></a>
Nama komponen.

`value`  
Konfigurasi yang diminta sebagai objek.

### Contoh
<a name="ipc-operation-getconfiguration-examples"></a>

Contoh-contoh berikut ini menunjukkan cara memanggil operasi ini dalam kode komponen kustom.

------
#### [ Rust ]

**Example Contoh: Dapatkan konfigurasi**  

```
use core::mem::MaybeUninit;
use gg_sdk::{Sdk, UnpackedObject};

fn main() {
    let sdk = Sdk::init();
    sdk.connect().expect("Failed to establish IPC connection");

    // Get a configuration value at key path ["mqtt", "port"]
    let mut buf = [MaybeUninit::uninit(); 1024];

    let value = sdk
        .get_config(&["mqtt", "port"], None, &mut buf)
        .expect("Failed to get configuration");

    if let UnpackedObject::I64(port) = value.unpack() {
        println!("Configuration value: {port}");
    }
}
```

------
#### [ C ]

**Example Contoh: Dapatkan konfigurasi**  

```
#include <gg/error.h>
#include <gg/ipc/client.h>
#include <gg/object.h>
#include <gg/sdk.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    gg_sdk_init();

    GgError err = ggipc_connect();
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to establish IPC connection.\n");
        exit(-1);
    }

    // Get a configuration value at key path ["mqtt", "port"]
    uint8_t response_mem[1024];
    GgObject value;

    err = ggipc_get_config(
        GG_BUF_LIST(GG_STR("mqtt"), GG_STR("port")),
        NULL, // component_name (NULL = current component)
        GG_BUF(response_mem),
        &value
    );
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to get configuration.\n");
        exit(-1);
    }

    if (gg_obj_type(value) == GG_TYPE_I64) {
        printf("Configuration value: %" PRId64 "\n", gg_obj_into_i64(value));
    } else if (gg_obj_type(value) == GG_TYPE_BUF) {
        GgBuffer buf = gg_obj_into_buf(value);
        printf("Configuration value: %.*s\n", (int) buf.len, buf.data);
    } else {
        printf("Configuration value is of unexpected type.\n");
    }
}
```

------
#### [ C\$1\$1 (Component SDK) ]

**Example Contoh: Dapatkan konfigurasi**  

```
#include <gg/ipc/client.hpp>
#include <iostream>

int main() {
    auto &client = gg::ipc::Client::get();

    auto error = client.connect();
    if (error) {
        std::cerr << "Failed to establish IPC connection.\n";
        exit(-1);
    }

    // Get a configuration value at key path ["mqtt", "port"]
    std::array key_path = { gg::Buffer { "mqtt" }, gg::Buffer { "port" } };
    int64_t value = 0;

    error = client.get_config(key_path, std::nullopt, value);
    if (error) {
        std::cerr << "Failed to get configuration.\n";
        exit(-1);
    }

    std::cout << "Configuration value: " << value << "\n";
}
```

------

## UpdateConfiguration
<a name="ipc-operation-updateconfiguration"></a>

Memperbarui nilai konfigurasi untuk komponen ini pada perangkat inti.

### Permintaan
<a name="ipc-operation-updateconfiguration-request"></a>

Permintaan operasi ini memiliki parameter berikut:

`keyPath`(Python:) `key_path`  
(Opsional) Path kunci untuk node kontainer (objek) yang akan diperbarui. Tentukan daftar di mana setiap entri adalah kunci untuk satu tingkat dalam objek konfigurasi. Sebagai contoh, tentukan path kunci `["mqtt"]` dan nilai merge `{ "port": 443 }` untuk mengatur nilai `port` dalam konfigurasi berikut.  

```
{
  "mqtt": {
    "port": 443
  }
}
```
Path kunci harus menentukan node kontainer (objek) dalam konfigurasi tersebut. Jika node tidak ada dalam konfigurasi komponen, operasi ini akan membuatnya dan menetapkan nilainya ke objek tersebut di `valueToMerge`.  
Default ke akar dari objek konfigurasi.

`timestamp`  
Jangka waktu Unix saat ini dalam milidetik. Operasi ini menggunakan ini stempel waktu untuk menyelesaikan pembaruan bersamaan untuk kunci. Jika kunci dalam konfigurasi komponen memiliki cap waktu yang lebih besar dari cap waktu dalam permintaan, maka permintaan akan gagal.

`valueToMerge`(Python:) `value_to_merge`  
Objek konfigurasi yang akan digabungkan di lokasi yang Anda tentukan di `keyPath`. Untuk informasi selengkapnya, lihat [Perbarui konfigurasi komponen](update-component-configurations.md).

### Respons
<a name="ipc-operation-updateconfiguration-response"></a>

Operasi ini tidak memberikan informasi apa pun dalam tanggapannya.

### Contoh
<a name="ipc-operation-updateconfiguration-examples"></a>

Contoh-contoh berikut ini menunjukkan cara memanggil operasi ini dalam kode komponen kustom.

------
#### [ Rust ]

**Example Contoh: Perbarui konfigurasi**  

```
use gg_sdk::Sdk;

fn main() {
    let sdk = Sdk::init();
    sdk.connect().expect("Failed to establish IPC connection");

    // Update configuration value at key path ["mqtt", "port"] to 443
    sdk.update_config(&["mqtt", "port"], None, 443)
        .expect("Failed to update configuration");

    println!("Successfully updated configuration.");
}
```

------
#### [ C ]

**Example Contoh: Perbarui konfigurasi**  

```
#include <gg/error.h>
#include <gg/ipc/client.h>
#include <gg/object.h>
#include <gg/sdk.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    gg_sdk_init();

    GgError err = ggipc_connect();
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to establish IPC connection.\n");
        exit(-1);
    }

    // Update configuration value at key path ["mqtt", "port"] to 443
    err = ggipc_update_config(
        GG_BUF_LIST(GG_STR("mqtt"), GG_STR("port")),
        NULL, // timestamp (NULL = current time)
        gg_obj_i64(443)
    );
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to update configuration.\n");
        exit(-1);
    }

    printf("Successfully updated configuration.\n");
}
```

------
#### [ C\$1\$1 (Component SDK) ]

**Example Contoh: Perbarui konfigurasi**  

```
#include <gg/ipc/client.hpp>
#include <iostream>

int main() {
    auto &client = gg::ipc::Client::get();

    auto error = client.connect();
    if (error) {
        std::cerr << "Failed to establish IPC connection.\n";
        exit(-1);
    }

    // Update configuration value at key path ["mqtt", "port"] to 443
    std::array key_path = { gg::Buffer { "mqtt" }, gg::Buffer { "port" } };

    error = client.update_config(key_path, 443);
    if (error) {
        std::cerr << "Failed to update configuration.\n";
        exit(-1);
    }

    std::cout << "Successfully updated configuration.\n";
}
```

------

## SubscribeToConfigurationUpdate
<a name="ipc-operation-subscribetoconfigurationupdate"></a>

Berlanggananlah untuk menerima notifikasi ketika konfigurasi komponen diperbarui. Ketika Anda berlangganan kunci, Anda akan menerima notifikasi ketika setiap anak dari kunci tersebut melakukan pembaruan.

<a name="ipc-subscribe-operation-note"></a>Operasi ini adalah operasi berlangganan di mana Anda berlangganan aliran pesan peristiwa. Untuk menggunakan operasi ini, tentukan bagian yang menangani respons aliran dengan fungsi yang menangani pesan peristiwa, kesalahan, dan penutupan aliran. Untuk informasi selengkapnya, lihat [Berlangganan pengaliran peristiwa IPC](interprocess-communication.md#ipc-subscribe-operations).

**Jenis pesan peristiwa:** `ConfigurationUpdateEvents`

### Permintaan
<a name="ipc-operation-subscribetoconfigurationupdate-request"></a>

Permintaan operasi ini memiliki parameter berikut:

`componentName`(Python:) `component_name`  <a name="ipc-configuration-request-component-name"></a>
(Opsional) Nama komponen.  
Default untuk nama komponen yang membuat permintaan.

`keyPath`(Python:) `key_path`  
Path kunci untuk nilai konfigurasi yang akan dijadikan langganan. Tentukan daftar di mana setiap entri adalah kunci untuk satu tingkat dalam objek konfigurasi. Sebagai contoh, tentukan `["mqtt", "port"]` untuk mendapatkan nilai `port` dalam konfigurasi berikut.  

```
{
  "mqtt": {
    "port": 443
  }
}
```
Untuk berlangganan pembaruan untuk semua nilai dalam konfigurasi komponen, tentukan daftar kosong.

### Respons
<a name="ipc-operation-subscribetoconfigurationupdate-response"></a>

Tanggapan operasi ini memiliki informasi berikut:

`messages`  
Aliran pesan notifikasi. Objek ini, `ConfigurationUpdateEvents`, berisi informasi berikut:    
`configurationUpdateEvent`(Python:) `configuration_update_event`  
Peristiwa pembaruan konfigurasi. Objek ini, `ConfigurationUpdateEvent`, berisi informasi berikut:    
`componentName`(Python:) `component_name`  <a name="ipc-configuration-response-component-name"></a>
Nama komponen.  
`keyPath`(Python:) `key_path`  
Path kunci untuk nilai konfigurasi yang diperbarui.

### Contoh
<a name="ipc-operation-subscribetoconfigurationupdate-examples"></a>

Contoh-contoh berikut ini menunjukkan cara memanggil operasi ini dalam kode komponen kustom.

------
#### [ Rust ]

**Example Contoh: Berlangganan pembaruan konfigurasi**  

```
use gg_sdk::Sdk;
use std::{thread, time::Duration};

fn main() {
    let sdk = Sdk::init();
    sdk.connect().expect("Failed to establish IPC connection");

    // Subscribe to configuration updates for key path ["mqtt"]
    let callback = |component_name: &str, key_path: &[&str]| {
        println!(
            "Received configuration update for component: {component_name}"
        );
        println!("Key path: {key_path:?}");
    };

    let _sub = sdk
        .subscribe_to_configuration_update(None, &["mqtt"], &callback)
        .expect("Failed to subscribe to configuration updates");

    println!("Successfully subscribed to configuration updates.");

    // Keep the main thread alive, or the process will exit.
    loop {
        thread::sleep(Duration::from_secs(10));
    }
}
```

------
#### [ C ]

**Example Contoh: Berlangganan pembaruan konfigurasi**  

```
#include <gg/error.h>
#include <gg/ipc/client.h>
#include <gg/object.h>
#include <gg/sdk.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static void on_subscription_response(
    void *ctx,
    GgBuffer component_name,
    GgList key_path,
    GgIpcSubscriptionHandle handle
) {
    (void) ctx;
    (void) handle;

    printf(
        "Received configuration update for component: %.*s\n",
        (int) component_name.len,
        component_name.data
    );

    printf("Key path: [");
    for (size_t i = 0; i < key_path.len; i++) {
        if (i > 0) {
            printf(", ");
        }
        GgObject *obj = &key_path.items[i];
        if (gg_obj_type(*obj) == GG_TYPE_BUF) {
            GgBuffer key = gg_obj_into_buf(*obj);
            printf("\"%.*s\"", (int) key.len, key.data);
        }
    }
    printf("]\n");
}

int main(void) {
    gg_sdk_init();

    GgError err = ggipc_connect();
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to establish IPC connection.\n");
        exit(-1);
    }

    // Subscribe to configuration updates for key path ["mqtt"]
    GgIpcSubscriptionHandle handle;
    err = ggipc_subscribe_to_configuration_update(
        NULL, // component_name (NULL = current component)
        GG_BUF_LIST(GG_STR("mqtt")),
        on_subscription_response,
        NULL,
        &handle
    );
    if (err != GG_ERR_OK) {
        fprintf(stderr, "Failed to subscribe to configuration updates.\n");
        exit(-1);
    }

    printf("Successfully subscribed to configuration updates.\n");

    // Keep the main thread alive, or the process will exit.
    while (1) {
        sleep(10);
    }

    // To stop subscribing, close the stream.
    ggipc_close_subscription(handle);
}
```

------
#### [ C\$1\$1 (Component SDK) ]

**Example Contoh: Berlangganan pembaruan konfigurasi**  

```
#include <gg/ipc/client.hpp>
#include <unistd.h>
#include <iostream>

class ResponseHandler : public gg::ipc::ConfigurationUpdateCallback {
    void operator()(
        std::string_view component_name,
        gg::List key_path,
        gg::ipc::Subscription &handle
    ) override {
        (void) handle;
        std::cout << "Received configuration update for component: "
                  << component_name << "\n";
        std::cout << "Key path: [";
        for (size_t i = 0; i < key_path.size(); i++) {
            if (i > 0) {
                std::cout << ", ";
            }
            std::cout << "\"" << get<gg::Buffer>(key_path[i]) << "\"";
        }
        std::cout << "]\n";
    }
};

int main() {
    auto &client = gg::ipc::Client::get();

    auto error = client.connect();
    if (error) {
        std::cerr << "Failed to establish IPC connection.\n";
        exit(-1);
    }

    // Subscribe to configuration updates for key path ["mqtt"]
    std::array key_path = { gg::Buffer { "mqtt" } };

    static ResponseHandler handler;
    error = client.subscribe_to_configuration_update(
        key_path, std::nullopt, handler
    );
    if (error) {
        std::cerr << "Failed to subscribe to configuration updates.\n";
        exit(-1);
    }

    std::cout << "Successfully subscribed to configuration updates.\n";

    // Keep the main thread alive, or the process will exit.
    while (1) {
        sleep(10);
    }
}
```

------

## SubscribeToValidateConfigurationUpdates
<a name="ipc-operation-subscribetovalidateconfigurationupdates"></a>

Berlanggananlah untuk menerima notifikasi sebelum konfigurasi komponen diperbarui. Hal ini memungkinkan komponen memvalidasi pembaruan konfigurasinya sendiri. Gunakan operasi [SendConfigurationValidityReport](#ipc-operation-sendconfigurationvalidityreport) untuk memberitahu nukleus apakah konfigurasi tersebut valid atau tidak.

**penting**  
Deployment lokal tidak menotifikasi komponen tentang pembaruan.

<a name="ipc-subscribe-operation-note"></a>Operasi ini adalah operasi berlangganan di mana Anda berlangganan aliran pesan peristiwa. Untuk menggunakan operasi ini, tentukan bagian yang menangani respons aliran dengan fungsi yang menangani pesan peristiwa, kesalahan, dan penutupan aliran. Untuk informasi selengkapnya, lihat [Berlangganan pengaliran peristiwa IPC](interprocess-communication.md#ipc-subscribe-operations).

**Jenis pesan peristiwa:** `ValidateConfigurationUpdateEvents`

### Permintaan
<a name="ipc-operation-subscribetovalidateconfigurationupdates-request"></a>

Operasi ini tidak memiliki parameter apa pun.

### Respons
<a name="ipc-operation-subscribetovalidateconfigurationupdates-response"></a>

Tanggapan operasi ini memiliki informasi berikut:

`messages`  
Aliran pesan notifikasi. Objek ini, `ValidateConfigurationUpdateEvents`, berisi informasi berikut:    
`validateConfigurationUpdateEvent`(Python:) `validate_configuration_update_event`  
Peristiwa pembaruan konfigurasi. Objek ini, `ValidateConfigurationUpdateEvent`, berisi informasi berikut:    
`deploymentId`(Python:) `deployment_id`  
ID AWS IoT Greengrass penyebaran yang memperbarui komponen.  
`configuration`  
Objek yang berisi konfigurasi baru.

## SendConfigurationValidityReport
<a name="ipc-operation-sendconfigurationvalidityreport"></a>

Beritahu nukleus apakah pembaruan konfigurasi komponen ini valid atau tidak. Deployment tersebut gagal jika Anda memberitahu nukleus bahwa konfigurasi baru tidak valid. Gunakan operasi [SubscribeToValidateConfigurationUpdates](#ipc-operation-subscribetovalidateconfigurationupdates) untuk berlangganan untuk memvalidasi pembaruan konfigurasi.

Jika komponen tidak menanggapi notifikasi pembaruan konfigurasi yang tervalidasi, inti akan menunggu sejumlah waktu yang Anda tentukan dalam kebijakan validasi konfigurasi deployment. Setelah batas waktu itu, nukleus akan melanjutkan deployment. Batas waktu validasi komponen default adalah 20 detik. Untuk informasi selengkapnya, lihat [Buat deployment](create-deployments.md) dan [DeploymentConfigurationValidationPolicy](https://docs.aws.amazon.com/greengrass/v2/APIReference/API_DeploymentConfigurationValidationPolicy.html)objek yang dapat Anda berikan saat Anda memanggil [CreateDeployment](https://docs.aws.amazon.com/greengrass/v2/APIReference/API_CreateDeployment.html)operasi.

### Permintaan
<a name="ipc-operation-sendconfigurationvalidityreport-request"></a>

Permintaan operasi ini memiliki parameter berikut:

`configurationValidityReport`(Python:) `configuration_validity_report`  
Laporan tersebut yang memberitahu inti apakah pembaruan konfigurasi valid atau tidak. Objek ini, `ConfigurationValidityReport`, berisi informasi berikut:    
`status`  
Status validitas. Enum ini, `ConfigurationValidityStatus`, memiliki nilai-nilai berikut:  
+ `ACCEPTED` – Konfigurasi ini valid dan nukleus dapat menerapkannya pada komponen ini.
+ `REJECTED` – Konfigurasi ini tidak valid dan deployment ini gagal.  
`deploymentId`(Python:) `deployment_id`  
ID AWS IoT Greengrass penyebaran yang meminta pembaruan konfigurasi.  
`message`  
(Opsional) Pesan yang melaporkan mengapa konfigurasi tidak valid.

### Respons
<a name="ipc-operation-sendconfigurationvalidityreport-response"></a>

Operasi ini tidak memberikan informasi apa pun dalam tanggapannya.