-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·102 lines (80 loc) · 1.79 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
#!/usr/bin/env node
var program = require('commander');
var sys = require('util');
program
.version('0.0.1')
.option('-i, --ttyinterface [interface]', 'Took this interface [/dev/ttyUSB0]', '/dev/ttyUSB0')
.parse(process.argv);
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort(program.ttyinterface, {
parser: serialport.parsers.readline("\n\r")
});
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('my other event', function (data) {
console.log(data);
});
});
var states = {
PAUSE: 0,
RUNNING: 1,
ENDING: 2,
END: 3
};
var game = {
state: states.PAUSE,
time: 0,
fails: 0
};
function sendGameData() {
io.sockets.emit('game', { game: game });
}
serialPort.on("data", function (data) {
switch(data) {
case "go":
console.log("Start");
game.time = 0;
game.state = states.RUNNING;
break;
case "stop":
game.state = states.ENDING;
break;
default:
switch(game.state) {
case states.ENDING:
game.fails = data;
console.log("Fertig");
console.log("Fails: " + game.fails);
console.log("Zeit: " + game.time + "ms");
game.state = states.END;
break;
case states.RUNNING:
if(data != "fail") {
game.time += parseInt(data);
}
break;
}
break;
}
sendGameData();
if(game.state == states.END) {
game.state = states.PAUSE;
game.fails = 0;
game.time = 0;
}
});