-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.js
191 lines (163 loc) · 6.03 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
// Raspberry Pi GPIO Remote Control WebInterface
// NOTE: Raspberry Pi GPIO access requires sudo
// $ sudo node app.js 8080
// $ npm start (for testing, will use port 8080)
//
// to test websockets version use:
// $ sudo node app.js 8080 websockets
var flatiron = require('flatiron'),
path = require('path'),
ecstatic = require('ecstatic'),
app = flatiron.app,
rpi_gpio = require('rpi-gpio'),
tcpport = 8080,
flag_use_websockets = false;
var gpio_status = {
"GPIO_04": false,
"GPIO_17": false,
"GPIO_21": false,
"GPIO_22": false,
"GPIO_23": false,
"GPIO_24": false,
"GPIO_25": false,
"WEBSOCKETS": false
};
// Processing parameters
if(process.argv[2] !== undefined && process.argv[2].trim() !== '') {
tcpport = process.argv[2];
console.log("Tcp/ip port parameter defined: "+tcpport);
}
else {
console.log("Using default tcp/ip port: "+tcpport);
}
// websockets param
if(process.argv[3] !== undefined && process.argv[3].trim() !== '' && process.argv[3].trim() === 'websockets') {
flag_use_websockets = true;
console.log("Use WebSockets protocol!");
gpio_status["WEBSOCKETS"] = true;
}
else {
console.log("Using default Http REST protocol!");
gpio_status["WEBSOCKETS"] = false;
}
// maps / exports gpio pins
// (gpio_pin_number, gpio_pin_name)
rpi_gpio.setup(7, rpi_gpio.DIR_OUT, gpioWrite(7, 'GPIO_04', false));
rpi_gpio.setup(11, rpi_gpio.DIR_OUT, gpioWrite(11, 'GPIO_17', false));
rpi_gpio.setup(13, rpi_gpio.DIR_OUT, gpioWrite(13, 'GPIO_21', false));
rpi_gpio.setup(15, rpi_gpio.DIR_OUT, gpioWrite(15, 'GPIO_22', false));
rpi_gpio.setup(16, rpi_gpio.DIR_OUT, gpioWrite(16, 'GPIO_23', false));
rpi_gpio.setup(18, rpi_gpio.DIR_OUT, gpioWrite(18, 'GPIO_24', false));
rpi_gpio.setup(22, rpi_gpio.DIR_OUT, gpioWrite(22, 'GPIO_25', false));
// gpio write function
function gpioWrite(gpionumber, gpioname, value) {
return function() {
rpi_gpio.write(gpionumber, value, function(err) {
if (err) throw err;
console.log('Written to pin number '+gpionumber+' ('+gpioname+') value:'+value+'\r\n');
gpio_status[gpioname] = value;
});
};
};
// gpio unexport
function gpioClose() {
rpi_gpio.destroy(function() {
console.log('All pins unexported');
return process.exit(0);
});
}
// flatiron configurations
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
// flatiron plugins
app.use(flatiron.plugins.http);
// flatiron - ecstatic (server resources from directory - html, css, js, images)
app.http.before = [
ecstatic(__dirname + '/public')
];
//app.router.configure({ "strict": false });
// flatiron router - REST to get gpio_status
app.router.get('/gpiostatus', function () {
// responding back to the brower request
this.res.writeHead(200, {'Content-Type':'application/json'});
this.res.write(JSON.stringify(gpio_status));
this.res.end();
});
// flatiron router - REST GPIO commands
app.router.get('/gpio/:cmd', function (cmd) {
console.log('\r\nParsing REST gpio command: '+cmd);
// decode SPACE and ; chars (previously encoded in client)
var gpio = cmd;
//var gpio = cmd.replace(/_/g, " ");
gpio = gpio.replace(/--/g, ";");
console.log('Decoded gpio command: '+gpio);
// check if it contains a multiple gpio commands (will use ; as separator)
if(gpio.indexOf(";") != -1) {
var gpio_array=gpio.split(";");
var gpio_cmd;
for(var i=0, len=gpio_array.length; i<len; i++) {
gpio_cmd=unescape(gpio_array[i].replace(/\+/g, " ")); // url decode
console.log("GPIO"+i.toString()+"="+gpio_cmd);
gpioCmd(gpio_cmd);
}
}
else {
var gpio0=gpio;
gpio0=unescape(gpio0.replace(/\+/g, " ")); // url decode
console.log("GPIO="+gpio0);
gpioCmd(gpio0);
}
// responding back to the brower request
this.res.writeHead(200, {'Content-Type':'text/plain'});
this.res.write('ACK');
this.res.end();
});
// launch app on tcpoprt
app.start(tcpport);
console.log('Raspberry Pi GPIO WebInterface Server running on port '+tcpport);
// verify if websockets is active
if(flag_use_websockets === true) {
var io = require('socket.io').listen(app.server);
io.sockets.on('connection', function(socket) {
socket.on('gpiodata', function(data) {
console.log('Received GPIO Data: '+data.wsdata);
var gpio0=data.wsdata;
console.log("GPIO="+gpio0);
gpioCmd(gpio0);
// broadcast to all clients the new received update command for GPIO (cmd replication)
socket.broadcast.emit('gpionewstatus', { newdata: data.wsdata });
});
});
}
function gpioCmd(gpio0) {
var value = true;
if(gpio0.indexOf("RESET_") != -1)
value = false;
if(gpio0.indexOf("SET_GPIO_04") != -1 || gpio0.indexOf("RESET_GPIO_04") != -1) {
gpioWrite(7, 'GPIO_04', value).call();
}
else if(gpio0.indexOf("SET_GPIO_17") != -1 || gpio0.indexOf("RESET_GPIO_17") != -1) {
gpioWrite(11, 'GPIO_17', value).call();
}
else if(gpio0.indexOf("SET_GPIO_21") != -1 || gpio0.indexOf("RESET_GPIO_21") != -1) {
gpioWrite(13, 'GPIO_21', value).call();
}
else if(gpio0.indexOf("SET_GPIO_22") != -1 || gpio0.indexOf("RESET_GPIO_22") != -1) {
gpioWrite(15, 'GPIO_22', value).call();
}
else if(gpio0.indexOf("SET_GPIO_23") != -1 || gpio0.indexOf("RESET_GPIO_23") != -1) {
gpioWrite(16, 'GPIO_23', value).call();
}
else if(gpio0.indexOf("SET_GPIO_24") != -1 || gpio0.indexOf("RESET_GPIO_24") != -1) {
gpioWrite(18, 'GPIO_24', value).call();
}
else if(gpio0.indexOf("SET_GPIO_25") != -1 || gpio0.indexOf("RESET_GPIO_25") != -1) {
gpioWrite(22, 'GPIO_25', value).call();
}
};
// CTRL+C (sigint)
process.on( 'SIGINT', function() {
console.log("Unexport GPIO!");
gpioClose();
console.log( "Gracefully shutting down from SIGINT (Crtl-C)");
process.exit( )
});