我正在研究 AWS IoT,试图创建 API 来更新影子事物。
我做了什么(在 ClaudiaJS 中)
参考https://github.com/aws/aws-iot-device-sdk-js
var awsIot = require('aws-iot-device-sdk');
api.post(PREFIX + '/iot/test/{property}', function (request) {
var property = request.pathParams.property;
var thingShadows = awsIot.thingShadow({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
host: <YourCustomEndpoint>
});
var clientTokenUpdate;
thingShadows.on('connect', function() {
thingShadows.register( 'IoTTestThing', {}, function() {
var shadowState = {"state":{"desired":{"property": property}}};
clientTokenUpdate = thingShadows.update('IoTTestThing', shadowState );
if (clientTokenUpdate === null)
{
console.log('update shadow failed, operation still in progress');
}
});
});
thingShadows.on('status',
function('IoTTestThing', stat, clientToken, stateObject) {
console.log('received '+stat+' on '+thingName+': '+
JSON.stringify(stateObject));
return 'IoT status updated';
});
thingShadows.on('delta',
function('IoTTestThing', stateObject) {
console.log('received delta on '+thingName+': '+
JSON.stringify(stateObject));
return 'IoT delta updated';
});
}
我运行 API,没有任何反应,我知道我还没有在我的代码中实现 Promise 的原因。但我不知道如何在 AWS IoT SDK 中做到这一点,尽管 AWS SDK 支持 Promise(https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/)
任何建议都非常感谢。