Last active
August 29, 2015 14:01
-
-
Save AdamMagaluk/adabe47930ca4ec3628c to your computer and use it in GitHub Desktop.
Zetta Scoutes
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); | |
}; | |
/* | |
Example of how the provision stuff could be modified to clean things up. | |
- this.compare would go on the device driver. | |
*/ | |
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); | |
}; | |
util.inherits(HueScout, EventEmitter); | |
HueScout.prototype.init = function(zetta, next) { | |
zetta.provision(HueHubDriver); | |
zetta.provision(HueBulbDriver, this.provisionBulb); | |
// start search logic | |
this.search(); | |
setInterval(this.search.bind(this),this.interval); | |
next(); | |
}; |
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 EventEmitter = require('events').EventEmitter; | |
var mdns = require('mdns2'); | |
var util = require('util'); | |
var LCDDriver = require('../drivers/lcd_arduino.js'); | |
var MDNSScout = module.exports = function() { | |
EventEmitter.call(this); | |
}; | |
util.inherits(MDNSScout, EventEmitter); | |
MDNSScout.prototype.init = function() { | |
var self = this; | |
var browser = mdns.createBrowser(mdns.tcp('lcddisplay')); | |
browser.on('serviceUp', function(service) { | |
var ipAddr = service.addresses[0]; | |
self.emit('discover', LCDDriver,ipAddr); | |
}); | |
browser.start(); | |
}; |
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 EventEmitter = require('events').EventEmitter; | |
var readline = require('readline'); | |
var util = require('util'); | |
var serialport = require('serialport'); | |
var ScreenDriver = require('../drivers/screen_driver'); | |
var ScreenScout = module.exports = function() { | |
EventEmitter.call(this); | |
this._serialPort = null; | |
this.drivers = []; | |
this.deviceName = process.env.SCREEN_DEVICE || '/dev/tty.usbmodem1421'; | |
}; | |
util.inherits(ScreenScout, EventEmitter); | |
ScreenScout.prototype.init = function(next) { | |
this._serialPort = new serialport.SerialPort(this.deviceName, { | |
baudRate: 9600, | |
parser: serialport.parsers.readline('\r') | |
}); | |
var self = this; | |
this._serialPort.on('open', function(err) { | |
if (err) { | |
console.log('error on open:', err); | |
} | |
self._serialPort.on('data', function(data) { | |
if (data === 'ready') { | |
self.emit('discover', ScreenDriver, self._serialPort, 'touch-screen'); | |
} | |
}) | |
}); | |
this._serialPort.on('error', function(err) { | |
console.log('error on serialport:', err); | |
}); | |
next(); | |
}; |
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'); | |
var EventEmitter = require('events').EventEmitter; | |
var SerialPort = require('serialport').SerialPort; | |
var xbee_api = require('xbee-api'); | |
var PhotoSensor = require('../drivers/photosensor_driver'); | |
var ZigbeeScout = module.exports = function() { | |
EventEmitter.call(this); | |
this.drivers = []; | |
this.portname = '/dev/tty.usbserial-A601EM9Z'; | |
this.port = null; | |
this.xbeeAPI = new xbee_api.XBeeAPI({ | |
api_mode: 1 | |
}); | |
}; | |
util.inherits(ZigbeeScout, EventEmitter); | |
ZigbeeScout.prototype.init = function(next) { | |
var self = this; | |
this.port = new SerialPort(this.portname, { | |
baudrate: 9600, | |
parser: this.xbeeAPI.rawParser() | |
}); | |
this.port.on('open',function(){ | |
self.xbeeAPI.on("frame_object", self._onFrame.bind(self)); | |
next(); | |
}); | |
}; | |
ZigbeeScout.prototype._onFrame = function(frame) { | |
if(frame.type === 146){ | |
this.emit('discover',PhotoSensor,frame,this.xbeeAPI); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment