Skip to content
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

Merged
merged 2 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions device/LIFX/controller.js
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';
Copy link
Contributor

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


const BluePromise = require('bluebird');

let lifx = require('lifx-http-api');
let client = new lifx({
Copy link
Contributor

Choose a reason for hiding this comment

The 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, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is 'id:' + name correct here?

can you make a helper function to get device selector, for example

function getLifxDeviceSelector(deviceId) {
  return 'id:' + deviceId;
}

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')
Copy link
Contributor

@neophob neophob Apr 30, 2017

Choose a reason for hiding this comment

The 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);
});
}
51 changes: 51 additions & 0 deletions device/LIFX/index.js
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);
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"private": false,
"dependencies": {
"bluebird": "^3.5.0",
"neeo-sdk": "*"
"neeo-sdk": "*",
"lifx-http-api": "^1.0.3"
},
"engines": {
"node": ">=6.0.0"
Expand All @@ -17,11 +18,12 @@
"recipe": "node recipe/listAllRecipes.js",
"server:simple": "node device/simpleDevice/index.js",
"server:complex": "node device/discoverableLightDevice/index.js",
"server:lifx": "node device/LIFX/index.js",
"clean": "rm -rf ./dist && mkdir -p ./dist",
"release": "npm run jshint && npm run clean && npm run release:copy",
"release:copy": "cp -r ./{device,recipe} ./dist && cp .jshintrc .gitignore .editorconfig *.md package.json License ./dist"
},
"devDependencies": {
"jshint": "^2.9.4"
}
}
}