【发布时间】: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, ...)。
有没有人有一个与命名阴影交互的工作示例?
【问题讨论】: