【问题标题】:AWS IoT NodeJS SDK cannot update named shadowsAWS IoT NodeJS SDK 无法更新命名阴影
【发布时间】:2020-10-30 13:50:26
【问题描述】:

试图总结一下 AWS IoT 命名阴影,但我无法订阅或更新命名阴影,只能订阅默认阴影。 在我的 IoT 控制台中,我有 Classic Shadow 和一个名为 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);
});

上面的代码工作正常。我可以使用事物名称 (mySimulatedThing) 影响 Classic Shadow,在 AWS IoT 控制台中查看所需值是如何修改的,如果我从 AWS 控制台更改 json 所需值,我可以看到 NodeJS 事件状态和被调用的增量。

但我找不到与命名阴影“simShadow1”交互的方法。我尝试将事物名称设置为“mySimulatedThing/simShadow1”、“mySimulatedThing/shadow/name/simShadow1”和许多其他组合。我也尝试过thinkShadow.register("with the thing name"...),然后在更新和订阅这样的thinkShadow.update(thingName+"/"+ shadowName, {...})和thinkShadow时添加命名shadow .subscribe(thingName+"/"+ shadowName, ...)。

有没有人有一个与命名阴影交互的工作示例?

【问题讨论】:

    标签: node.js shadow aws-iot


    【解决方案1】:

    显然我必须将完整路径设置为:

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

    我没想到会设置完整路径。我想知道是否有更优雅的解决方案。

    【讨论】:

      【解决方案2】:

      我处于类似情况 - 我可以使用 GG(v1) 和 IoT Cloud 中的东西进行更新。但无法使用本地影子服务影响设备上的状态更改 - 即当我让本地影子服务成为主题而不是事物的订阅者时。我仍在试图弄清楚为什么.....

      【讨论】:

      • thingShadow.register 和 update 函数怎么样 - 你必须指定整个主题路径吗?
      猜你喜欢
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 2019-01-20
      • 2017-07-15
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多