建立偵測器模型以代表 中的裝置狀態 AWS IoT Events - AWS IoT Events

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

建立偵測器模型以代表 中的裝置狀態 AWS IoT Events

在 中建立 AWS IoT Events 輸入以擷取裝置資料,您input根據報告來自馬達壓力資料的訊息建立 。若要繼續範例,以下是可回應馬達中過壓事件的偵測器模型。

您可以建立兩個狀態:"Normal" 和 "Dangerous"。每個偵測器 (執行個體) 都會在建立時進入「Normal」狀態。當具有 key "motorid" 唯一值的輸入到達時,便會建立執行個體。

如果偵測器執行個體收到 70 或更高的壓力讀數,則會進入「Dangerous」狀態,並傳送 Amazon SNS 訊息作為警告。如果連續三個輸入的壓力讀數回到正常 (低於 70),偵測器會回到「Normal」狀態,並傳送另一個 Amazon SNS 訊息作為全部清除。

此範例偵測器模型假設您已建立兩個 Amazon SNS主題,其 Amazon Resource Names (ARNs) 會顯示在定義中,顯示為 "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction""targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction"

如需詳細資訊,請參閱 Amazon Simple Notification Service 開發人員指南,以及更具體而言,Amazon Simple Notification Service API參考 中的CreateTopic操作文件。

此範例也假設您已建立具有適當許可的 AWS Identity and Access Management (IAM) 角色。此角色ARN的 會在偵測器模型定義中顯示為 "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole"。請依照 中的步驟設定 的許可 AWS IoT Events建立此角色,並在偵測器模型定義的適當位置複製角色ARN的 。

您可以使用下列 AWS CLI 命令建立偵測器模型。

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" }