【问题标题】:AWS IOT SDK- how to implement the PromiseAWS IOT SDK-如何实现 Promise
【发布时间】:2018-02-12 09:25:14
【问题描述】:

我正在研究 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/)

非常感谢任何建议。

【问题讨论】:

    标签: promise aws-sdk aws-iot aws-sdk-js claudiajs


    【解决方案1】:

    我也遇到过同样的问题,但我可以使用这段代码解决这个问题 我正在提供链接和代码,如果需要任何帮助,请告诉我 https://gist.github.com/dave-malone/611800d7afa90561f3b40ca6b2380faf

    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))
    }
    

    【讨论】:

      【解决方案2】:

      也许我猜你需要打开('foreignStateChange')

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 2023-03-12
        • 2018-05-04
        • 2019-01-13
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 2016-03-28
        相关资源
        最近更新 更多