-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added simple LIFX example #2
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Quick example for an LIFX light wrote by Geert Wille | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const BluePromise = require('bluebird'); | ||
|
||
let lifx = require('lifx-http-api'); | ||
let client = new lifx({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lifx and client should be a const, they will never change |
||
bearerToken: 'INSERT_ACCESS_TOKEN_HERE' // https://cloud.lifx.com/settings | ||
}); | ||
|
||
let sliderValue = 50; | ||
let switchValue = true; | ||
let sendComponentUpdate; | ||
|
||
/* | ||
* Device Controller | ||
* Events on that device from the Brain will be forwarded here for handling. | ||
*/ | ||
module.exports.onPulse = function (deviceId, name) { | ||
// Just randomly pulse the light | ||
client.pulse('id:' + name, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is can you make a helper function to get device selector, for example
this would make the code much more readable |
||
color: '#' + Math.floor(Math.random() * 16777215).toString(16), // Generate random color | ||
from_color: '#' + Math.floor(Math.random() * 16777215).toString(16), // Generate random color | ||
period: 1, | ||
cycles: 5, | ||
persist: false, | ||
power_on: true, | ||
peak: 0.8 | ||
}); | ||
}; | ||
|
||
/** | ||
* Getters and setters: | ||
* - The getters are used to send the current Values through the SDK (read) | ||
* - The setter allow changing values on the Brain and handling the changes here (write) | ||
*/ | ||
module.exports.switchSet = function (deviceId, value) { | ||
console.log('[CONTROLLER] switch set to', deviceId, value); | ||
switchValue = value; | ||
|
||
// Update LIFX light | ||
client.setState('id:' + deviceId, { | ||
power: switchValue == 'true' ? 'on' : 'off' | ||
}).then(console.log, console.error); | ||
}; | ||
|
||
module.exports.switchGet = function (deviceId) { | ||
console.log('[CONTROLLER] return switch value', deviceId, switchValue); | ||
return BluePromise.resolve(switchValue); | ||
}; | ||
|
||
module.exports.sliderSet = function (deviceId, value) { | ||
console.log('[CONTROLLER] dimmer set to', deviceId, value); | ||
sliderValue = value; | ||
|
||
// Update LIFX light | ||
client.setState('all', { | ||
brightness: sliderValue / 100 | ||
}).then(console.log, console.error); | ||
}; | ||
|
||
module.exports.sliderGet = function (deviceId) { | ||
console.log('[CONTROLLER] return current dimmer value', deviceId, sliderValue); | ||
return BluePromise.resolve(sliderValue); | ||
}; | ||
|
||
/* | ||
* Discover all LIFX lights and return them in the proper format | ||
*/ | ||
module.exports.discoverLIFX = function discoverLIFX() { | ||
return client.listLights('all') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make a const for 'all' like: const LIFX_ALL_DEVICES_SELECTOR = 'all'; so it's much more readable |
||
.then((LIFXLights) => { | ||
return LIFXLights.map((LIFX) => ({ | ||
id: LIFX.id, | ||
name: LIFX.group.name, | ||
reachable: LIFX.reachable | ||
})); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR!', err); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Quick example for an LIFX light wrote by Geert Wille | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const neeoapi = require('neeo-sdk'); | ||
const controller = require('./controller'); | ||
|
||
console.log('NEEO SDK Example "LIFX" adapter'); | ||
console.log('---------------------------------------------'); | ||
|
||
/* | ||
* Adapter - simple LIFX integration. | ||
*/ | ||
|
||
// first we set the device info, used to identify it on the Brain | ||
const simpleLIFXLight = neeoapi.buildDevice('Smart Light') // LIFX LIFX looked weird in the app so I just named it Smart Light | ||
.setManufacturer('LIFX') | ||
.addAdditionalSearchToken('light') | ||
.setType('LIGHT') | ||
.enableDiscovery({ | ||
headerText: 'Ready to discover LIFX lights', | ||
description: 'Make sure you create a new accessToken on https://cloud.lifx.com/settings so we can retrieve your lights' | ||
}, controller.discoverLIFX) | ||
// Then we add the capabilities of the LIFX bulb | ||
.addSlider({ name: 'power-slider', label: 'Dimmer', range: [0, 100], unit: '%' }, { setter: controller.sliderSet, getter: controller.sliderGet }) | ||
.addSwitch({ name: 'toggle', label: 'Toggle ON/OFF' }, { setter: controller.switchSet, getter: controller.switchGet }) | ||
.addButton({ name: 'pulse', label: 'Pulse' }) | ||
.addButtonHander(controller.onPulse); | ||
|
||
console.log('- discover one NEEO Brain...'); | ||
neeoapi.discoverOneBrain() | ||
.then((brain) => { | ||
console.log('- Brain discovered:', brain.name); | ||
|
||
console.log('- Start server'); | ||
return neeoapi.startServer({ | ||
brain, | ||
port: 6336, | ||
name: 'simple-lifx', | ||
devices: [simpleLIFXLight] | ||
}); | ||
}) | ||
.then(() => { | ||
console.log('# READY! use the NEEO app to search for "LIFX".'); | ||
}) | ||
.catch((err) => { | ||
console.error('ERROR!', err); | ||
process.exit(1); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use strict should be at the first line of the file