AWS IoT Events 使用示例 AWS CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

AWS IoT Events 使用示例 AWS CLI

以下代码示例向您展示了如何使用with来执行操作和实现常见场景 AWS IoT Events。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 batch-put-message

AWS CLI

向 AWS IoT Events 发送消息(输入)

以下batch-put-message示例向 AWS IoT Events 系统发送了一组消息。每个消息有效载荷都将转换为您指定的输入 (inputName),并输入到任何监视该输入的检测器中。如果发送了多条消息,则无法保证消息的处理顺序。为了保证顺序,您必须逐一发送一条消息,然后等待成功响应。

aws iotevents-data batch-put-message \ --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 API 参考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 API 参考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 API 参考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 API 参考CreateInput中的。

以下代码示例演示如何使用 delete-detector-model

AWS CLI

删除探测器模型

以下delete-detector-model示例删除了指定的探测器模型。探测器模型的所有活动实例也将被删除。

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

此命令不生成任何输出。

有关更多信息,请参阅 AWS IoT Events API 参考DeleteDetectorModel中的。

以下代码示例演示如何使用 delete-input

AWS CLI

删除输入

以下delete-input示例删除了指定的输入。

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

此命令不生成任何输出。

有关更多信息,请参阅 AWS IoT Events API 参考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 API 参考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 API 参考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 API 参考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 API 参考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 API 参考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 API 参考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 API 参考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 API 参考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 API 参考ListTagsForResource中的。

以下代码示例演示如何使用 put-logging-options

AWS CLI

设置日志选项

以下put-logging-options示例设置或更新 AWS IoT Events 日志选项。如果您更新任何loggingOptions` field, it can take up to one minute for the change to take effect. Also, if you change the policy attached to the role you specified in the ``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 API 参考PutLoggingOptions中的。

以下代码示例演示如何使用 tag-resource

AWS CLI

为资源添加标签

以下tag-resource示例添加或修改(如果密钥deviceType已存在)附加到指定资源的标签。

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 API 参考TagResource中的。

以下代码示例演示如何使用 untag-resource

AWS CLI

要从资源中删除标签

以下untag-resource示例从指定资源中删除具有指定密钥名称的标签。

aws iotevents untag-resource \ --resource-arn arn:aws:iotevents:us-west-2:123456789012:input/PressureInput \ --tagkeys deviceType

此命令不生成任何输出。

有关更多信息,请参阅 AWS IoT Events API 参考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 API 参考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 API 参考UpdateInput中的。