本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
起重机
后台故事
许多起重机的操作员都希望检测机器何时需要维护或更换,并触发相应的通知。每台起重机都有马达。马达发出包含压力和温度信息的消息(输入)。操作员需要两个级别的事件探测器:
-
起重机级别的事件探测器
-
马达级别的事件探测器
使用来自马达的消息(其中包含 "craneId"
和 "motorId"
的元数据),操作员可以使用适当的路由执行两个级别的事件探测器。当满足事件条件时,应向相应的 Amazon SNS 主题发送通知。操作员可以配置探测器模型,这样就不会发出重复的通知。
该示例演示以下功能:
-
创建、读取、更新、删除 (CRUD) 输入。
-
创建、读取、更新、删除 (CRUD) 事件探测器模型和不同版本的事件探测器。
-
将一个输入路由到多个事件探测器。
-
将输入摄入探测器模型。
-
评估触发条件和生命周期事件。
-
能够在条件中引用状态变量并根据条件设置其值。
-
包含定义、状态、触发器评估器和操作执行器的运行时系统编排。
-
使用 SNS 目标在
ActionsExecutor
中执行操作。
命令
#Create Pressure Input aws iotevents create-input --cli-input-json file://pressureInput.json aws iotevents describe-input --input-name PressureInput aws iotevents update-input --cli-input-json file://pressureInput.json aws iotevents list-inputs aws iotevents delete-input --input-name PressureInput #Create Temperature Input aws iotevents create-input --cli-input-json file://temperatureInput.json aws iotevents describe-input --input-name TemperatureInput aws iotevents update-input --cli-input-json file://temperatureInput.json aws iotevents list-inputs aws iotevents delete-input --input-name TemperatureInput #Create Motor Event Detector using pressure and temperature input aws iotevents create-detector-model --cli-input-json file://motorDetectorModel.json aws iotevents describe-detector-model --detector-model-name motorDetectorModel aws iotevents update-detector-model --cli-input-json file://updateMotorDetectorModel.json aws iotevents list-detector-models aws iotevents list-detector-model-versions --detector-model-name motorDetectorModel aws iotevents delete-detector-model --detector-model-name motorDetectorModel #Create Crane Event Detector using temperature input aws iotevents create-detector-model --cli-input-json file://craneDetectorModel.json aws iotevents describe-detector-model --detector-model-name craneDetectorModel aws iotevents update-detector-model --cli-input-json file://updateCraneDetectorModel.json aws iotevents list-detector-models aws iotevents list-detector-model-versions --detector-model-name craneDetectorModel aws iotevents delete-detector-model --detector-model-name craneDetectorModel #Replace craneIds sed -i '' "s/100008/100009/g" messages/* #Replace motorIds sed -i '' "s/200008/200009/g" messages/* #Send HighPressure message aws iotevents-data batch-put-message --cli-input-json file://messages/highPressureMessage.json --cli-binary-format raw-in-base64-out #Send HighTemperature message aws iotevents-data batch-put-message --cli-input-json file://messages/highTemperatureMessage.json --cli-binary-format raw-in-base64-out #Send LowPressure message aws iotevents-data batch-put-message --cli-input-json file://messages/lowPressureMessage.json --cli-binary-format raw-in-base64-out #Send LowTemperature message aws iotevents-data batch-put-message --cli-input-json file://messages/lowTemperatureMessage.json --cli-binary-format raw-in-base64-out
探测器模型数
文件:craneDetectorModel.json
{ "detectorModelName": "craneDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Running", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overheated", "condition": "$input.TemperatureInput.temperature > 35", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "$variable.craneThresholdBreached + 1" } } ] }, { "eventName": "Crane Threshold Breached", "condition": "$variable.craneThresholdBreached > 5", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:CraneSNSTopic" } } ] }, { "eventName": "Underheated", "condition": "$input.TemperatureInput.temperature < 25", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "0" } } ] } ] } } ], "initialStateName": "Running" }, "key": "craneid", "roleArn": "arn:aws:iam::123456789012:role/columboSNSRole" }
更新现有的探测器模型。文件:updateCraneDetectorModel.json
{ "detectorModelName": "craneDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Running", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "0" } }, { "setVariable": { "variableName": "alarmRaised", "value": "'false'" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overheated", "condition": "$input.TemperatureInput.temperature > 30", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "$variable.craneThresholdBreached + 1" } } ] }, { "eventName": "Crane Threshold Breached", "condition": "$variable.craneThresholdBreached > 5 && $variable.alarmRaised == 'false'", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:CraneSNSTopic" } }, { "setVariable": { "variableName": "alarmRaised", "value": "'true'" } } ] }, { "eventName": "Underheated", "condition": "$input.TemperatureInput.temperature < 10", "actions": [ { "setVariable": { "variableName": "craneThresholdBreached", "value": "0" } } ] } ] } } ], "initialStateName": "Running" }, "roleArn": "arn:aws:iam::123456789012:role/columboSNSRole" }
文件:motorDetectorModel.json
{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Running", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "motorThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overheated And Overpressurized", "condition": "$input.PressureInput.pressure > 70 && $input.TemperatureInput.temperature > 30", "actions": [ { "setVariable": { "variableName": "motorThresholdBreached", "value": "$variable.motorThresholdBreached + 1" } } ] }, { "eventName": "Motor Threshold Breached", "condition": "$variable.motorThresholdBreached > 5", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:MotorSNSTopic" } } ] } ] } } ], "initialStateName": "Running" }, "key": "motorid", "roleArn": "arn:aws:iam::123456789012:role/columboSNSRole" }
更新现有的探测器模型。文件:updateMotorDetectorModel.json
{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Running", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "motorThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overheated And Overpressurized", "condition": "$input.PressureInput.pressure > 70 && $input.TemperatureInput.temperature > 30", "actions": [ { "setVariable": { "variableName": "motorThresholdBreached", "value": "$variable.motorThresholdBreached + 1" } } ] }, { "eventName": "Motor Threshold Breached", "condition": "$variable.motorThresholdBreached > 5", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:MotorSNSTopic" } } ] } ] } } ], "initialStateName": "Running" }, "roleArn": "arn:aws:iam::123456789012:role/columboSNSRole" }
输入
文件:pressureInput.json
{ "inputName": "PressureInput", "inputDescription": "this is a pressure input description", "inputDefinition": { "attributes": [ {"jsonPath": "pressure"} ] } }
文件:temperatureInput.json
{ "inputName": "TemperatureInput", "inputDescription": "this is temperature input description", "inputDefinition": { "attributes": [ {"jsonPath": "temperature"} ] } }
消息
文件:highPressureMessage.json
{ "messages": [ { "messageId": "1", "inputName": "PressureInput", "payload": "{\"craneid\": \"100009\", \"pressure\": 80, \"motorid\": \"200009\"}" } ] }
文件:highTemperatureMessage.json
{ "messages": [ { "messageId": "2", "inputName": "TemperatureInput", "payload": "{\"craneid\": \"100009\", \"temperature\": 40, \"motorid\": \"200009\"}" } ] }
文件:lowPressureMessage.json
{ "messages": [ { "messageId": "1", "inputName": "PressureInput", "payload": "{\"craneid\": \"100009\", \"pressure\": 20, \"motorid\": \"200009\"}" } ] }
文件:lowTemperatureMessage.json
{ "messages": [ { "messageId": "2", "inputName": "TemperatureInput", "payload": "{\"craneid\": \"100009\", \"temperature\": 20, \"motorid\": \"200009\"}" } ] }