-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathapp.js
executable file
·194 lines (150 loc) · 5.18 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
'use strict';
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server), // jshint ignore:line
Board = require('firmata'),
program = require('commander'),
fs = require('fs'),
path = require('path');
// Parse command-line args
var directory, index, program;
function makeAbsolute(filepath){
return path.isAbsolute(filepath) ? filepath : process.cwd() + '/' + filepath;
}
// This both defines options as well as what prints when '--help' is called.
program
.description('Run `node app.js` plus these flags to configure your server.')
.option('-d, --dir <d>', 'Set base directory for server')
.option('-f, --file <f>', 'Set file to use for index page')
.option('-p, --ufilepath <p>',
'Path to file containing user-defined server-side listeners.')
.option('-n, --ufilename <n>',
'Path, inluding file name, to user-defined server-side listeners.')
.parse(process.argv);
exports.program = program;
exports.directory = directory = program.dir || __dirname;
exports.index = index = program.file || (directory + '/index.html');
// Setup server, sockets, and events
server.listen(8000);
app.use(express.static(directory));
console.log('server starting');
app.get('/', function(req, res) {
res.sendFile(makeAbsolute(index));
});
// App code: Define as export for IDE, call at end of file for non-IDE usage
var setup = exports.setup = function(io) {
var board;
io.of('/sensors').on('connect', function(socket) {
console.log('connected');
exports.socket = socket;
// Error handling
socket.on('error', function(err){
console.log(err);
});
// Board setup
socket.on('board object', function(data) {
function init() {
console.log('board object caught', data);
initializeSpecialFuncs(board);
socket.emit('board ready', { analogArr: board.analogPins });
}
// If the board has already been initialized in firmata, it won't
// call the callback again on client reload; this way the init
// functions are called without restarting the whole proces
if (!board) {
board = new Board(data.port, function(err) {
if (err) {
throw new Error(err);
}
init();
});
} else {
init();
}
});
// Pin setup
socket.on('pin object', function(data){
console.log('pin object caught', data);
// Digital pins are set to INPUT or OUTPUT in firmata
data.mode === 'digital' ?
board.pinMode(data.pin, board.MODES[data.direction.toUpperCase()]) :
board.pinMode(data.pin, board.MODES[data.mode.toUpperCase()]);
});
// Action functions:
// The primary action function formats the read & write functions & sends
// these to firmata
socket.on('action', function(data){
// console.log('action data', data);
var argument = data.arg;
if (argument){
// If it is digtalWrite, augment the argument with
// `board` to match firmata call
if (argument && (argument === 'HIGH' || argument === 'LOW')) {
board[data.action](data.pin, board[argument]);
} else {
board[data.action](data.pin, argument);
}
// Otherwise it is read with no argument, set pin.val on update
} else if (data.type === 'read') {
board[data.action](data.pin, function(val){
socket.emit('return val' + data.pin, { val: val });
});
}
});
// Special functions
function initializeSpecialFuncs(board) {
// LED
var led = require('./lib/led.js');
led.blink(board, socket);
led.fade(board, socket);
// RGB
var rgb = require('./lib/rgb.js');
rgb.write(board, socket);
rgb.read(board, socket);
rgb.blink(board, socket);
rgb.fade(board, socket);
// Servo
var servo = require('./lib/servo.js');
servo.range(board, socket);
servo.sweep(board, socket);
// Piezo
var piezo = require('./lib/piezo.js');
piezo.tone(board, socket);
// User defined, if present
var filepath;
if (program.ufilename) {
filepath = program.ufilename;
} else if (program.ufilepath) {
var userpath = program.ufilepath || __dirname;
filepath = userpath + '/user.js';
}
if (filepath) {
filepath = path.normalize(filepath);
fs.stat(filepath, function(err, stats){
if (err == null) {
var reqPath = makeAbsolute(filepath);
var user = require(reqPath),
keys = Object.keys(user);
keys.forEach(function(key){
user[key](board, socket);
});
} else if (err.code === 'ENOENT'){
throw new Error(filepath +
' does not seem to exist. Maybe it is a ghost.');
} else {
console.log(err);
}
});
}
}
// Serial does not require firmata board
var serial = require('./lib/serial.js');
serial.init(socket);
serial.read(socket);
serial.write(socket);
serial.list(socket);
});
};
setup(io);