首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS IOT SDK-如何实现Promise

AWS IOT SDK-如何实现Promise
EN

Stack Overflow用户
提问于 2018-02-12 17:25:14
回答 2查看 600关注 0票数 2

我在亚马逊网络服务的IoT上工作,试图创建应用程序接口来更新阴影事物。

我所做的(在ClaudiaJS中)

请参阅https://github.com/aws/aws-iot-device-sdk-js

代码语言:javascript
复制
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,什么也没有发生,我知道我还没有在代码中实现承诺的原因。但是我不知道如何在AWS IoT SDK中做到这一点,尽管AWS SDK支持Promise(https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/)

任何建议都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2019-03-18 21:27:56

我曾面临同样的问题,但我能够解决这个问题使用这个代码,我提供了链接以及代码,请让我知道,如果需要任何帮助https://gist.github.com/dave-malone/611800d7afa90561f3b40ca6b2380faf

代码语言:javascript
复制
const AWS = require('aws-sdk')

AWS.config.region = process.env.AWS_REGION

const iotdata = new AWS.IotData({
endpoint: process.env.MQTT_BROKER_ENDPOINT,
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY
})

const openState = "open"
const closedState = "closed"

let currentState = closedState

function toggleGarageDoor() {
return new Promise((resolve, reject) => {
let desiredState = (currentState === closedState) ? openState : closedState

var params = {
  payload: `{"state":{"desired":{"door":"${desiredState}"}}}`,
  thingName: process.env.THING_NAME
}

iotdata.updateThingShadow(params, (err, data) => {
  if (err){
    console.log(err, err.stack)
    reject(`Failed to update thing shadow: ${err.errorMessage}`)
  }else{
    console.log(`update thing shadow response: ${JSON.stringify(data)}`)
    currentState = desiredState
    resolve({"update thing shadow response": data})
  }
})
})
}

exports.handler = async (event, context, callback) => {
await toggleGarageDoor()
.then((result) => callback(null, result))
.catch((err) => callback(err))
}
票数 1
EN

Stack Overflow用户

发布于 2020-04-08 17:25:38

也许我猜你需要一些东西(‘foreignStateChange’)

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48743069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档