使用 HVAC 系统的探测器模型定义 AWS IoT Events - AWS IoT Events

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 HVAC 系统的探测器模型定义 AWS IoT Events

areaDetectorModel 定义了每个探测器实例的工作方式。每个 state machine 实例都将摄取温度传感器读数,然后根据这些读数更改状态并发送控制消息。

使用的 CLI 命令:

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

文件:areaDetectorModel.json

{ "detectorModelName": "areaDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "start", "onEnter": { "events": [ { "eventName": "prepare", "condition": "true", "actions": [ { "setVariable": { "variableName": "sensorId", "value": "0" } }, { "setVariable": { "variableName": "reportedTemperature", "value": "0.1" } }, { "setVariable": { "variableName": "resetMe", "value": "false" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "initialize", "condition": "$input.seedTemperatureInput.sensorCount > 0", "actions": [ { "setVariable": { "variableName": "rangeHigh", "value": "$input.seedTemperatureInput.rangeHigh" } }, { "setVariable": { "variableName": "rangeLow", "value": "$input.seedTemperatureInput.rangeLow" } }, { "setVariable": { "variableName": "desiredTemperature", "value": "$input.seedTemperatureInput.desiredTemperature" } }, { "setVariable": { "variableName": "averageTemperature", "value": "$input.seedTemperatureInput.desiredTemperature" } }, { "setVariable": { "variableName": "allowedError", "value": "$input.seedTemperatureInput.allowedError" } }, { "setVariable": { "variableName": "anomalousHigh", "value": "$input.seedTemperatureInput.anomalousHigh" } }, { "setVariable": { "variableName": "anomalousLow", "value": "$input.seedTemperatureInput.anomalousLow" } }, { "setVariable": { "variableName": "sensorCount", "value": "$input.seedTemperatureInput.sensorCount" } }, { "setVariable": { "variableName": "noDelay", "value": "$input.seedTemperatureInput.noDelay == true" } } ], "nextState": "idle" }, { "eventName": "reset", "condition": "($variable.resetMe == true) && ($input.temperatureInput.sensorData.temperature < $variable.anomalousHigh && $input.temperatureInput.sensorData.temperature > $variable.anomalousLow)", "actions": [ { "setVariable": { "variableName": "averageTemperature", "value": "((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount)" } } ], "nextState": "idle" } ] }, "onExit": { "events": [ { "eventName": "resetHeatCool", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOff" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOff" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/Off" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/Off" } } ] } ] } }, { "stateName": "idle", "onInput": { "events": [ { "eventName": "whatWasInput", "condition": "true", "actions": [ { "setVariable": { "variableName": "sensorId", "value": "$input.temperatureInput.sensorId" } }, { "setVariable": { "variableName": "reportedTemperature", "value": "$input.temperatureInput.sensorData.temperature" } } ] }, { "eventName": "changeDesired", "condition": "$input.seedTemperatureInput.desiredTemperature != $variable.desiredTemperature", "actions": [ { "setVariable": { "variableName": "desiredTemperature", "value": "$input.seedTemperatureInput.desiredTemperature" } } ] }, { "eventName": "calculateAverage", "condition": "$input.temperatureInput.sensorData.temperature < $variable.anomalousHigh && $input.temperatureInput.sensorData.temperature > $variable.anomalousLow", "actions": [ { "setVariable": { "variableName": "averageTemperature", "value": "((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount)" } } ] } ], "transitionEvents": [ { "eventName": "anomalousInputArrived", "condition": "$input.temperatureInput.sensorData.temperature >= $variable.anomalousHigh || $input.temperatureInput.sensorData.temperature <= $variable.anomalousLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/anomaly" } } ], "nextState": "idle" }, { "eventName": "highTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature > $variable.rangeHigh", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "cooling" }, { "eventName": "lowTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature < $variable.rangeLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "heating" }, { "eventName": "highTemperatureThreshold", "condition": "(((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount) > ($variable.desiredTemperature + $variable.allowedError))", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "cooling" }, { "eventName": "lowTemperatureThreshold", "condition": "(((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount) < ($variable.desiredTemperature - $variable.allowedError))", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "heating" } ] } }, { "stateName": "cooling", "onEnter": { "events": [ { "eventName": "delay", "condition": "!$variable.noDelay && $variable.enteringNewState", "actions": [ { "setTimer": { "timerName": "coolingTimer", "seconds": 180 } }, { "setVariable": { "variableName": "goodToGo", "value": "false" } } ] }, { "eventName": "dontDelay", "condition": "$variable.noDelay == true", "actions": [ { "setVariable": { "variableName": "goodToGo", "value": "true" } } ] }, { "eventName": "beenHere", "condition": "true", "actions": [ { "setVariable": { "variableName": "enteringNewState", "value": "false" } } ] } ] }, "onInput": { "events": [ { "eventName": "whatWasInput", "condition": "true", "actions": [ { "setVariable": { "variableName": "sensorId", "value": "$input.temperatureInput.sensorId" } }, { "setVariable": { "variableName": "reportedTemperature", "value": "$input.temperatureInput.sensorData.temperature" } } ] }, { "eventName": "changeDesired", "condition": "$input.seedTemperatureInput.desiredTemperature != $variable.desiredTemperature", "actions": [ { "setVariable": { "variableName": "desiredTemperature", "value": "$input.seedTemperatureInput.desiredTemperature" } } ] }, { "eventName": "calculateAverage", "condition": "$input.temperatureInput.sensorData.temperature < $variable.anomalousHigh && $input.temperatureInput.sensorData.temperature > $variable.anomalousLow", "actions": [ { "setVariable": { "variableName": "averageTemperature", "value": "((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount)" } } ] }, { "eventName": "areWeThereYet", "condition": "(timeout(\"coolingTimer\"))", "actions": [ { "setVariable": { "variableName": "goodToGo", "value": "true" } } ] } ], "transitionEvents": [ { "eventName": "anomalousInputArrived", "condition": "$input.temperatureInput.sensorData.temperature >= $variable.anomalousHigh || $input.temperatureInput.sensorData.temperature <= $variable.anomalousLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/anomaly" } } ], "nextState": "cooling" }, { "eventName": "highTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature > $variable.rangeHigh", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } } ], "nextState": "cooling" }, { "eventName": "lowTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature < $variable.rangeLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOff" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/Off" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "heating" }, { "eventName": "desiredTemperature", "condition": "(((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount) <= ($variable.desiredTemperature - $variable.allowedError)) && $variable.goodToGo == true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOff" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/Off" } } ], "nextState": "idle" } ] } }, { "stateName": "heating", "onEnter": { "events": [ { "eventName": "delay", "condition": "!$variable.noDelay && $variable.enteringNewState", "actions": [ { "setTimer": { "timerName": "heatingTimer", "seconds": 120 } }, { "setVariable": { "variableName": "goodToGo", "value": "false" } } ] }, { "eventName": "dontDelay", "condition": "$variable.noDelay == true", "actions": [ { "setVariable": { "variableName": "goodToGo", "value": "true" } } ] }, { "eventName": "beenHere", "condition": "true", "actions": [ { "setVariable": { "variableName": "enteringNewState", "value": "false" } } ] } ] }, "onInput": { "events": [ { "eventName": "whatWasInput", "condition": "true", "actions": [ { "setVariable": { "variableName": "sensorId", "value": "$input.temperatureInput.sensorId" } }, { "setVariable": { "variableName": "reportedTemperature", "value": "$input.temperatureInput.sensorData.temperature" } } ] }, { "eventName": "changeDesired", "condition": "$input.seedTemperatureInput.desiredTemperature != $variable.desiredTemperature", "actions": [ { "setVariable": { "variableName": "desiredTemperature", "value": "$input.seedTemperatureInput.desiredTemperature" } } ] }, { "eventName": "calculateAverage", "condition": "$input.temperatureInput.sensorData.temperature < $variable.anomalousHigh && $input.temperatureInput.sensorData.temperature > $variable.anomalousLow", "actions": [ { "setVariable": { "variableName": "averageTemperature", "value": "((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount)" } } ] }, { "eventName": "areWeThereYet", "condition": "(timeout(\"heatingTimer\"))", "actions": [ { "setVariable": { "variableName": "goodToGo", "value": "true" } } ] } ], "transitionEvents": [ { "eventName": "anomalousInputArrived", "condition": "$input.temperatureInput.sensorData.temperature >= $variable.anomalousHigh || $input.temperatureInput.sensorData.temperature <= $variable.anomalousLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/anomaly" } } ], "nextState": "heating" }, { "eventName": "highTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature > $variable.rangeHigh", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOff" } }, { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:coolOn" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/Off" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Cooling/On" } }, { "setVariable": { "variableName": "enteringNewState", "value": "true" } } ], "nextState": "cooling" }, { "eventName": "lowTemperatureSpike", "condition": "$input.temperatureInput.sensorData.temperature < $variable.rangeLow", "actions": [ { "iotTopicPublish": { "mqttTopic": "temperatureSensor/spike" } } ], "nextState": "heating" }, { "eventName": "desiredTemperature", "condition": "(((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount) >= ($variable.desiredTemperature + $variable.allowedError)) && $variable.goodToGo == true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:heatOff" } }, { "iotTopicPublish": { "mqttTopic": "hvac/Heating/Off" } } ], "nextState": "idle" } ] } } ], "initialStateName": "start" }, "key": "areaId", "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }

响应:

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