本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在中提取 MQTT 消息 AWS IoT Events
如果您的传感器计算资源无法使用 "BatchPutMessage"
API,但可以使用轻量级 MQTT 客户端将其数据发送到 AWS IoT Core 消息代理,则可以创建 AWS IoT Core 主题规则以将消息数据重定向到 AWS IoT Events 输入。以下是 AWS IoT Events 主题规则的定义,该规则采用 MQTT 主题中的"areaId"
和"sensorId"
输入字段,以及消息负载"sensorData.temperature"
"temp"
字段中的字段,并将这些数据提取到我们的。 AWS IoT Events "temperatureInput"
CLI 命令:
aws iot create-topic-rule --cli-input-json file://temperatureTopicRule.json
seedSetDesiredTemp.json
文件:
{ "ruleName": "temperatureTopicRule", "topicRulePayload": { "sql": "SELECT topic(3) as areaId, topic(4) as sensorId, temp as sensorData.temperature FROM 'update/temperature/#'", "description": "Ingest temperature sensor messages into IoT Events", "actions": [ { "iotEvents": { "inputName": "temperatureInput", "roleArn": "arn:aws:iam::123456789012:role/service-role/anotheRole" } } ], "ruleDisabled": false, "awsIotSqlVersion": "2016-03-23" } }
响应:[无]
如果传感器通过以下有效负载发送了一条有关主题"update/temperature/Area51/03"
的消息。
{ "temp": 24.5 }
这会导致数据被摄取,就好 AWS IoT Events 像进行了以下 "BatchPutMessage"
API 调用一样。
aws iotevents-data batch-put-message --cli-input-json file://spoofExample.json --cli-binary-format raw-in-base64-out
spoofExample.json
文件:
{ "messages": [ { "messageId": "54321", "inputName": "temperatureInput", "payload": "{\"sensorId\": \"03\", \"areaId\": \"Area51\", \"sensorData\": {\"temperature\": 24.5} }" } ] }