Last active
August 29, 2015 14:01
-
-
Save AdamMagaluk/e5181c2d5a9c93d7755d to your computer and use it in GitHub Desktop.
Zetta Device Package
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 HueScout = require('zetta-hue-driver'); | |
var PushNotificationScout = require('zetta-apigee-driver'); | |
module.exports = function(runtime) { | |
runtime.scouts.push(HueScout); | |
runtime.scouts.push(PushNotificationScout); | |
}; |
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 lightState = require("node-hue-api").lightState; | |
var HueBulbDriver = module.exports = function(data,hue) { | |
this.type = 'huebulb'; | |
this.name = 'Hue Bulb '+data.name; | |
data.hue = hue; | |
this.data = data; | |
this.hue = hue; | |
this.state = 'off'; | |
}; | |
HueBulbDriver.prototype.init = function(config) { | |
config | |
.when('on', { allow: ['turn-off', 'toggle','blink','color'] }) | |
.when('off', { allow: ['turn-on', 'toggle','blink','color'] }) | |
.when('blink', { allow: ['turn-on', 'toggle','blink','color'] }) | |
.map('turn-on', this.turnOn) | |
.map('turn-off', this.turnOff) | |
.map('toggle', this.toggle) | |
.map('blink',this.blink) | |
.map('color',this.color,[{type:'color',name : 'color'}]); | |
}; | |
HueBulbDriver.prototype.blink = function(cb){ | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
this.hue.setLightState(this.data.id,{alert : 'select'},function(err){ | |
cb(); | |
}); | |
}; | |
HueBulbDriver.prototype.color = function(color,cb){ | |
if(!this.hue) | |
return cb(); | |
color = color.match(/[0-9a-f]{1,2}/g).map(function(c){ return parseInt(c,16); }); | |
var self = this; | |
var state = lightState.create().on().rgb(color[0],color[1],color[2]); | |
this.hue.setLightState(this.data.id,state,function(err){ | |
cb(); | |
}); | |
}; | |
HueBulbDriver.prototype.turnOn = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
self.state = 'on'; | |
var state = lightState.create().on(); | |
this.hue.setLightState(this.data.id,state,function(err){ | |
if(err) | |
return cb(err); | |
cb(); | |
}); | |
}; | |
HueBulbDriver.prototype.turnOff = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
self.state = 'off'; | |
var state = lightState.create().off(); | |
this.hue.setLightState(this.data.id,state,function(err){ | |
if(err) | |
return cb(); | |
cb(); | |
}); | |
}; | |
HueBulbDriver.prototype.toggle = function(cb) { | |
if (this.state === 'off') { | |
this.call('turn-on'); | |
cb(); | |
} else if (this.state === 'on') { | |
this.call('turn-off'); | |
cb(); | |
} else { | |
cb(new Error('Invalid state - Valid states are "on" and "off".')); | |
} | |
}; |
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 HueApi = require("node-hue-api").HueApi; | |
var async = require('async'); | |
var HueBulbDriver = require('./hue_bulb'); | |
var Scientist = require("zetta-runtime").scientist; | |
var lightState = require("node-hue-api").lightState; | |
var HueHubDriver = module.exports = function(data,_newLightFunc) { | |
this.type = 'huehub'; | |
this.name = 'Hue Hub '+data.id; | |
this.data = data; | |
this._newLight = _newLightFunc; | |
this.lights = []; | |
if(!data.registered) | |
this.state = 'unregistered'; | |
else{ | |
this.state = 'registered'; | |
this.hue = new HueApi(this.data.ipaddress, this.data.user); | |
} | |
}; | |
HueHubDriver.prototype.init = function(config) { | |
config | |
.when('unregistered', { allow: ['register'] }) | |
.when('registered', { allow: ['blink','find-lights','all-on','all-off','brightness', 'color','color-loop'] }) | |
.when('colorloop', { allow: ['blink','find-lights','all-on','all-off','brightness', 'color'] }) | |
.map('register', this.register) | |
.map('blink', this.blink) | |
.map('all-on',this.allOn) | |
.map('all-off',this.allOff) | |
.map('find-lights',this.findLights) | |
.map('color-loop',this.colorLoop) | |
.map('color',this.color,[{type:'color',name : 'color'}]) | |
.map('brightness',this.brightness,[{type: 'number', name: 'brightness'}]); | |
}; | |
HueHubDriver.prototype.register = function(cb) { | |
var self = this; | |
var hue = new HueApi(); | |
hue.createUser(this.data.ipaddress, null, null, function(err, user) { | |
if (err) | |
return cb(null); | |
self.data.user = user; | |
self.data.registered = true; | |
self.state = 'registered'; | |
self.hue = new HueApi(self.data.ipaddress, self.data.user); | |
self.findLights(function(){ | |
cb(null); | |
}); | |
}); | |
}; | |
HueHubDriver.prototype.color = function(color,cb){ | |
if(!this.hue) | |
return cb(); | |
color = color.match(/[0-9a-f]{1,2}/g).map(function(c){ return parseInt(c,16); }); | |
console.log('setting color:',color); | |
var self = this; | |
var state = lightState.create().on().rgb(color[0],color[1],color[2]); | |
this.hue.setGroupLightState(0,state,function(err){ | |
cb(); | |
}); | |
}; | |
HueHubDriver.prototype.blink = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
self.hue.setGroupLightState(0, {alert : "select"},function(){ | |
cb(); | |
}); | |
}; | |
HueHubDriver.prototype.allOn = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
this.data.lightval = 'on'; | |
var state = lightState.create().on().brightness(100).transition(0).effect('none'); | |
this.hue.setGroupLightState(0,state,function(err){ | |
if(!err) | |
self.state = 'registered'; | |
cb(); | |
}); | |
}; | |
HueHubDriver.prototype.allOff = function(cb) { | |
if(!this.hue) | |
return cb(); | |
this.data.lightval = 'off'; | |
var state = lightState.create().off().transition(0); | |
this.hue.setGroupLightState(0,state,function(){ | |
return cb(); | |
}); | |
}; | |
HueHubDriver.prototype.colorLoop = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
this.data.lightval = 'on'; | |
var state = lightState.create().on().brightness(100).transition(0).effect('colorloop'); | |
this.hue.setGroupLightState(0,state,function(err){ | |
if(!err) | |
self.state = 'colorloop'; | |
cb(); | |
}); | |
}; | |
HueHubDriver.prototype.brightness = function(brightness,cb) { | |
if(!this.hue) | |
return cb(); | |
var state = lightState.create().brightness(brightness); | |
this.hue.setGroupLightState(0,state,function(err){ | |
cb(); | |
}); | |
}; | |
HueHubDriver.prototype._lightExists = function(light) { | |
return (this.lights.filter(function(l){ | |
return (l.id === light.id); | |
}).length > 0); | |
}; | |
HueHubDriver.prototype.findLights = function(cb) { | |
if(!this.hue) | |
return cb(); | |
var self = this; | |
this.hue.lights(function(err, res) { | |
if (err) | |
return cb(); | |
res.lights.forEach(function(light){ | |
if(self._lightExists(light)) | |
return; | |
self.lights.push(light); | |
self._newLight(light,self.hue); | |
}); | |
cb(); | |
}); | |
}; |
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 util = require('util') | |
, EventEmitter = require('events').EventEmitter | |
, hue = require("node-hue-api") | |
, HueApi = require("node-hue-api").HueApi | |
, HueHubDriver = require('./hue_hub') | |
, HueBulbDriver = require('./hue_bulb'); | |
var HueScout = module.exports = function() { | |
this.interval = 15000; | |
EventEmitter.call(this); | |
this.drivers = ['huehub','huebulb']; | |
}; | |
util.inherits(HueScout, EventEmitter); | |
HueScout.prototype.init = function(next) { | |
// start search logic | |
this.search(); | |
setInterval(this.search.bind(this),this.interval); | |
next(); | |
}; | |
HueScout.prototype.provision = function(device) { | |
if(device.type === 'huehub'){ | |
return [HueHubDriver,device.data,this.newLight.bind(this)]; | |
} else if(device.type === 'huebulb'){ | |
var hueapi = new HueApi(device.data.hue.host, device.data.hue.username); | |
return [HueBulbDriver,device.data,hueapi] | |
} | |
}; | |
HueScout.prototype.search = function() { | |
var self = this; | |
hue.locateBridges(function(err, hubs) { | |
if(err) | |
return; | |
hubs.forEach(function(hueHub){ | |
self.emit('discover', HueHubDriver,hueHub,self.newLight.bind(this)); | |
}); | |
}); | |
}; | |
HueScout.prototype.newLight = function(light,hueapi){ | |
this.emit('discover', HueBulbDriver,light,hueapi); | |
}; | |
HueScout.prototype.compare = function(a,b) { | |
return (a.data.id === b.data.id); | |
}; |
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
{ | |
"name": "zetta-hue-driver", | |
"version": "0.0.8", | |
"description": "Zetta hue driver", | |
"main": "hue_scout.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"node-hue-api": "^0.2.2", | |
"async": "^0.7.0", | |
"zetta-runtime": ">0.0.12" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment