-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
125 lines (102 loc) · 3.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var firebase = require('firebase');
var fs = require('fs');
var five = require('johnny-five');
var chipio = require('chip-io');
var q = require('q');
//Johny five & chip
var board = new five.Board({
//When running Johnny-Five programs as a sub-process (eg. init.d, or npm scripts), be sure to shut the REPL off!
repl: false,
io: new chipio()
});
//Johny five & arduino
//Firebase configuration
var firebaseConfig = {
apiKey: 'AIzaSyDrjTwTt-_KXK8XGZCGy0y_1Ou_NBYmrq0',
authDomain: 'ship-582de.firebaseapp.com',
databaseURL: 'https://ship-582de.firebaseio.com',
storageBucket: 'ship-582de.appspot.com'
};
firebase.initializeApp({
serviceAccount: 'run/ship.json',
databaseURL: firebaseConfig.databaseURL
});
var db = firebase.database();
var ships = db.ref('ships');
//Local configuration
var configFile = './run/config.json';
var Config = require('./shipConfig.js');
//Use the config module to get a current or generate a new config.
var config = new Config(configFile, ships);
var deferredChipBoard = q.defer();
var deferredConfig = q.defer();
var io;
var IO_HANDLERS = {
setStatusLed: function (status) {
console.log('StatusLed', status);
io.statusLed.stop();
if(status){
io.statusLed.on();
} else {
io.statusLed.off();
}
},
setPin: function (status, pin) {
console.log('Pin: ', pin, ' Status: ', status);
}
};
board.on('ready', function() {
var statusLed = new chipio.StatusLed();
// Blink every half second
statusLed.blink(500);
io = {
statusLed : statusLed
};
deferredConfig.resolve(board, io);
});
//Once we have our configuration loaded from the server we can run all defined IO event handlers
config.once('loaded', (id) => {
//Get a reference to this ships model
var shipRef = ships.child(id);
//An event handler for this specific Ship
//shipRef.on('child_changed', function (snapshot) {
// var newValue = snapshot.val();
// console.log(snapshot.key + ' changed to: ', newValue);
//});
//Setup event handlers for all soft IO events
//If a buttons value changes, all attached handlers are fired with the new value
var softButtons = shipRef.child('softIO/buttons');
softButtons.once('value', function handleSoftButtonChanges(snapshot){
var newButtons = snapshot.val();
for (var button in newButtons) {
if (newButtons.hasOwnProperty(button)) {
softButtons.child(button).on('value', updateHandlers);
}
}
});
deferredChipBoard.resolve(shipRef);
});
q.all([deferredConfig.promise, deferredChipBoard.promise]).then(function(){
//console.log(arguments);
});
function isFunction(o){
return Object.prototype.toString.call(o) === '[object Function]';
}
function isArray(o){
return Object.prototype.toString.call(o) === '[object Array]';
}
function getIOHandler(name){
return IO_HANDLERS[name];
}
function updateHandlers(snapshot) {
var newButton = snapshot.val();
if(isArray(newButton.handlers)){
newButton.handlers.forEach(function (handlerObj) {
var handler = getIOHandler(handlerObj.action);
if(isFunction(handler)){
//Execute the handler in a new scope with the given arguments
handler.apply({}, [newButton.value].concat(handlerObj.args || []));
}
});
}
}