-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (35 loc) · 1010 Bytes
/
server.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
const PORT = 3002;
const min = 50;
const max = 250;
const http = require('http');
const connect = require('connect');
const connectStatic = require('serve-static');
const exec = require('child_process').exec;
const command = (servo, progress) => {
servo = servo || 0;
if (isNaN(progress) || typeof progress !== 'number') {
progress = 50;
}
if (progress < 5) {
progress = 5;
} else if (progress > 95) {
progress = 95;
}
const cmd = 50 + 200*progress/100;
return `echo ${servo}=${cmd} > /dev/servoblaster`;
};
const app = connect();
app.use(connectStatic('dist'))
const server = http.createServer(app);
server.listen(PORT, console.log.bind(this, 'server listening on port %s', PORT));
const io = require('socket.io')(server);
io.on('connection', client => {
client.on('progress', (servo, progress) => {
exec(command(servo, progress), (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
});
});
});