我正在尝试使用aws-iot-device-sdk-js
和node.js解决一个简单的任务。我想订阅对影子所做的所有更新,以及它们的一些更新。我可以使用带有证书的device
类来完成此操作,但这在此演示中不可用。
这是我的代码:
var AWS = require('aws-sdk');
var AWSIoTData = require('aws-iot-device-sdk');
var thingName = 'MyThing';
var shadowsRegistered = false;
var AWSConfiguration = {
poolId: 'us-east-2:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', // 'YourCognitoIdentityPoolId'
host: 'XXXXXXXXXXXXXX-ats.iot.us-east-2.amazonaws.com', // 'YourAWSIoTEndpoint', e.g. 'prefix.iot.us-east-1.amazonaws.com'
region: 'us-east-2' // 'YourAwsRegion', e.g. 'us-east-1'
};
//
// Initialize our configuration.
//
AWS.config.region = AWSConfiguration.region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: AWSConfiguration.poolId
});
var shadowsRegistered = false;
iniShadows = function(credentials){
const shadows = AWSIoTData.thingShadow({
region: AWS.config.region, // Set the AWS region we will operate in.
host:AWSConfiguration.host, //Set the AWS IoT Host Endpoint
clientId: 'some random string', // Use a random client ID.
protocol: 'wss', // Connect via secure WebSocket
//
// Set the maximum reconnect time to 8 seconds; this is a browser application
// so we don't want to leave the user waiting too long for reconnection after
// re-connecting to the network/re-opening their laptop/etc...
//
maximumReconnectTimeMs: 8000,
debug: true, // Enable console debugging information (optional)
//
// IMPORTANT: the AWS access key ID, secret key, and sesion token must be
// initialized with empty strings.
//
accessKeyId: credentials.AccessKeyId,
secretKey: credentials.SecretKey,
sessionToken: credentials.SessionToken
});
shadows.on('connect', function(){
console.log('connect');
//
// We only register our shadows once.
//
if (!shadowsRegistered) {
console.log('=== registering shadows ');
shadows.register(thingName,{
persistentSubscribe:true
},function(){
console.log('after registerging. ');
shadowsRegistered = true;
});
}
});
shadows.on('reconnect', function(){
console.log('reconnect');
});
shadows.on('status', function(thingName, statusType, clientToken, stateObject) {
console.log(thingName);
console.log('----> got status ' + statusType);
console.log(JSON.stringify(stateObject));
});
shadows.on('delta', function(thingName, stateObject) {
console.log(thingName);
console.log(JSON.stringify(stateObject));
});
shadows.on('timeout', function(thingName, clientToken) {
console.log('received timeout on '+thingName);
});
}
var cognitoIdentity = new AWS.CognitoIdentity();
AWS.config.credentials.get(function(err, data) {
if (!err) {
console.log('retrieved identity: ' + AWS.config.credentials.identityId);
var params = {
IdentityId: AWS.config.credentials.identityId
};
cognitoIdentity.getCredentialsForIdentity(params, function(err, data) {
if (!err) {
//
// Update our latest AWS credentials; the MQTT client will use these
// during its next reconnect attempt.
//
//shadows.updateWebSocketCredentials(data.Credentials.AccessKeyId,
// data.Credentials.SecretKey,
/// data.Credentials.SessionToken);
iniShadows(data.Credentials);
console.log('updated credentials in shadows ');
} else {
console.log('error retrieving credentials: ' + err);
alert('error retrieving credentials: ' + err);
}
});
} else {
console.log('error retrieving identity:' + err);
alert('error retrieving identity: ' + err);
}
});
控制台上的日志仅为: ...(一些认知输出) 阴影中的更新凭据 连接 ===注册阴影 注册后。
因此,认知部分正在运行,我能够注册该东西。
从未调用shadows.on('status',...
和shadows.on('delta',
。即使有数据在流,因为我可以注册到AWS IoT mqtt客户端中的影子/更新/接受。
然后,我尝试从注册函数的回调中调用shadows.get(thingName)
,并且得到status
的回调工作一次。但是,对于所有被影子接受的新消息,回调都不起作用。
if (!shadowsRegistered) {
console.log('=== registering shadows ');
shadows.register(thingName,{
persistentSubscribe:true
},function(){
console.log('after registerging. ');
shadowsRegistered = true;
opClientToken = shadows.get(thingName);
});
}
如您所见,我可以轻松进行身份验证或其他操作。这可能只是对影子类如何工作的误解。任何帮助,高度赞赏。