Created
November 1, 2017 21:37
-
-
Save lheritage/da5da1a3410b14d6f458bdb61b5e6030 to your computer and use it in GitHub Desktop.
Amazon IoT Button - API Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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