Created
August 3, 2016 03:20
-
-
Save jinman/b2c50cc16df6526caed08041e7766ae2 to your computer and use it in GitHub Desktop.
LambdaToLIFXBulb
This file contains hidden or 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
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