更新零組件組態 - AWS IoT Greengrass

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

更新零組件組態

組件配置是定義每個組件參數的 JSON 對象。每個元件的配方都會定義其預設組態,您可以在將元件部署到核心裝置時修改這些組態。

建立部署時,您可以指定要套用至每個元件的規劃更新。組態更新是修補程式作業,這表示更新會修改核心裝置上存在的元件組態。如果核心裝置沒有元件,則組態更新會修改並套用該部署的預設組態。

組態更新會定義重設更新和合併更新。重設更新定義要重設為預設值或移除的組態值。合併更新會定義要為零組件設定的新組態值。當您部署組態更新時, AWS IoT Greengrass 核心軟體會在合併更新之前執行重設更新。

元件可以驗證您部署的組態更新。元件會訂閱在部署變更其組態時接收通知,而且可以拒絕不支援的組態。如需詳細資訊,請參閱 與組件配置互動

重設更新

重設更新會定義要在核心裝置上將哪些組態值重設為其預設值。如果組態值沒有預設值,則重設更新會從元件的組態中移除該值。這可協助您修正因組態無效而中斷的元件。

使用 JSON 指標清單來定義要重設的組態值。JSON 指針以正斜線開頭/。要識別巢狀零組件模型組態中的值,請使用正斜線 (/) 來分隔模型組態中每個層級的關鍵字。如需詳細資訊,請參閱 JSON 指標規格

注意

您只能將整個清單重設為預設值。您無法使用重設更新來重設清單中的個別元素。

若要將組件的整個組態重設為預設值,請指定單一空字串作為重設更新。

"reset": [""]

合併更新

合併更新定義要插入核心元件組態中的組態值。合併更新是一個 JSON 物件, AWS IoT Greengrass 核心軟體會在重設更新中指定的路徑中重設值之後合併。當您使用 AWS CLI 或 AWS SDK 時,您必須將此 JSON 物件序列化為字串。

您可以合併元件預設組態中不存在的索引鍵值配對。您也可以合併具有與具有相同索引鍵之值不同類型的鍵值配對。新值會取代舊值。這表示您可以變更配置物件的結構。

您可以合併 null 值和空字串、清單和物件。

注意

您不能將合併更新用於將元素插入或附加到列表中。您可以取代整個清單,也可以定義每個元素都有唯一索引鍵的物件。

AWS IoT Greengrass 使用 JSON 作為組態值。JSON 指定了一個數字類型,但不區分整數和浮點數。因此,組態值可能會轉換為中 AWS IoT Greengrass的浮點數。若要確保您的元件使用正確的資料類型,建議您將數值組態值定義為字串。然後,讓你的組件將它們解析為整數或浮點數。如此可確保您的組態值在組態和核心裝置上具有相同的類型。

在合併更新中使用配方變數

此功能適用於 v2.6.0 及更高版 Greeng rass 核組件。

如果您將 Greengrass 核心的interpolateComponentConfiguration組態選項設定為true,則可以在合併更新中使用 recipe 變數以外的component_dependency_name:configuration:json_pointer配方變數。例如,您可以在合併更新中使用 {iot:thingName} recipe 變數,將核心裝置的 AWS IoT 物件名稱包含在元件組態值中,例如處理序間通訊 (IPC) 授權原則。

範例

下列範例示範具有下列預設組態之儀表板元件的組態更新。此範例元件顯示有關工業設備的資訊。

{ "name": null, "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 80, "https": 443 }, }, "tags": [] }
JSON
{ "RecipeFormatVersion": "2020-01-25", "ComponentName": "com.example.IndustrialDashboard", "ComponentVersion": "1.0.0", "ComponentDescription": "Displays information about industrial equipment.", "ComponentPublisher": "Amazon", "ComponentConfiguration": { "DefaultConfiguration": { "name": null, "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 80, "https": 443 }, }, "tags": [] } }, "Manifests": [ { "Platform": { "os": "linux" }, "Lifecycle": { "run": "python3 -u {artifacts:path}/industrial_dashboard.py" } }, { "Platform": { "os": "windows" }, "Lifecycle": { "run": "py -3 -u {artifacts:path}/industrial_dashboard.py" } } ] }
YAML
--- RecipeFormatVersion: '2020-01-25' ComponentName: com.example.IndustrialDashboard ComponentVersion: '1.0.0' ComponentDescription: Displays information about industrial equipment. ComponentPublisher: Amazon ComponentConfiguration: DefaultConfiguration: name: null mode: REQUEST network: useHttps: true port: http: 80 https: 443 tags: [] Manifests: - Platform: os: linux Lifecycle: run: | python3 -u {artifacts:path}/industrial_dashboard.py - Platform: os: windows Lifecycle: run: | py -3 -u {artifacts:path}/industrial_dashboard.py
範例 1:合併更新

您可以建立套用下列規劃更新的部署,該更新會指定合併更新,但不指定重設更新。此組態更新會告訴元件在 HTTP 連接埠 8080 上顯示儀表板,其中包含來自兩個鍋爐的資料。

Console
要合併的組態
{ "name": "Factory 2A", "network": { "useHttps": false, "port": { "http": 8080 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] }
AWS CLI

下列指令會建立核心裝置的部署。

aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment.json

該文dashboard-deployment.json件包含以下 JSON 文檔。

{ "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore", "deploymentName": "Deployment for MyGreengrassCore", "components": { "com.example.IndustrialDashboard": { "componentVersion": "1.0.0", "configurationUpdate": { "merge": "{\"name\":\"Factory 2A\",\"network\":{\"useHttps\":false,\"port\":{\"http\":8080}},\"tags\":[\"/boiler/1/temperature\",\"/boiler/1/pressure\",\"/boiler/2/temperature\",\"/boiler/2/pressure\"]}" } } } }
Greengrass CLI

以下 Greengrass CLI 命令會在核心裝置上建立本機部署。

sudo greengrass-cli deployment create \ --recipeDir recipes \ --artifactDir artifacts \ --merge "com.example.IndustrialDashboard=1.0.0" \ --update-config dashboard-configuration.json

該文dashboard-configuration.json件包含以下 JSON 文檔。

{ "com.example.IndustrialDashboard": { "MERGE": { "name": "Factory 2A", "network": { "useHttps": false, "port": { "http": 8080 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] } } }

此更新之後,儀表板元件具有下列組態。

{ "name": "Factory 2A", "mode": "REQUEST", "network": { "useHttps": false, "port": { "http": 8080, "https": 443 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] }
範例 2:重設和合併更新

然後,您會建立套用下列規劃更新的部署,該更新會指定重設更新和合併更新。這些更新會指定在預設 HTTPS 連接埠上顯示儀表板,其中包含來自不同鍋爐的資料。這些更新會修改前一個範例中組態更新所產生的組態。

Console
重設路徑
[ "/network/useHttps", "/tags" ]
要合併的組態
{ "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure" ] }
AWS CLI

下列指令會建立核心裝置的部署。

aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment2.json

該文dashboard-deployment2.json件包含以下 JSON 文檔。

{ "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore", "deploymentName": "Deployment for MyGreengrassCore", "components": { "com.example.IndustrialDashboard": { "componentVersion": "1.0.0", "configurationUpdate": { "reset": [ "/network/useHttps", "/tags" ], "merge": "{\"tags\":[\"/boiler/3/temperature\",\"/boiler/3/pressure\",\"/boiler/4/temperature\",\"/boiler/4/pressure\"]}" } } } }
Greengrass CLI

以下 Greengrass CLI 命令會在核心裝置上建立本機部署。

sudo greengrass-cli deployment create \ --recipeDir recipes \ --artifactDir artifacts \ --merge "com.example.IndustrialDashboard=1.0.0" \ --update-config dashboard-configuration2.json

該文dashboard-configuration2.json件包含以下 JSON 文檔。

{ "com.example.IndustrialDashboard": { "RESET": [ "/network/useHttps", "/tags" ], "MERGE": { "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure" ] } } }

此更新之後,儀表板元件具有下列組態。

{ "name": "Factory 2A", "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 8080, "https": 443 } }, "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure", ] }