AWS IoT规则SQL重新发布将物体名称作为JSON对象键

0

【以下的问题经过翻译处理】 我正在从多台iot设备发布Shadow更新。我想过滤这些更新,并提取一些数据,这些数据会更新另一个Shadow(主Shadow),该主Shadow具有Json对象列表。每个Json对象键是设备id。

为此,我有一个规则,订阅到$aws/things/+/shadow/update/documents以捕获所有设备的Shadow更新。因此,例如以下更新

到$aws/things/device101/shadow/update/documents

{
 state: {
    reported: {
      "temperature": "28"
    }
 }
}

到$aws/things/device202/shadow/update/documents

{
 state: {
    reported: {
      "temperature": "31"
    }
 }

主Shadow将导致

{
 state: {
    reported: {
      "device101": {
         "temperature": "28"
      }
      "device202": {
         "temperature": "31"
      }    
    }
 }

在我的SQL中,我仍然找不到将设备id用作键来发布到主Shadow的方法。

我使用topic(3)作为函数获取设备名称和文字,以构建json,但它一直告诉我需要有一个键。我尝试转换topic(3)函数,但结果相同。

SELECT { topic(3): {'temperature': current.state.reported.temperature }} as state.reported FROM '$aws/things/+/shadow/update/documents'


SqlParseException
Expected a key, but got IDENT(topic). Object literals should follow the format {"keyName": any-expression, "anotherKey": any-expression} topic(3): 


有什么想法可以实现吗?

谢谢

profile picture
专家
已提问 5 个月前8 查看次数
1 回答
0

【以下的回答经过翻译处理】 你尝试的方法目前在 AWS IoT Rules 的 SQL 语言中不支持。这种方法的另一个问题是,设备影子文档的大小有限,因此,根据每个设备报告的设备或属性数量,迟早会达到此限制。

相反,你可以使用命名影子和 Fleet Indexing 结合使用。你可以将每个单独的设备作为主影子文档的命名影子添加,而不是将它们作为主影子文档的一部分添加。

如果你使用 Fleet Indexing 搜索主影子,则在 SearchIndex 调用的响应中,还将获得所有与主影子相关联的命名影子。类似于以下内容,其中命名影子包含在 name 属性中:

{
    "desired": {
        "welcome": "aws-iot"
    },
    "reported": {
        "welcome": "aws-iot"
    },
    "metadata": {
        "desired": {
            "welcome": {
                "timestamp": 1632389871
            }
        },
        "reported": {
            "welcome": {
                "timestamp": 1632389871
            }
        }
    },
    "hasDelta": false,
    "version": 15,
    "name": {
        "connection_info": {
            "reported": {
                "lastConnected": 1663695514109,
                "ipAddress": "172.31.33.131",
                "lastDisconnected": 1663695513922,
                "disconnectReason": "SERVER_INITIATED_DISCONNECT"
            },
            "metadata": {
                "reported": {
                    "lastConnected": 1663695514109,
                    "ipAddress": "172.31.33.131",
                    "lastDisconnected": 1663695513922,
                    "disconnectReason": "SERVER_INITIATED_DISCONNECT"
                }
            },
            "hasDelta": false,
            "version": 472
        }
    }
}

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则