Skip to content

Instantly share code, notes, and snippets.

@lheritage
Created November 1, 2017 21:37
Show Gist options
  • Save lheritage/da5da1a3410b14d6f458bdb61b5e6030 to your computer and use it in GitHub Desktop.
Save lheritage/da5da1a3410b14d6f458bdb61b5e6030 to your computer and use it in GitHub Desktop.
Amazon IoT Button - API Builder
'use strict';
const AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
console.log('Received event:', event);
console.log('event.clickType = '+event.clickType);
if(event.clickType === "SINGLE") {
console.log('Single click detected');
logObservation(event.serialNumber);
} else if(event.clickType === "DOUBLE") {
console.log('Double click detected');
getObservationLog(event.serialNumber);
} else if(event.clickType === "LONG") {
console.log('Long click detected');
// requestMortgageDoc();
clearObservationLog(event.serialNumber);
} else {
console.log('Unsupported click detected');
}
};
function logObservation(deviceId) {
console.log('logObservation() called');
var http = require("https");
var options = {
"method": "POST",
"hostname": "{your api builder endpoint}.cloudapp-enterprise.appcelerator.com",
"port": null,
"path": "/api/observation",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(JSON.stringify({ deviceId: deviceId, type: 'Single' }));
req.end();
}
function getObservationLog(deviceId) {
console.log('getObservationLog() called');
var http = require("https");
var options = {
"method": "GET",
"hostname": "{your api builder endpoint}.cloudapp-enterprise.appcelerator.com",
"port": null,
"path": "/api/requestObservationLog?deviceId="+deviceId,
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(JSON.stringify({ deviceId: deviceId, type: 'Single' }));
req.end();
}
function clearObservationLog(deviceId) {
console.log('clearObservationLog() called');
var http = require("https");
var options = {
"method": "GET",
"hostname": "{your api builder endpoint}.cloudapp-enterprise.appcelerator.com",
"port": null,
"path": "/api/deleteObservationsById?deviceId="+deviceId,
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "145a4756-6f66-9642-25eb-7c69d5dacffe"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment