2

I am working on AWS IoT, trying to create API to update Shadow things.

What I have done (in ClaudiaJS)

refer to 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';
    });

 }

I run the API, nothing happens, I know the reason that I didn't implement the Promise in my code yet. But I don't know how to do it in AWS IoT SDK though AWS SDK supported Promise(https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/)

Any suggestion is very appreciated.

Phong Vu
  • 2,726
  • 6
  • 24
  • 52

2 Answers2

1

I had faced the same problem but i was able to solved this problem using this code i am providing the link as well as code, please let me know if any help is needed 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))
}
Narendra Chouhan
  • 2,291
  • 1
  • 15
  • 24
-1

Maybe I guess you need thing on('foreignStateChange')