how to get the state of all shadows, classic and named ?

0

I have a thing with a classic shadow and several named shadows. I don't know the name of the named shadows. they are created by the devices with a uuid the moment the device connects to IoT Core I need to get in a single document the current state of all the shadows of the thing. one json that includes the classic shadow current state together with the current state of all the named shadows

Is it possible to do this with one single API call? using IoT core and/or other services like fleet indexing ?

Currently I'm using the API to fetch all the named shadows of the thing and then fetch each shadow document per named shadow. But maybe there is a better option.

thanks

mvp
asked a year ago358 views
1 Answer
0

Hi mvp, you are right, the only way to get the list of named shadows for a Thing is to call the ListNamedShadowsForThing API and fetch the classic shadow and named shadow individually by calling GetThingShadow

Ideally the named shadow name should be known by your backend, do you have a special reason why you use a UUID as the named shadow name? If your backend could guess the name, you could avoid listing the shadow for each Thing and fetch the shadow directly.

Using predefined names for your named shadows also allows you to use AWS IoT AWS IoT Fleet Indexing. To enable Fleet Indexing on named Shadow you need to specify the list of named shadow names. Once done, you can query the Index and retrieve multiple shadows at the same time using a Lucene based query syntax. In the example below I retrieve all Things and corresponding shadows documents that have a named shadow Home01 and an attribute welcome in the reported section:

> aws iot search-index --query-string "shadow.name.Home01.reported.welcome: *"
{
    "things": [
        {
            "thingName": "ShadowTest01",
            "thingId": "2254b6c2-6f86-4d32-898b-9335a941882a",
            "shadow": "{\"name\":{\"Home01\":{\"desired\":{\"welcome\":\"aws-iot\"},\"reported\":{\"welcome\":\"aws-iot\"},\"metadata\":{\"desired\":{\"welcome\":\"aws-iot\"},\"reported\":{\"welcome\":\"aws-iot\"}},\"hasDelta\":false,\"version\":1}}}",
            "connectivity": {
                "connected": false
            }
        },
        {
            "thingName": "ShadowTest02",
            "thingId": "28295d73-7790-40dc-825f-16bfe7cef6a2",
            "shadow": "{\"name\":{\"Home01\":{\"reported\":{\"welcome\":\"Second device!\"},\"metadata\":{\"reported\":{\"welcome\":\"Second device!\"}},\"hasDelta\":false,\"version\":3}}}",
            "connectivity": {
                "connected": false
            }
        }
    ]
}
profile pictureAWS
EXPERT
Jan_B
answered a year ago
  • Thanks @Jan_B for your answer.

    Each of the devices (things) is a gateway to which several wireless sensors are connected. A gateway doesn't have a fixed number of sensors. it can have one or many (max 20 sensors).

    When a sensor connects to the gateway it publishes its data to a named shadow using it's UUID I wanted to keep an array of the connected sensors in the shadow of the gateway but two problems with that: 1. classic shadow size is limited. 2. Arrays in shadows are supported as a full value. So it would push me to have a track of all the list of the wireless sensors somewhere else in order to update the array. Which increase complexity.

    Then the other option is to create a new thing when a sensor connects to the gateway and publish sensor data to its own classic shadow, but again it increases complexity. e.g. How can I get the current state of all the wireless sensors for a particular gateway in a JSON document ? I will have to create a group, add each device to the group, fix the index, ect. Also whenever a mqtt rule receives the message that a new sensor is connected to the gateway it triggers a lambda that creates the thing. too much stuff.

    Let me know if you have any ideas on how to simplify this architecture.

    thanks

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions