Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save AdamMagaluk/6e73ad65743d4f42e0bc to your computer and use it in GitHub Desktop.

Select an option

Save AdamMagaluk/6e73ad65743d4f42e0bc to your computer and use it in GitHub Desktop.
Zetta Apps
var intelliChiliBlink = require('./lib/intellichili-blink')
, photosensorHue = require('./lib/photosensor-hue')
, intelliChiliPebble = require('./lib/intellichili-pebble')
, photosensorDisplay = require('./lib/photosensor-display')
, pebbleHue = require('./lib/pebble-hue');
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
// expose everything to the browser
zetta.on('deviceready',function(device){
zetta.expose(device);
});
// setup hue notification for intellichili
intelliChiliBlink(zetta);
// setup nightlight between all photosensors and hubhub
photosensorHue(zetta);
// pebble notifications for intelli chili
intelliChiliPebble(zetta);
// display values on photosensor
photosensorDisplay(zetta);
pebbleHue(zetta);
};
var Scientist = require('../../scientist');
var Nightlight = require('./nightlight');
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta.get('joes-office-photosensor', function(err, photosensor) {
zetta.get('joes-office-led', function(err, led) {
var nightlight = Scientist.configure(Nightlight, photosensor, led);
zetta.expose(nightlight);
zetta.expose(nightlight.leds[0], '/nightlight/led')
});
});
};
var Nightlight = require('./nightlight');
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta.get('joes-office-photosensor', function(err, photosensor) {
zetta.get('joes-office-led', function(err, led) {
photosensor.on('change', function(value) {
if (value < 100) {
led.call('turn-on');
} else {
led.call('turn-off');
}
});
zetta.expose(led);
zetta.expose(photosensor);
});
});
};
var IntelliChili = module.exports = function() {
this.name = 'intellichili';
};
IntelliChili.prototype.init = function(elroy) {
elroy.observe('type="crockpot"').subscribe(function(err,crockpot){
elroy.expose(crockpot);
elroy.observe('type="huehub"').subscribe(function(err,hub){
crockpot.on('lid-closed',function(){
hub.call('blink');
});
crockpot.on('lid-closed',function(){
hub.call('blink');
});
});
});
};
/*
Weaknesses
- Having to expose everything
*/
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta
.observe('type="arm"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="lcd"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="barometer"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="humidity"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="sound"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="photocell"')
.subscribe(function(d) {
zetta.expose(d);
});
zetta
.observe('type="temperature"')
.subscribe(function(d) {
zetta.expose(d);
});
};
// Today
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta.on('deviceready', function(device){
zetta.expose(device);
});
};
// If we exposed everything by default
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
};
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(fog) {
var photosensor = fog.provision('photosensor');
var led = fog.provision('led');
led.on('turn-on', function() {
console.log('turning on led');
});
led.on('turn-off', function() {
console.log('turning off led');
});
photosensor.on('change', function(value) {
console.log(value);
if (value > 700) {
led.call('turn-on');
} else {
led.call('turn-off');
}
});
fog.expose('/led', led);
fog.expose('/photosensor', photosensor);
};
var ClumsyBird = module.exports = function() {
this.name = 'clumsy-bird';
};
var cb = function(){};
ClumsyBird.prototype.init = function(zetta) {
zetta.observe('type="button"')
.zip(zetta.observe('type="ardrone"')
,zetta.observe('type="huehub"')
,zetta.observe('type="smartwatch"'))
.subscribe(function(devices) {
console.log('Devices Ready')
var button = devices[0];
var ardrone = devices[1];
var hue = devices[2];
var watch = devices[3];
// ardrone climb
var droneLift = new ArDroneLift(ardrone);
button.on('press', function() {
hue.call('blink');
watch.call('sms', 'Pebble', 'Hello potato!', cb);
// droneLift.goUp();
});
});
zetta
.observe('type="huebulb"')
.zip(zetta.observe('type="photosensor"'))
.subscribe(function(devices){
var bulb = devices[0];
var sensor = devices[1];
bulb.call('turn-off');
sensor.on('update',function(val){
if(val < 900 && bulb.state !== 'off'){
bulb.call('turn-off');
}else if(val > 900 && bulb.state !== 'on'){
bulb.call('turn-on');
}
});
});
zetta.on('deviceready',function(device){
zetta.expose(device);
});
};
function ArDroneLift(ardrone){
this.timer = null;
this.count = 0;
this.duration = ardrone.data.movementTime;
this.ardrone = ardrone;
};
ArDroneLift.prototype.goUp = function(){
var self = this;
if(this.ardrone.state === 'landed'){
this.ardrone.call('take-off');
}else{
this.ardrone.call('up');
self.count++;
if(this.timer !== null)
clearTimeout(this.timer);
this.timer = setTimeout(function(){
self.ardrone.call('timed-down',self.count*self.duration*0.7,cb);
self.count = 0;
},self.duration);
}
};
/*
process.on('uncaughtException',function(err){
console.log('oops:',err);
});
*/
/*
Currently: App that pairs one led with one photosensor until 5 pairs are made.
Expose each.
Weaknesses
- Multiple mini apps in one app.js
- Have to pull in Scientist, possibly can be avoided.
- var clapper = Scientist.configure(ClapperLogic, sound, gocrazy);
- zetta.observe('type="arm"'), zetta.observe('type="huehub"'), zetta.observe('type="screen"'), zetta.observe('type="apigee"')
- devices[1],devices[2],devices[3],devices[4] -> devices.shift()
- Looping through all transitions on the arm.
- Catch all for devices.
- Devices cant use zetta.observe to create a device that uses existing devices. See: ClapperLogic
Strengths
- Use of external file/module for actual logic.
- EventEmitter pattern is very easy to understand when looking at code.
*/
var System = require('./system');
var GoCrazy = require('./gocrazy');
var ClapperLogic = require('./clapper_logic');
var Scientist = require('zetta-runtime').scientist;
var HelloApp = module.exports = function() {
this.name = process.env.APP_NAME || 'minifactory';
};
HelloApp.prototype.init = function(zetta) {
zetta
.observe('type="button"')
.zip(zetta.observe('type="arm"'), zetta.observe('type="huehub"'), zetta.observe('type="screen"'), zetta.observe('type="apigee"') )
.first()
.subscribe(function(devices){
console.log('Logic Button Ready')
var button = devices[0];
var gocrazy = new GoCrazy(devices[1],devices[2],devices[3],devices[4]);
button.on('click',function(){
gocrazy.notify();
});
});
zetta
.observe('type="screen"')
.zip(zetta.observe('type="arm"'), zetta.observe('type="huehub"'))
.first()
.subscribe(function(devices) {
var screen = devices[0];
var arm = devices[1];
var huehub = devices[2];
var armTransitions = ['open-claw', 'close-claw', 'elbow-up', 'elbow-down', 'shoulder-up', 'shoulder-down', 'pivot-left', 'pivot-right'];
armTransitions.forEach(function(transition) {
arm.on(transition, function(){
screen.call('change', 'arm: ' + transition,function(){
huehub.call('blink');
});
});
});
});
zetta
.observe('type="sound"')
.zip(zetta.observe('type="arm"'), zetta.observe('type="huehub"'), zetta.observe('type="screen"'), zetta.observe('type="apigee"') )
.first()
.subscribe(function(devices){
console.log('Logic Sound Ready')
var sound = devices[0];
var gocrazy = new GoCrazy(devices[1],devices[2],devices[3],devices[4]);
var clapper = Scientist.configure(ClapperLogic, sound, gocrazy);
zetta.expose(clapper);
});
zetta.on('deviceready',function(device){
zetta.expose(device);
});
var system = zetta.configure(System);
zetta.expose(system);
};
/*
Ideal with use of one app.js but multiple app files.
- Removed System as that was really a hack for solid, system control should
sit above the app.
- Moved button logic to external module.
- subscribe could take an arg array to remove the devices[0]...
- GoCrazy finds it's own devices.
*/
var GoCrazy = require('./gocrazy');
var HelloApp = module.exports = function() {
this.name = 'minifactory';
};
HelloApp.prototype.init = function(zetta) {
// Option 1:
// When big red button is pressed init GoCrazy
buttonLogic(zetta);
// Option 2:
// Look for one screen, arm, hue hub. When any transition fires on arm
// update screen with transition's name and blink hue.
zetta
.observe('type="screen"')
.zip(zetta.observe('type="arm"'), zetta.observe('type="huehub"'))
.first()
.subscribe(function(screen, arm, huehub) { //.subscribe(function(devices) {
arm.on('*', function(event){
screen.call('change', 'arm: ' + event.name);
huehub.call('blink');
});
});
// When any arm in any fog moves update all screens and blink all hues
zetta
.observe('type="arm"')
.subscribe(function(arm) {
zetta
.observe('type="screen"')
.zip(zetta.observe('type="huehub"'))
.subscribe(function(screen, arm, huehub) {
arm.on('*', function(event){
screen.call('change', 'arm: ' + event.name);
huehub.call('blink');
});
});
});
zetta
.observe('type="microphone"')
.subscribe(function(microphone){
// GoCrazy is not an app, just external logic.
var gocrazy = new GoCrazy(zetta);
// zetta.configure is still not instantly clear.
var clapper = zetta.configure(ClapperLogic, microphone, gocrazy);
});
};
//////// buttonLogic.js
var GoCrazy = require('./goCrazy');
module.exports = function(zetta){
zetta
.observe('type="button"')
.subscribe(function(button){
// pass zetta to go crazy allowing it to pull in all devices that go crazy
// uses.
var gocrazy = new GoCrazy(zetta);
button.on('click',function(){
gocrazy.notify();
});
});
};
/*
Currently: App that pairs one led with one photosensor until 5 pairs are made.
Expose each.
Weaknesses
- Exposing each device with a loop.
- Pairing logic doesn't usually make sense.
- Why is 5 hard coded?
Strengths
- Uses a existing pattern. Knowledge base for reactive is established. Don't
reinvent the wheel.
*/
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta.observe('type="led"')
.zip(zetta.observe('type="photosensor"'))
.take(5)
.subscribe(function(devices) {
devices.forEach(function(d) {
zetta.expose(d);
});
});
};
/*
Hue Use Case: Hue light bulbs paired with Wemo Light switch.
1. API would need to expose all hue bulbs and lights.
- Are switches only wemo? Should you query on all switch attributes.
- Currently you can't query againts devices on the API.
2. App would generally have a login, is this user associated with a fog?
- If so then all queries would just have to happen on one fog.
- Factory/Enteprise would need to have more fined grained permission model.
- If not one fog, would the cloud api aggragate api calls to fogs?
3. In order for an app the create some link between a light and switch through
some app it would need to save which lights are linked to which switches.
- Local storage on apps that saves the assocations.
- Would the app save data to the device? Multiple apps writing to a device could conflict.
- App scoped data on the device.
*/
// Configure your app.
var HelloApp = module.exports = function() {
this.name = 'hello';
// Disable default exposing of alll devices.
this.exposeDevices = false;
// Other options...
};
HelloApp.prototype.init = function(zetta) {
zetta.observe('type="switch"')
.subscribe(function(button) {
// Watch for any leds that come online and have its switch associated with
// the switch's ID.
zetta.observe('type="led" AND associatedSwitch="'+button.id+'" ')
.subscribe(function(led){
//toggle switch when clicked.
button.on('click', function(){
led.call('toggle');
});
});
});
};
// Alt without subscribe
var HelloApp = module.exports = function() {
this.name = 'hello';
};
HelloApp.prototype.init = function(zetta) {
zetta.observe('type="switch"', function(switch) {
// Watch for any leds that come online and have its switch associated with
// the switch's ID.
zetta.observe('type="led" AND associatedSwitch="'+switch.id+'" ', function(led){
//toggle switch when clicked.
switch.on('click', function(){
led.call('toggle');
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment