步驟 4:在您的裝置上開發和測試元件 - AWS IoT Greengrass

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

步驟 4:在您的裝置上開發和測試元件

元件是在 AWS IoT Greengrass 核心裝置上執行的軟體模組。元件可讓您將複雜的應用程式建立和管理為分散的建置區塊,以便從一個 Greengrass 核心裝置重複使用。每個元件都由配方成品組成。

  • 配方

    每個元件都包含配方檔案,定義其中繼資料。配方也會指定元件的組態參數、元件相依性、生命週期和平台相容性。元件生命週期會定義安裝、執行和關閉元件的命令。如需詳細資訊,請參閱AWS IoT Greengrass 元件配方參考

    您可以 JSONYAML 格式定義配方。

  • 成品

    元件可以有任意數量的成品,這些成品是元件二進位檔。成品可以包含指令碼、編譯的程式碼、靜態資源,以及元件使用的任何其他檔案。元件也可以使用元件相依性的成品。

透過 AWS IoT Greengrass,您可以使用 Greengrass CLI 在 Greengrass 核心裝置上於本機開發和測試元件,而無需與 AWS 雲端互動。當您完成本機元件時,您可以使用元件配方和成品在 AWS 雲端的 AWS IoT Greengrass 服務中建立該元件,然後將其部署到所有 Greengrass 核心裝置。如需元件的詳細資訊,請參閱 開發 AWS IoT Greengrass 元件

在本節中,您將了解如何在核心裝置上於本機建立和執行基本 Hello World 元件。

在裝置上開發 Hello World 元件
  1. 使用配方和成品的子資料夾為您的元件建立資料夾。在 Greengrass 核心裝置上執行下列命令,以建立這些資料夾並變更為元件資料夾。將 ~/greengrassv2%USERPROFILE%\greengrassv2 取代為用於本機開發的資料夾路徑。

    Linux or Unix
    mkdir -p ~/greengrassv2/{recipes,artifacts} cd ~/greengrassv2
    Windows Command Prompt (CMD)
    mkdir %USERPROFILE%\greengrassv2\\recipes, %USERPROFILE%\greengrassv2\\artifacts cd %USERPROFILE%\greengrassv2
    PowerShell
    mkdir ~/greengrassv2/recipes, ~/greengrassv2/artifacts cd ~/greengrassv2
  2. 使用文字編輯器來建立配方檔案,定義元件的中繼資料、參數、相依性、生命週期和平台功能。在配方檔案名稱中包含元件版本,讓您可以識別哪個配方會反映哪個元件版本。您可以為您的配方選擇 YAML 或 JSON 格式。

    例如,在以 Linux 為基礎的系統上,您可以執行下列命令來使用 GNU nano 來建立 檔案。

    JSON
    nano recipes/com.example.HelloWorld-1.0.0.json
    YAML
    nano recipes/com.example.HelloWorld-1.0.0.yaml
    注意

    AWS IoT Greengrass 使用元件的語意版本。語意版本遵循 major.minor.patch 號碼系統。例如,版本1.0.0代表元件的第一個主要版本。如需詳細資訊,請參閱語意版本規格

  3. 將下列配方貼到 檔案中。

    JSON
    { "RecipeFormatVersion": "2020-01-25", "ComponentName": "com.example.HelloWorld", "ComponentVersion": "1.0.0", "ComponentDescription": "My first AWS IoT Greengrass component.", "ComponentPublisher": "Amazon", "ComponentConfiguration": { "DefaultConfiguration": { "Message": "world" } }, "Manifests": [ { "Platform": { "os": "linux" }, "Lifecycle": { "run": "python3 -u {artifacts:path}/hello_world.py {configuration:/Message}" } }, { "Platform": { "os": "windows" }, "Lifecycle": { "run": "py -3 -u {artifacts:path}/hello_world.py {configuration:/Message}" } } ] }
    YAML
    --- RecipeFormatVersion: '2020-01-25' ComponentName: com.example.HelloWorld ComponentVersion: '1.0.0' ComponentDescription: My first AWS IoT Greengrass component. ComponentPublisher: Amazon ComponentConfiguration: DefaultConfiguration: Message: world Manifests: - Platform: os: linux Lifecycle: run: | python3 -u {artifacts:path}/hello_world.py "{configuration:/Message}" - Platform: os: windows Lifecycle: run: | py -3 -u {artifacts:path}/hello_world.py "{configuration:/Message}"

    此配方的 ComponentConfiguration 區段定義 參數 Message,預設為 worldManifests 區段定義資訊清單,這是一組平台的生命週期指示和成品。您可以定義多個資訊清單,以指定各種平台的不同安裝指示,例如 。在資訊清單中, Lifecycle區段會指示 Greengrass 核心裝置以Message參數值做為引數來執行 Hello World 指令碼。

  4. 執行下列命令來建立元件成品的資料夾。

    Linux or Unix
    mkdir -p artifacts/com.example.HelloWorld/1.0.0
    Windows Command Prompt (CMD)
    mkdir artifacts\com.example.HelloWorld\1.0.0
    PowerShell
    mkdir artifacts\com.example.HelloWorld\1.0.0
    重要

    您必須針對成品資料夾路徑使用下列格式。包含您在配方中指定的元件名稱和版本。

    artifacts/componentName/componentVersion/
  5. 使用文字編輯器為您的 Hello World 元件建立 Python 指令碼成品檔案。

    例如,在以 Linux 為基礎的系統上,您可以執行下列命令來使用 GNU nano 來建立 檔案。

    nano artifacts/com.example.HelloWorld/1.0.0/hello_world.py

    將下列 Python 指令碼複製並貼到 檔案中。

    import sys message = "Hello, %s!" % sys.argv[1] # Print the message to stdout, which Greengrass saves in a log file. print(message)
  6. 使用本機 AWS IoT Greengrass CLI 來管理 Greengrass 核心裝置上的元件。

    執行下列命令,將元件部署至 AWS IoT Greengrass 核心。將 /greengrass/v2C:\greengrass\v2 取代為您的 AWS IoT Greengrass V2 根資料夾,並將 ~/greengrassv2%USERPROFILE%\greengrassv2 取代為您的元件開發資料夾。

    Linux or Unix
    sudo /greengrass/v2/bin/greengrass-cli deployment create \ --recipeDir ~/greengrassv2/recipes \ --artifactDir ~/greengrassv2/artifacts \ --merge "com.example.HelloWorld=1.0.0"
    Windows Command Prompt (CMD)
    C:\greengrass\v2\bin\greengrass-cli deployment create ^ --recipeDir %USERPROFILE%\greengrassv2\recipes ^ --artifactDir %USERPROFILE%\greengrassv2\artifacts ^ --merge "com.example.HelloWorld=1.0.0"
    PowerShell
    C:\greengrass\v2\bin\greengrass-cli deployment create ` --recipeDir ~/greengrassv2/recipes ` --artifactDir ~/greengrassv2/artifacts ` --merge "com.example.HelloWorld=1.0.0"

    此命令會新增在 中使用配方的元件,recipes以及在 中使用 Python 指令碼的元件artifacts--merge 選項會新增或更新您指定的元件和版本。

  7. Core AWS IoT Greengrass 軟體會將 stdout 從元件程序儲存至 logs 資料夾中的日誌檔案。執行下列命令來驗證 Hello World 元件是否執行並列印訊息。

    Linux or Unix
    sudo tail -f /greengrass/v2/logs/com.example.HelloWorld.log
    Windows Command Prompt (CMD)
    type C:\greengrass\v2\logs\com.example.HelloWorld.log

    type 命令會將檔案的內容寫入終端機。多次執行此命令,以觀察 檔案中的變更。

    PowerShell
    gc C:\greengrass\v2\logs\com.example.HelloWorld.log -Tail 10 -Wait

    您應該會看到類似下列範例的訊息。

    Hello, world!
    注意

    如果檔案不存在,則本機部署可能尚未完成。如果檔案在 15 秒內不存在,則部署可能會失敗。例如,如果您的配方無效,就可能會發生這種情況。執行下列命令來檢視 AWS IoT Greengrass 核心日誌檔案。此檔案包含 Greengrass 核心裝置部署服務的日誌。

    Linux or Unix
    sudo tail -f /greengrass/v2/logs/greengrass.log
    Windows Command Prompt (CMD)
    type C:\greengrass\v2\logs\greengrass.log

    type 命令會將檔案的內容寫入終端機。多次執行此命令,以觀察 檔案中的變更。

    PowerShell
    gc C:\greengrass\v2\logs\greengrass.log -Tail 10 -Wait
  8. 修改本機元件以反覆執行和測試程式碼。在文字編輯器hello_world.py中開啟 ,並在第 4 行新增下列程式碼,以編輯 AWS IoT Greengrass 核心記錄的訊息。

    message += " Greetings from your first Greengrass component."

    hello_world.py 指令碼現在應具有下列內容。

    import sys message = "Hello, %s!" % sys.argv[1] message += " Greetings from your first Greengrass component." # Print the message to stdout, which Greengrass saves in a log file. print(message)
  9. 執行下列命令,以您的變更更新元件。

    Linux or Unix
    sudo /greengrass/v2/bin/greengrass-cli deployment create \ --recipeDir ~/greengrassv2/recipes \ --artifactDir ~/greengrassv2/artifacts \ --merge "com.example.HelloWorld=1.0.0"
    Windows Command Prompt (CMD)
    C:\greengrass\v2\bin\greengrass-cli deployment create ^ --recipeDir %USERPROFILE%\greengrassv2\recipes ^ --artifactDir %USERPROFILE%\greengrassv2\artifacts ^ --merge "com.example.HelloWorld=1.0.0"
    PowerShell
    C:\greengrass\v2\bin\greengrass-cli deployment create ` --recipeDir ~/greengrassv2/recipes ` --artifactDir ~/greengrassv2/artifacts ` --merge "com.example.HelloWorld=1.0.0"

    此命令會使用最新的 Hello World 成品更新com.example.HelloWorld元件。

  10. 執行下列命令以重新啟動元件。當您重新啟動元件時,核心裝置會使用最新的變更。

    Linux or Unix
    sudo /greengrass/v2/bin/greengrass-cli component restart \ --names "com.example.HelloWorld"
    Windows Command Prompt (CMD)
    C:\greengrass\v2\bin\greengrass-cli component restart ^ --names "com.example.HelloWorld"
    PowerShell
    C:\greengrass\v2\bin\greengrass-cli component restart ` --names "com.example.HelloWorld"
  11. 再次檢查日誌,確認 Hello World 元件列印新訊息。

    Linux or Unix
    sudo tail -f /greengrass/v2/logs/com.example.HelloWorld.log
    Windows Command Prompt (CMD)
    type C:\greengrass\v2\logs\com.example.HelloWorld.log

    type 命令會將檔案的內容寫入終端機。多次執行此命令,以觀察 檔案中的變更。

    PowerShell
    gc C:\greengrass\v2\logs\com.example.HelloWorld.log -Tail 10 -Wait

    您應該會看到類似下列範例的訊息。

    Hello, world! Greetings from your first Greengrass component.
  12. 您可以更新元件的組態參數,以測試不同的組態。部署元件時,您可以指定組態更新,以定義如何修改核心裝置上的元件組態。您可以指定要重設為預設值的組態值,以及要合併至核心裝置的新組態值。如需詳細資訊,請參閱更新元件組態

    請執行下列操作:

    1. 使用文字編輯器來建立名為 的檔案hello-world-config-update.json,以包含組態更新

      例如,在以 Linux 為基礎的系統上,您可以執行下列命令來使用 GNU nano 來建立 檔案。

      nano hello-world-config-update.json
    2. 將下列 JSON 物件複製並貼入 檔案。此 JSON 物件會定義組態更新,將值合併friendMessage 參數以更新其值。此組態更新不會指定任何要重設的值。您不需要重設 Message 參數,因為合併更新會取代現有的值。

      { "com.example.HelloWorld": { "MERGE": { "Message": "friend" } } }
    3. 執行下列命令,將組態更新部署到 Hello World 元件。

      Linux or Unix
      sudo /greengrass/v2/bin/greengrass-cli deployment create \ --merge "com.example.HelloWorld=1.0.0" \ --update-config hello-world-config-update.json
      Windows Command Prompt (CMD)
      C:\greengrass\v2\bin\greengrass-cli deployment create ^ --merge "com.example.HelloWorld=1.0.0" ^ --update-config hello-world-config-update.json
      PowerShell
      C:\greengrass\v2\bin\greengrass-cli deployment create ` --merge "com.example.HelloWorld=1.0.0" ` --update-config hello-world-config-update.json
    4. 再次檢查日誌,確認 Hello World 元件輸出新訊息。

      Linux or Unix
      sudo tail -f /greengrass/v2/logs/com.example.HelloWorld.log
      Windows Command Prompt (CMD)
      type C:\greengrass\v2\logs\com.example.HelloWorld.log

      type 命令會將檔案的內容寫入終端機。多次執行此命令,以觀察 檔案中的變更。

      PowerShell
      gc C:\greengrass\v2\logs\com.example.HelloWorld.log -Tail 10 -Wait

      您應該會看到類似下列範例的訊息。

      Hello, friend! Greetings from your first Greengrass component.
  13. 完成元件測試後,請將其從核心裝置中移除。執行下列命令。

    Linux or Unix
    sudo /greengrass/v2/bin/greengrass-cli deployment create --remove="com.example.HelloWorld"
    Windows Command Prompt (CMD)
    C:\greengrass\v2\bin\greengrass-cli deployment create --remove="com.example.HelloWorld"
    PowerShell
    C:\greengrass\v2\bin\greengrass-cli deployment create --remove="com.example.HelloWorld"
    重要

    上傳元件到 之後,您需要此步驟,才能將元件部署回核心裝置 AWS IoT Greengrass。否則,部署會失敗並出現版本相容性錯誤,因為本機部署會指定不同版本的元件。

    執行下列命令,並確認com.example.HelloWorld元件未出現在裝置上的元件清單中。

    Linux or Unix
    sudo /greengrass/v2/bin/greengrass-cli component list
    Windows Command Prompt (CMD)
    C:\greengrass\v2\bin\greengrass-cli component list
    PowerShell
    C:\greengrass\v2\bin\greengrass-cli component list

您的 Hello World 元件已完成,您現在可以將其上傳至 AWS IoT Greengrass 雲端服務。然後,您可以將元件部署到 Greengrass 核心裝置。