AWS IoT Events-Data 使用 的範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

AWS IoT Events-Data 使用 的範例 AWS CLI

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 來執行動作和實作常見案例 AWS IoT Events-Data。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 batch-put-message

AWS CLI

將訊息 (輸入) 傳送至 AWS IoT 事件

下列batch-put-message範例會將一組訊息傳送至 AWS IoT Events 系統。每個訊息承載都會轉換為您指定的輸入 inputName (),並擷取到監控該輸入的任何偵測器。如果傳送多則訊息,則無法保證訊息的處理順序。若要保證訂購,您必須一次傳送訊息一個,並等待成功回應。

aws iotevents-data batch-put-message \ --cli-binary-format raw-in-base64-out \ --cli-input-json file://highPressureMessage.json

highPressureMessage.json 的內容:

{ "messages": [ { "messageId": "00001", "inputName": "PressureInput", "payload": "{\"motorid\": \"Fulton-A32\", \"sensorData\": {\"pressure\": 80, \"temperature\": 39} }" } ] }

輸出:

{ "BatchPutMessageErrorEntries": [] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 BatchPutMessage

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 BatchPutMessage

下列程式碼範例示範如何使用 batch-update-detector

AWS CLI

更新偵測器 (執行個體)

下列batch-update-detector範例會更新指定偵測器模型之一或多個偵測器 (執行個體) 的狀態、變數值和計時器設定。

aws iotevents-data batch-update-detector \ --cli-input-json file://budFulton-A32.json

budFulton-A32.json 的內容:

{ "detectors": [ { "messageId": "00001", "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32", "state": { "stateName": "Normal", "variables": [ { "name": "pressureThresholdBreached", "value": "0" } ], "timers": [ ] } } ] }

輸出:

{ "batchUpdateDetectorErrorEntries": [] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 BatchUpdateDetector

下列程式碼範例示範如何使用 create-detector-model

AWS CLI

若要建立偵測器模型

下列create-detector-model範例會建立偵測器模型。

aws iotevents create-detector-model \ --cli-input-json file://motorDetectorModel.json

motorDetectorModel.json 的內容:

{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "key": "motorid", "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }

輸出:

{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 CreateDetectorModel

下列程式碼範例示範如何使用 create-input

AWS CLI

建立輸入

下列create-input範例會建立輸入。

aws iotevents create-input \ --cli-input-json file://pressureInput.json

pressureInput.json 的內容:

{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }

輸出:

{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 CreateInput

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateInput

下列程式碼範例示範如何使用 delete-detector-model

AWS CLI

若要刪除偵測器模型

下列delete-detector-model範例會刪除偵測器模型。偵測器模型的任何作用中執行個體也會遭到刪除。

aws iotevents delete-detector-model \ --detector-model-name motorDetectorModel*

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DeleteDetectorModel

下列程式碼範例示範如何使用 delete-input

AWS CLI

刪除輸入

下列delete-input範例會刪除輸入。

aws iotevents delete-input \ --input-name PressureInput

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DeleteInput

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteInput

下列程式碼範例示範如何使用 describe-detector-model

AWS CLI

取得偵測器模型的相關資訊

下列describe-detector-model範例說明偵測器模型。如果未指定 version 參數,命令會傳回最新版本的相關資訊。

aws iotevents describe-detector-model \ --detector-model-name motorDetectorModel

輸出:

{ "detectorModel": { "detectorModelConfiguration": { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" }, "detectorModelDefinition": { "states": [ { "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70", "nextState": "Dangerous" } ], "events": [] }, "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ], "condition": "true" } ] }, "onExit": { "events": [] } }, { "onInput": { "transitionEvents": [ { "eventName": "BackToNormal", "actions": [], "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ], "events": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70" }, { "eventName": "Pressure Okay", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ], "condition": "$input.PressureInput.sensorData.pressure <= 70" } ] }, "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ], "condition": "$variable.pressureThresholdBreached > 1" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ], "condition": "true" } ] } } ], "initialStateName": "Normal" } } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DescribeDetectorModel

下列程式碼範例示範如何使用 describe-detector

AWS CLI

取得偵測器的相關資訊 (執行個體)

下列describe-detector範例會傳回指定偵測器 (執行個體) 的相關資訊。

aws iotevents-data describe-detector \ --detector-model-name motorDetectorModel \ --key-value "Fulton-A32"

輸出:

{ "detector": { "lastUpdateTime": 1560797852.776, "creationTime": 1560797852.775, "state": { "variables": [ { "name": "pressureThresholdBreached", "value": "3" } ], "stateName": "Dangerous", "timers": [] }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DescribeDetector

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeDetector

下列程式碼範例示範如何使用 describe-input

AWS CLI

取得輸入的相關資訊

下列describe-input範例會擷取輸入的詳細資訊。

aws iotevents describe-input \ --input-name PressureInput

輸出:

{ "input": { "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" }, "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DescribeInput

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeInput

下列程式碼範例示範如何使用 describe-logging-options

AWS CLI

若要取得記錄設定的相關資訊

下列describe-logging-options範例會擷取目前的 AWS IoT Events 記錄選項。

aws iotevents describe-logging-options

輸出:

{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "enabled": false, "level": "ERROR" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 DescribeLoggingOptions

下列程式碼範例示範如何使用 list-detector-model-versions

AWS CLI

取得偵測器模型版本的相關資訊

下列list-detector-model-versions範例會列出偵測器模型的所有版本。只會傳回與每個偵測器模型版本相關聯的中繼資料。

aws iotevents list-detector-model-versions \ --detector-model-name motorDetectorModel

輸出:

{ "detectorModelVersionSummaries": [ { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 ListDetectorModelVersions

下列程式碼範例示範如何使用 list-detector-models

AWS CLI

若要取得偵測器模型的清單

下列list-detector-models範例列出您建立的偵測器模型。只會傳回與每個偵測器模型相關聯的中繼資料。

aws iotevents list-detector-models

輸出:

{ "detectorModelSummaries": [ { "detectorModelName": "motorDetectorModel", "creationTime": 1552072424.212 "detectorModelDescription": "Detect overpressure in a motor." } ] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 ListDetectorModels

下列程式碼範例示範如何使用 list-detectors

AWS CLI

若要取得偵測器模型的偵測器清單

下列list-detectors範例列出偵測器 (偵測器模型的執行個體)。

aws iotevents-data list-detectors \ --detector-model-name motorDetectorModel

輸出:

{ "detectorSummaries": [ { "lastUpdateTime": 1558129925.2, "creationTime": 1552073155.527, "state": { "stateName": "Normal" }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 ListDetectors

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListDetectors

下列程式碼範例示範如何使用 list-inputs

AWS CLI

若要列出輸入

下列list-inputs範例列出您已建立的輸入。

aws iotevents list-inputs

輸出:

{ "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1551742986.768, "creationTime": 1551742986.768, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 ListInputs

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListInputs

下列程式碼範例示範如何使用 list-tags-for-resource

AWS CLI

列出指派給資源的標籤

下列list-tags-for-resource範例會列出您指派給資源的標籤 (中繼資料)。

aws iotevents list-tags-for-resource \ --resource-arn "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput"

輸出:

{ "tags": [ { "value": "motor", "key": "deviceType" } ] }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 ListTagsForResource

下列程式碼範例示範如何使用 put-logging-options

AWS CLI

設定記錄選項

下列list-tags-for-resource範例會設定或更新 AWS IoT Events 記錄選項。如果您更新任何loggingOptions欄位的值,變更最多需要一分鐘才會生效。此外,如果您變更附加至您在 roleArn 欄位中所指定角色的政策 (例如,更正無效政策),則變更最多需要五分鐘才會生效。

aws iotevents put-logging-options \ --cli-input-json file://logging-options.json

logging-options.json 的內容:

{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "level": "DEBUG", "enabled": true, "detectorDebugOptions": [ { "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32" } ] } }

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 PutLoggingOptions

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutLoggingOptions

下列程式碼範例示範如何使用 tag-resource

AWS CLI

將標籤新增至資源

下列tag-resource範例會新增或修改指定資源的標籤。標籤是可用於管理資源的中繼資料。

aws iotevents tag-resource \ --cli-input-json file://pressureInput.tag.json

pressureInput.tag.json 的內容:

{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tags": [ { "key": "deviceType", "value": "motor" } ] }

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 TagResource

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 TagResource

下列程式碼範例示範如何使用 untag-resource

AWS CLI

從資源中移除標籤

下列untag-resource範例會從資源中移除指定的標籤。

aws iotevents untag-resource \ --cli-input-json file://pressureInput.untag.json

pressureInput.untag.json 的內容:

{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tagKeys": [ "deviceType" ] }

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 UntagResource

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UntagResource

下列程式碼範例示範如何使用 update-detector-model

AWS CLI

更新偵測器模型

下列update-detector-model範例會更新偵測器模型。系統會刪除由先前版本所產生之偵測器 (執行個體),然後在新輸入到達時重新建立。

aws iotevents update-detector-model \ --cli-input-json file://motorDetectorModel.update.json

motorDetectorModel.update.json 的內容:

{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }

輸出:

{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560799387.719, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560799387.719, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "2" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 UpdateDetectorModel

下列程式碼範例示範如何使用 update-input

AWS CLI

更新輸入

下列update-input範例會更新輸入。

aws iotevents update-input \ --cli-input-json file://pressureInput.json

pressureInput.json 的內容:

{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }

輸出:

{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795976.458, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }

如需詳細資訊,請參閱 AWS IoT Events 開發人員指南*中的 UpdateInput

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateInput