AWS AWS IoT 1-Click 使用 AWS IoT 1-Click - AWS IoT 1-Click

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

AWS AWS IoT 1-Click 使用 AWS IoT 1-Click

若要示範如何使用 AWS Command Line Interface (AWS CLI),請考慮使用 AWS IoT 1-Click 簡化其垃圾箱取件服務的處置公司案例。

在此案例中,每個垃圾桶會與 AWS IoT Enterprise Button 配對。垃圾箱已滿時,客戶只需按下相關聯的按鈕,即可請求更換垃圾箱。

注意

所有 AWS IoT Enterprise Button 裝置 ID 都以「G030PM」開頭。

垃圾處理公司會使用下列步驟來準備供客戶使用的 AWS IoT Enterprise Button。

準備 AWS IoT Enterprise Button 供客戶使用

  1. AWS IoT Enterprise Button 1-Click 行動裝置應用程式的唯一方法是使用 AWS IoT 1-Click 行動裝置應用程式。若要安裝應用程式,請參閱 AWS IoT 1-Click 行動應用程式。安裝應用程式後,請勿按下 Login to AWS Account (登入 AWS 帳戶) (通常情況會是如此)。在本練習中,我們想要示範如何使用 AWS CLI。如果您按下 Login to AWS Account (登入 AWS 帳戶),則系統會為您叫用 initiate-device-claimfinalize-device-claim 命令,我們希望使用 CLI「手動」執行此操作,如下列步驟所示。

  2. 對於 AWS CLI 示範目的,而不是按登入 AWS 帳戶下,選擇右上角的小圓形 Wi-Fi 圖示。接下來,選擇 Configure Wi-Fi (設定 Wi-Fi)。掃描或輸入裝置 ID,然後依照行動應用程式的其餘指示操作。

  3. 如果您未安裝 AWS CLI,請遵循安裝 AWS CLI。若要列出可用的 AWS IoT 1-Click AWS CLI 命令,請執行下列兩個命令。

    aws iot1click-projects help
    aws iot1click-devices help
  4. 若要將現在連接到 Wi-Fi 建立關聯的 AWS IoT Enterprise Button 與垃圾處理公司的 AWS 帳戶 Wi-Fi 建立關聯,請使用裝置上的裝置 ID 執行以下命令。

    aws iot1click-devices initiate-device-claim --device-id G030PM0123456789 { "State": "CLAIM_INITIATED" }

    按下裝置上的按鈕。在白光暫時閃爍之後,您應該會看到綠燈恆亮約一秒鐘。如果沒有這樣做,請重複先前的 Wi-Fi 連線程序。

  5. 在上一個步驟中看到綠燈恆亮後,請執行下列命令 (使用裝置的 ID 值)。

    aws iot1click-devices finalize-device-claim --device-id G030PM0123456789 { "State": "CLAIMED" }

    所以此"State": "CLAIMED"回應表示裝置已使用 AWS IoT 1-Click 服務成功註冊裝置。

    注意

    如果裝置製造商提供以「C-」開頭的領取程式碼,您可以只使用 aws iot1click-devices claim-devices-by-claim-code 命令來領取一或多個裝置,如下列範例所示。

    aws iot1click-devices claim-devices-by-claim-code --claim-code C-123EXAMPLE { "Total": 9 "ClaimCode": "C-123EXAMPLE" }

    在此範例中,"Total": 9表示與領取程式碼相關聯的九個裝置C-123EXAMPLE已使用 AWS IoT 1-Click 服務成功領取領取。

  6. 接下來,您可以建立名為的 JSON 文字檔,以準備為垃圾處理公司建立適當的 AWS IoT 1-Click 專案。create-project.json。該檔案包含下列項目:

    { "projectName": "SeattleDumpsters", "description": "All dumpsters in the Seattle region.", "placementTemplate": { "defaultAttributes": { "City" : "Seattle" }, "deviceTemplates": { "empty-dumpster-request" : { "deviceType": "button" } } } }

    placementTemplatedeviceTemplates 金鑰值對屬性會套用到 SeattleDumpsters 專案部分的所有按鈕。若要建立此專案,請執行下列命令 (假設create-project.json位於目前的工作目錄)。

    aws iot1click-projects create-project --cli-input-json file://create-project.json

    要查看新建立的專案,請執行以下命令。

    aws iot1click-projects list-projects { "projects": [ { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "createdDate": 1563483100, "updatedDate": 1563483100, "tags": {} } ] }

    如需詳細資訊,請執行 describe-project 命令,如下所示。

    aws iot1click-projects describe-project --project-name SeattleDumpsters { "project": { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "description": "All dumpsters in the Seattle region.", "createdDate": 1563483100, "updatedDate": 1563483100, "placementTemplate": { "defaultAttributes": { "City": "Seattle" }, "deviceTemplates": { "empty-dumpster-request": { "deviceType": "button", "callbackOverrides": {} } } }, "tags": {} } }
  7. 在為西雅圖區域建立專案後,接下來您要針對特定垃圾箱建立置放位置這 (針對客戶 217),如下所示。逸出的引號對 Windows 是必要的標記。

    aws iot1click-projects create-placement --project-name SeattleDumpsters --placement-name customer217 --attributes "{\"location\": \"1800 9th Ave Seattle, WA 98101\", \"phone\": \"206-123-4567\"}"

    若要檢視新建立的置放位置,請執行下列命令。

    aws iot1click-projects list-placements --project-name SeattleDumpsters { "placements": [ { "projectName": "SeattleDumpsters", "placementName": "customer217", "createdDate": 1563488454, "updatedDate": 1563488454 } ] }

    如需詳細資訊,請執行 describe-placement 命令,如下所示。

    aws iot1click-projects describe-placement --project-name SeattleDumpsters --placement-name customer217 { "placement": { "projectName": "SeattleDumpsters", "placementName": "customer217", "attributes": { "phone": "206-123-4567", "location": "1800 9th Ave Seattle, WA 98101" }, "createdDate": 1563488454, "updatedDate": 1563488454 } }
  8. 雖然裝置現在已與處置公司的 AWS IoT 1-Click 帳戶相關聯,但裝置與置放位置無關聯。透過執行下列命令即可確認此部份:

    aws iot1click-projects get-devices-in-placement --project-name SeattleDumpsters --placement-name customer217 { "devices": {} }

    若要將裝置與置放位置相關聯,請執行下列命令。

    aws iot1click-projects associate-device-with-placement --project-name SeattleDumpsters --placement-name customer217 --device-template-name empty-dumpster-request --device-id G030PM0123456789

    若要確認前一個命令,請再次執行 get-devices-in-placement

    aws iot1click-projects get-devices-in-placement --project-name SeattleDumpsters --placement-name customer217 { "devices": { "empty-dumpster-request": "G030PM0123456789" } }

    如需詳細資訊,請執行 describe-device 命令,如下所示 (請注意從 iot1click-projects 切換到 iot1click-devices)。

    aws iot1click-devices describe-device --device-id G030PM0123456789 { "DeviceDescription": { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "SeattleDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": false, "RemainingLife": 99.9, "Type": "button", "Tags": {} } }

    因為目前只有一個裝置,下列命令會產生類似的結果。

    aws iot1click-devices list-devices --device-type button { "Devices": [ { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "SeattleDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": false, "RemainingLife": 99.9, "Type": "button", "Tags": {} } ] }
  9. 若要確認裝置運作正常,請執行下列命令。適當調整時間戳記,這會使用 ISO 8061 格式

    aws iot1click-devices list-device-events --device-id G030PM0123456789 --from-time-stamp 2019-07-17T15:45:12.880Z --to-time-stamp 2019-07-19T15:45:12.880Z { "Events": [ { "Device": { "Attributes": {}, "DeviceId": "G030PM0123456789", "Type": "button" }, "StdEvent": "{\"clickType\": \"SINGLE\", \"reportedTime\": \"2019-07-18T23:47:55.015Z\", \"certificateId\": \"fe8798a6c97c62ef8756b80eeefdcf2280f3352f82faa8080c74cc4f4a4d1811\", \"remainingLife\": 99.85000000000001, \"testMode\": false}" } ] }

    在這裡,我們看到一個單擊事件 (\"clickType\": \"SINGLE\") 在 2019-07-18T 23:47:55 .015Z 發生。現在按兩下設備 (連續快速按兩下按鈕),然後再次執行該命令。現在請注意按兩下事件 (\"clickType\": \"DOUBLE\"),如下所示。

    aws iot1click-devices list-device-events --device-id G030PM0123456789 --from-time-stamp 2019-07-17T15:45:12.880Z --to-time-stamp 2019-07-19T15:45:12.880Z { "Events": [ { "Device": { "Attributes": {}, "DeviceId": "G030PM0123456789", "Type": "button" }, "StdEvent": "{\"clickType\": \"SINGLE\", \"reportedTime\": \"2019-07-18T23:47:55.015Z\", \"certificateId\": \"fe8798a6c97c62ef8756b80eeefdcf2280f3352f82faa8080c74cc4f4a4d1811\", \"remainingLife\": 99.85000000000001, \"testMode\": false}" }, { "Device": { "Attributes": {}, "DeviceId": "G030PM0123456789", "Type": "button" }, "StdEvent": "{\"clickType\": \"DOUBLE\", \"reportedTime\": \"2019-07-19T00:14:41.353Z\", \"certificateId\": \"fe8798a6c97c62ef8756b80eeefdcf2280f3352f82faa8080c74cc4f4a4d1811\", \"remainingLife\": 99.8, \"testMode\": false}" } ] }
  10. 每個裝置類型都有一組可叫用的裝置方法。若要列出您裝置類型的可用方法,請執行 get-device-methods 命令,如下所示。

    aws iot1click-devices get-device-methods --device-id G030PM0123456789 { "DeviceMethods": [ { "MethodName": "getDeviceHealthParameters" }, { "MethodName": "setDeviceHealthMonitorCallback" }, { "MethodName": "getDeviceHealthMonitorCallback" }, { "MethodName": "setOnClickCallback" }, { "MethodName": "getOnClickCallback" } ] }

    若要叫用其中一個可用的方法,請使用 invoke-device-method 命令,如下所示。

    aws iot1click-devices invoke-device-method --cli-input-json file://invoke-device-method.json { "DeviceMethodResponse": "{\"remainingLife\": 99.8}" }

    在這裡,invoke-device-method.json 包含下列內容:

    { "DeviceId": "G030PM0123456789", "DeviceMethod": { "DeviceType": "device", "MethodName": "getDeviceHealthParameters" } }
    注意

    get 方法 (例如 getDeviceHealthParameters) 預期不會有參數。因此,JSON 文件中的 "DeviceMethodParameters": "" 行不能被使用 (這樣做會導致這種情況:An error occurred (InvalidRequestException) when calling the InvokeDeviceMethod operation: A request parameter was invalid.)

  11. 透過執行 aws iot1click-devices list-devices --device-type button,您可以看到 Enabled 的預設值是 false。下列命令會將此金鑰設定為 true

    aws iot1click-devices update-device-state --device-id G030PM0123456789 --enabled

    要將其設回 false,請再次使用 --no-enabled 引數以執行前一個命令。

  12. 如果客戶資訊有所變更,您可以更新裝置的置放資訊,如下所示 (請注意從 iot1click-devices 切換到 iot1click-projects)。執行下列命令以檢視 customer217 的目前資訊 (請參閱 attributes)。

    aws iot1click-projects describe-placement --project-name SeattleDumpsters --placement-name customer217 { "placement": { "projectName": "SeattleDumpsters", "placementName": "customer217", "attributes": { "phone": "206-123-4567", "location": "1800 9th Ave Seattle, WA 98101" }, "createdDate": 1563488454, "updatedDate": 1563488454 } }

    接下來,執行下列命令以更新客戶的電話和置放屬性。

    aws iot1click-projects update-placement --cli-input-json file://update-placement.json

    在這裡,update-placement.json 包含下列內容:

    { "projectName": "SeattleDumpsters", "placementName": "customer217", "attributes": { "phone": "206-266-1000", "location": "410 Terry Ave N Seattle, WA 98109" } }

    若要檢閱此更新,請再次執行 describe-placement ,如圖所示。

    aws iot1click-projects describe-placement --project-name SeattleDumpsters --placement-name customer217 { "placement": { "projectName": "SeattleDumpsters", "placementName": "customer217", "attributes": { "phone": "206-266-1000", "location": "410 Terry Ave N Seattle, WA 98109" }, "createdDate": 1563488454, "updatedDate": 1563572842 } }
  13. 若要更新專案資訊,請使用 update-project 命令。一個項目通常包含多個客戶置放位置。以下是現有的 SeattleDumpster 專案資訊。

    aws iot1click-projects describe-project --project-name SeattleDumpsters { "project": { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "description": "All dumpsters in the Seattle region.", "createdDate": 1563483100, "updatedDate": 1563483100, "placementTemplate": { "defaultAttributes": { "City": "Seattle" }, "deviceTemplates": { "empty-dumpster-request": { "deviceType": "button", "callbackOverrides": {} } } }, "tags": {} } }

    要更改「所有垃圾桶在西雅圖區域」為「所有垃圾桶(庭院垃圾、回收箱和垃圾桶)」,請執行下列命令。

    aws iot1click-projects update-project --project-name SeattleDumpsters --description "All dumpsters (yard waste, recycling, garbage) in the Seattle region."

    您可以看到,"description" 金鑰值已更新為所有的 SeattleDumpsters 置放位置。

    aws iot1click-projects describe-project --project-name SeattleDumpsters { "project": { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "description": "All dumpsters (yard waste, recycling, garbage) in the Seattle region.", "createdDate": 1563483100, "updatedDate": 1563819039, "placementTemplate": { "defaultAttributes": { "City": "Seattle" }, "deviceTemplates": { "empty-dumpster-request": { "deviceType": "button", "callbackOverrides": {} } } }, "tags": {} } }
  14. 您可以使用標籤將中繼資訊套用至專案資源 (iot1click-projects) 和置放資源 (iot1click-devices),如下所示。

    aws iot1click-projects tag-resource --cli-input-json file://projects-tag-resource.json

    在這裡,projects-tag-resource.json 包含下列內容:

    { "resourceArn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "tags": { "Account": "45215", "Manager": "Tom Jones" } }

    若要列出專案資源的標籤,請執行下列動作。

    aws iot1click-projects list-tags-for-resource --resource-arn "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters" { "tags": { "Manager": "Tom Jones", "Account": "45215" } }

    若要查看專案標籤的關聯,請執行以下操作。

    aws iot1click-projects describe-project --project-name SeattleDumpsters { "project": { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "description": "All dumpsters (yard waste, recycling, garbage) in the Seattle region.", "createdDate": 1563483100, "updatedDate": 1563819039, "placementTemplate": { "defaultAttributes": { "City": "Seattle" }, "deviceTemplates": { "empty-dumpster-request": { "deviceType": "button", "callbackOverrides": {} } } }, "tags": { "Manager": "Tom Jones", "Account": "45215" } } }

    若要探索裝置亞馬遜資源名稱 (ARN),請執行下列命令。

    aws iot1click-devices list-devices { "Devices": [ { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "SeattleDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": true, "RemainingLife": 99.7, "Type": "button", "Tags": {} } ] }

    若要將標籤新增至上一個裝置,請執行下列動作。

    aws iot1click-devices tag-resource --cli-input-json file://devices-tag-resource.json

    在這裡,devices-tag-resources.json 包含以下內容 (請注意要求的 ResourceArnTags 大小寫)。

    { "ResourceArn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Tags": { "Driver": "John Smith", "Driver Phone": "206-123-4567" } }

    若要列出裝置資源的標籤,請執行下列動作。

    aws iot1click-devices list-tags-for-resource --resource-arn "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789" { "Tags": { "Driver Phone": "206-123-4567", "Driver": "John Smith" } }

    若要查看關聯的裝置標籤,請執行 list-devices

    aws iot1click-devices list-devices { "Devices": [ { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "SeattleDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": true, "RemainingLife": 99.7, "Type": "button", "Tags": { "Driver Phone": "206-123-4567", "Driver": "John Smith" } } ] }
  15. 此時,您可以將動作與裝置按鈕建立關聯,例如觸發 AWS Lambda 函數或傳送 Amazon SNS 訊息。您可以使用 AWS IoT 1-Click 主控台 (AWS IoT 1-Click 程式設計模型也是一個選項)。在適當的動作與裝置產生關聯之後,您可以將裝置移至客戶的置放位置,然後使用步驟 1 和 2 所述的相同程序,將裝置連接至他們的 Wi-Fi 網路。

AWS IoT 1-Click 裝置

下列步驟說明如何反轉 (復原) 先前的步驟。

  1. 若要取消標記專案資源,請執行下列命令。

    aws iot1click-projects untag-resource --resource-arn "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters" --tag-keys "Manager"

    這會移除專案的 Manager 標籤,如下所示。

    aws iot1click-projects describe-project --project-name SeattleDumpsters { "project": { "arn": "arn:aws:iot1click:us-west-2:012345678901:projects/SeattleDumpsters", "projectName": "SeattleDumpsters", "description": "All dumpsters (yard waste, recycling, garbage) in the Seattle region.", "createdDate": 1563483100, "updatedDate": 1563819039, "placementTemplate": { "defaultAttributes": { "City": "Seattle" }, "deviceTemplates": { "empty-dumpster-request": { "deviceType": "button", "callbackOverrides": {} } } }, "tags": { "Account": "45215" } } }
  2. 若要取消標記裝置資源,請執行下列命令。

    aws iot1click-devices untag-resource --resource-arn "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789" --tag-keys "Driver Phone" "Driver"

    這會移除裝置的標籤,如下所示 (請注意空白清單 "Tags": {})。

    aws iot1click-devices list-devices { "Devices": [ { "Arn": "arn:aws:iot1click:us-west-2:012345678901:devices/G030PM0123456789", "Attributes": { "projectRegion": "us-west-2", "projectName": "SeattleDumpsters", "placementName": "customer217", "deviceTemplateName": "empty-dumpster-request" }, "DeviceId": "G030PM0123456789", "Enabled": true, "RemainingLife": 99.7, "Type": "button", "Tags": {} } ] }
  3. 若要取消裝置與置放位置的關聯,請執行下列命令。

    aws iot1click-projects disassociate-device-from-placement --project-name SeattleDumpsters --placement-name customer217 --device-template-name empty-dumpster-request

    如下文所示,置放位置 customer217 不再具有與其相關聯的裝置。

    aws iot1click-projects get-devices-in-placement --project-name SeattleDumpsters --placement-name customer217 { "devices": {} }
  4. 若要從專案中刪除置放位置,請執行下列命令。

    aws iot1click-projects delete-placement --project-name SeattleDumpsters --placement-name customer217

    如下所示,專案沒 SeattleDumpsters 有置放位置,因為置放位置 customer217 是中唯一的置放位置 SeattleDumpsters

    aws iot1click-projects list-placements --project-name SeattleDumpsters { "placements": [] }
  5. 若要刪除專案,請執行下列命令。

    aws iot1click-projects delete-project --project-name SeattleDumpsters

    如您所見,所有專案都會被移除,因為SeattleDumpsters是唯一與 AWS IoT 1-Click 鍵式帳戶相關聯的專案。

    aws iot1click-projects list-projects { "projects": [] }

    舉例來說,如果您想讓朋友使用他們的 AWS 帳戶試用您的裝置,您必須先從 AWS IoT 1-Click 帳戶取消領取裝置,如下所示。

    aws iot1click-devices unclaim-device --device-id G030PM0123456789 { "State": "UNCLAIMED" }

    裝置現在可以與任何 AWS IoT 1-Click 帳戶一起使用。