【发布时间】:2019-11-05 16:48:19
【问题描述】:
我刚开始使用 AWS IoT。我创建了一个东西并使用 mqtt-spy 连接到 AWS 服务器。一切正常。
现在我想在 web 控制台中检查每件事情的状态,但是我在设备附近找不到这样有用的信息。
【问题讨论】:
标签: amazon-web-services iot aws-iot
我刚开始使用 AWS IoT。我创建了一个东西并使用 mqtt-spy 连接到 AWS 服务器。一切正常。
现在我想在 web 控制台中检查每件事情的状态,但是我在设备附近找不到这样有用的信息。
【问题讨论】:
标签: amazon-web-services iot aws-iot
通过启用AWS IoT Fleet Indexing Service,您可以获得事物的连接状态。此外,您可以查询当前连接/断开的设备。
首先,您必须通过 aws-cli 或通过控制台启用索引 (thingConnectivityIndexingMode)。
aws iot update-indexing-configuration --thing-indexing-configuration thingIndexingMode=REGISTRY_AND_SHADOW,thingConnectivityIndexingMode=STATUS
然后你可以像下面这样查询一个事物的连接状态
aws iot search-index --index-name "AWS_Things" --query-string "thingName:mything1"
{
"things":[{
"thingName":"mything1",
"thingGroupNames":[
"mygroup1"
],
"thingId":"a4b9f759-b0f2-4857-8a4b-967745ed9f4e",
"attributes":{
"attribute1":"abc"
},
"connectivity": {
"connected":false,
"timestamp":1641508937
}
}
}
注意:Fleet Indexing Service 使用设备生命周期事件 ($aws/events/presence/connected/) 索引连接数据。在某些情况下,发生连接或断开连接事件后,服务可能需要一分钟左右的时间来更新索引。
编辑:这个的 javascript 版本:
var iot = new AWS.Iot({
apiVersion: "2015-05-28"
});
...
var params = {
queryString: "thingName:" + data.Item.thingName, // using result from DynamoDB
indexName: 'AWS_Things'
// maxResults: 'NUMBER_VALUE',
// nextToken: 'STRING_VALUE',
// queryVersion: 'STRING_VALUE'
};
iot.searchIndex(params, function(err, data) {
if (err) {
console.log("error from iot.searchIndex");
console.log(err, err.stack); // an error occurred
} else {
console.log("success from iot.searchIndex");
console.log(data.things[0].connectivity.connected); // t/f
}
});
【讨论】:
您需要在 aws iot 控制台订阅主题,AWS IoT-core 右上角的测试部分。例如,您订阅此主题将您的客户端替换为 .
$aws/events/presence/connected/<Your_clientId>
如果您有不止一件事,那么您必须使用您的 ClientID 订阅
供参考检查此链接https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html
【讨论】: