Skip to content

Instantly share code, notes, and snippets.

@jinman
Created August 3, 2016 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinman/b2c50cc16df6526caed08041e7766ae2 to your computer and use it in GitHub Desktop.
Save jinman/b2c50cc16df6526caed08041e7766ae2 to your computer and use it in GitHub Desktop.
LambdaToLIFXBulb
var https = require('https');
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`. For more information see the HTTPS module documentation
* at https://nodejs.org/api/https.html.
*
* Will succeed with the response body.
*/
exports.handler = function(event, context) {
var LIFX_API_KEY = "[YOUR KEY HERE]:";
var LIFX_BULB_ID = "[YOUR BULB ID HERE]";
var lifxopts = {
"color": "blue",
"period": 2,
"cycles": 3,
"persist": false,
"power_on": true
};
switch(event.clickType) {
case 'SINGLE':
lifxopts.color = "yellow";
break;
case 'DOUBLE':
lifxopts.color = "red";
break;
default:
}
// The 'breathe' effect is a soft pulse effect. Options defined in lifxopts above
var options = {
hostname: 'api.lifx.com',
port: 443,
path: '/v1/lights/' + LIFX_BULB_ID + '/effects/breathe',
method: 'POST',
headers: {
"content-type": "application/json",
"content-length": JSON.stringify(lifxopts).length
},
auth: LIFX_API_KEY
};
var req = https.request(options, function(res) {
var body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
context.succeed(body);
});
});
req.on('error', context.fail);
req.write(JSON.stringify(lifxopts));
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment