简体   繁体   中英

AWS IoT NodeJS SDK cannot update named shadows

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 ?

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.

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 .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM