0
votes

Trying to wrap-up my head around AWS IoT named shadows but I cannot subscribe or update to named shadows but only default shadow. In my IoT Console I have the Classic Shadow and an named shadow called simShadow1

const awsIoT = require('aws-iot-device-sdk');
const pathToCerts = "./certs";

const thingName = "mySimulatedThing";
const shadowName = "simShadow1";

const options = {
  keyPath:`${pathToCerts }/xxxx-private.pem.key`,
  certPath:`${pathToCerts }/xxxx-certificate.pem.crt`,
  caPath:`${pathToCerts }/rootCa.pem`,
  clientId:"xxxx",
  host:"xxxx-ats.iot.eu-west-1.amazonaws.com", 
  debug:true
};

let clientTokenUpdate;

const thinkShadow = awsIoT.thingShadow(options);
thinkShadow.on("connect", ()=>{
  console.info("connected to shadow");

  thinkShadow.register( thingName, {}, ()=>{
    console.debug(`registered to ${thingName} thing`);
    console.warn(`about to perform an update on ${thingName}`);

    clientTokenUpdate = thinkShadow.update( thingName, {
      state:{
        desired:{
          welcome:"Hello new value",
          ts:Date.now()
        }
      }
    });

    thinkShadow.subscribe(thingName, {}, (error, result)=>{
      console.info("simShadow1 subscription callback", error, result);
    });

    if(clientTokenUpdate === null){
      console.warn('update shadow failed, operation still in progress');
    }
  });

});

thinkShadow.on('status', (thingName, stat, clientToken, stateObject)=>{
  console.debug("on status", thingName, stat, clientToken, stateObject);
});

thinkShadow.on('delta', (thingName, stateObject)=>{
  console.debug("on delta", thingName, stateObject);
});

thinkShadow.on('timeout', (thingName, clientToken)=>{
  console.debug("on timeout", thingName, clientToken);
});

The above code works fine. I can affect the Classic Shadow using the thing name (mySimulatedThing), see in the AWS IoT Console how the desired values are modified and if I change the json desired value from the AWS Console I can see the NodeJS events status and delta being called.

But I cannot find a way to interact with the named shadow "simShadow1". I have tried to set the thing name to "mySimulatedThing/simShadow1", "mySimulatedThing/shadow/name/simShadow1" and many other combinations. I also have tried to thinkShadow.register("with the thing name"...), and then add the named shadow when updating and subscribing such thinkShadow.update(thingName+"/"+ shadowName, {...}) and thinkShadow.subscribe(thingName+"/"+ shadowName, ...).

Does anyone have a working example for interacting with named shadows ?

2

2 Answers

0
votes

Apparently I have to set the full path as:

     device.subscribe(`$aws/things/${thingName}/shadow/name/${shadowName}/update/accepted`, (error, result)=>{
         console.info(">>", error, result);
     });

I wouldn't have expected to set the full path. I wonder if there are more more elegant solutions.

0
votes

I am in a similar situation - I am able to update using thing in GG(v1) and IoT Cloud. But unable to affect state change on the device using local shadow service - ie when i make the local shadow service the subscriber of the topic instead of the thing. I am still trying to figure out why .....