Skip to content

Instantly share code, notes, and snippets.

@tonkatsu7
Created December 10, 2017 10:38
Show Gist options
  • Save tonkatsu7/c8d652ed69e6c84f51075dd5bb181c2b to your computer and use it in GitHub Desktop.
Save tonkatsu7/c8d652ed69e6c84f51075dd5bb181c2b to your computer and use it in GitHub Desktop.
XKit parsing function from Thinxtra tutorial
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB();
var iotData = new AWS.IotData({endpoint: 'something.iot.ap-southeast-2.amazonaws.com'});
//make sure your replace your_AWS_endpoint_you_copied_earlier with the value you copied just before
exports.handler = function(event, context) {
console.log("Request received:\n", JSON.stringify(event));
console.log("Context received:\n", JSON.stringify(context));
var tableName = event.device;
var time = event.time;
var Temperature = event.data.Temperature;
var Pressure = event.data.Pressure;
var Photo = event.data.Photo;
var x_Accelerator = event.data.x_Accelerator;
var y_Accelerator = event.data.y_Accelerator;
var z_Accelerator = event.data.z_Accelerator;
var Accelerator = event.data.Accelerator;
function returnTime(value){
return new Date((value)*1000 + 36000000);
}
//replace 36000000 with the Unix time offset according to your timezone. Here it is set to Sydney time offset.
timedecode = '' +returnTime(time).toLocaleString();
Temperature = Temperature/100;
Pressure = Pressure*3;
Photo = Photo/1000;
x_Accelerator = x_Accelerator/250;
y_Accelerator = y_Accelerator/250;
z_Accelerator = z_Accelerator/250;
Accelerator = Math.sqrt(x_Accelerator * x_Accelerator + y_Accelerator * y_Accelerator + z_Accelerator * z_Accelerator);
var payloadObj={ "state": {
"reported": {
"device": event.device,
"time": event.time,
"timedecode": timedecode,
"snr": event.snr,
"avgSnr": event.avgSnr,
"station": event.station,
"Temperature": Temperature,
"Pressure": Pressure,
"Photo": Photo,
"x_Accelerator": x_Accelerator,
"y_Accelerator": y_Accelerator,
"z_Accelerator": z_Accelerator,
"Accelerator": Accelerator
},
}
};
var paramsUpdate = {
thingName : event.deviceId,
payload : JSON.stringify(payloadObj)
};
//This function will Update the Device Shadow State
iotData.updateThingShadow(paramsUpdate, function(err, data) {
if (err){
console.log("Error in updating the Thing Shadow");
console.log(err, err.stack);
}
});
//Next function will store the message in a dynamoDB table
dynamodb.putItem({
"TableName": event.deviceId,
"Item": {
"deviceId": {
"S": event.deviceId
},
"time": {
"S": event.time
},
"timedecode": {
"S": timedecode.toString()
},
"Temperature": {
"S": Temperature.toString()
},
"Pressure": {
"S": Pressure.toString()
},
"Photo": {
"S": Photo.toString()
},
"x_Accelerator": {
"S": x_Accelerator.toString()
},
"y_Accelerator": {
"S": y_Accelerator.toString()
},
"z_Accelerator": {
"S": z_Accelerator.toString()
},
"Accelerator": {
"S": Accelerator.toString()
}
}
},
function(err, data){
if (err) {
context.fail('ERROR: Dynamo failed: ' + err);
} else {
console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
context.succeed('SUCCESS');
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment