Created
February 11, 2013 06:57
-
-
Save thatguydan/4753045 to your computer and use it in GitHub Desktop.
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
var util = require('util') | |
, stream = require('stream') | |
, os = require('os') | |
, helpers = require('./lib/helpers') | |
, Hue = require('hue.js') | |
, Light = require('./lib/Light'); | |
util.inherits(hue,stream); | |
module.exports = hue; | |
function hue(opts,app) { | |
var self = this; | |
this._app = app; | |
this._opts = opts; | |
this._opts.stations = opts.stations || []; | |
// Todo: use node ID | |
this.appName = 'Hue Ninja Module'; | |
app.on('client::up', function() { | |
if (self._opts.stations.length>0) { | |
loadStations.call(self); | |
} else { | |
findStations.call(self); | |
} | |
}); | |
}; | |
var HUE_ANNOUNCEMENT = { | |
"contents": [ | |
{ "type": "heading", "text": "New Philips Hue Link Detected" }, | |
{ "type": "paragraph", "text": "A new Philips Hue was detected on your network, would you like to enable it for use with Ninja Blocks?" }, | |
{ "type": "hidden", "name": "ip", "value": "x.x.x.x" } | |
], | |
"actions": [ | |
{ "type": "submit", "name": "Add to my Ninja Dashboard", "rpc_method": "HUE_POST_ANNOUNCEMENT" } | |
], | |
}; | |
/** | |
* Called when a user prompts a configuration | |
* @param {Object} payload The return payload from the UI | |
* @param {String} state Used to match up requests. | |
* @param {Function} cb Callback with return data | |
*/ | |
hue.prototype.config = function(rpc,cb) { | |
if (!payload) { | |
// GET/PROBED block | |
cb({ | |
contents: [{ | |
"type" : "paragraph", "text" : "To search for Hue stations, click the button below" | |
}, | |
actions: [{ | |
"type" : "submit" , "name" : "Scan", "rpc_method": "SCAN" | |
}] | |
}); | |
return; | |
} | |
switch (rpc.method) { | |
case 'HUE_POST_ANNOUNCEMENT': | |
// Register Base station | |
break; | |
case 'SCAN': | |
// Scan for new ones | |
break; | |
} | |
}; | |
function findStations() { | |
this._app.log.info('Hue: No configuration') | |
var self = this; | |
// If we do not want to auto register | |
if (!this._opts.autoRegister) return; | |
Hue.discover(function(stations) { | |
stations.forEach(registerStation.bind(self)); | |
}); | |
}; | |
function registerStation(station) { | |
if (this._opts.stations.indexOf(station)>-1) { | |
// We already have this station registered. | |
this._app.log.info('Hue: Already configured Hue %s, aborting',station); | |
return; | |
} | |
var self = this; | |
var client = Hue.createClient({ | |
stationIp:station, | |
appName:this.appName | |
}); | |
var registerOpts = { | |
interval:2000, | |
attempts:0 | |
}; | |
self._app.log.info('Hue: Please press link button on station %s',station); | |
// TODO Update Hue IP Address inside HUE_ANNOUNCEMENT | |
self.emit('announcement',HUE_ANNOUNCEMENT); | |
client.register(registerOpts,function(err) { | |
if (err) { | |
// Do nothing? | |
self._app.log.info('Timed out waiting for station') | |
return; | |
} | |
self._app.log.info('Hue: station %s registered, saving',station); | |
self._opts.stations.push(station); | |
self.save(); | |
loadStations.call(self); | |
}); | |
}; | |
function loadStations() { | |
this._opts.stations.forEach(fetchLights.bind(this)); | |
}; | |
function fetchLights(station,stationIndex) { | |
var self = this; | |
var client = Hue.createClient({ | |
stationIp:station, | |
appName:this.appName | |
}); | |
client.lights(function(err,lights) { | |
if (err) { | |
// TODO check we are registered | |
self._app.log.error(err); | |
return; | |
} | |
Object.keys(lights).forEach(function(lightIndex) { | |
self.emit('register',new Light(client,stationIndex,lightIndex)) | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment