创建用于表示设备状态的探测器模型 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 消息,表示已全部清除。

此示例检测器模型假设您创建了两个亚马逊SNS主题,其亚马逊资源名称 (ARNs) 在定义中显示为"targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction""targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction"

有关更多信息,请参阅《亚马逊简单通知服务开发者指南》,更具体地说,请参阅《亚马逊简单通知服务API参考》中的CreateTopic操作文档。

此示例还假设您已创建具有相应权限的 AWS Identity and Access Management (IAM) 角色。这个角色在探测器模型定义中显示为"roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole"。ARN按照中的步骤创建为设置权限 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" }