-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·236 lines (202 loc) · 6.49 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var config = require('./config.json');
// Create express app & add socket.io
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/client'));
app.use(express.vhost(config.domain, app));
app.use(express.vhost(config.domain1, app));
var http = require('http');
var server = http.createServer(app).listen(config.port);
var io = require('socket.io').listen(server);
// Connect to serial port
var com = require("serialport");
var serialPort = new com.SerialPort(config.serialPort, {
baudrate: 38400,
parser: com.parsers.readline("\n\r")
//parser: com.parsers.readline('\r\n')
});
// On connection to serial port.
serialPort.on('open',function() {
console.log('EJS Connected');
});
// On incomming data from serial port.
serialPort.on('data', function(data) {
if (config.debug == true) {
console.log(data);
}
if (!data.length) { return; }
var EJSdata = EJS.processData(data);
if (config.debug == true) {
console.log(EJSdata);
}
switch(EJSdata.eventType) {
case 'u':
console.log("Standing by...");
break;
case 'd':
console.log("Sensor Data...");
EJS.processSensorData(EJSdata);
break;
case 'r':
EJS.reset();
console.log("Resetting...");
io.sockets.emit('raceReset', {});
break;
case 's':
console.log("Starting...");
io.sockets.emit('raceStart', {});
break;
case 'z':
console.log("Race begin...");
io.sockets.emit('raceBegin', {});
break;
case 't':
EJS.stopRace(EJSdata);
console.log("Race Stopped...");
io.sockets.emit('raceFinish', {});
break;
}
});
var EJSheat = {
lane1 : {
totalTime : 0,
lastUpdate: 0,
lastPassCalc : 0,
lastPass : 0,
lastPassTime : 0,
dogIn : false,
totals : {
total : 0,
pass : 0,
perfect : 0
},
splits : []
},
lane2 : {
totalTime : 0,
lastUpdate: 0,
lastPassCalc : 0,
lastPass : 0,
lastPassTime : 0,
dogIn : false,
totals : {
total : 0,
pass : 0,
perfect : 0
},
splits : []
}
};
// EJS object to handle sensor data and race info
var EJS = {
startSequenceTime : config.startSequenceTime,
race : JSON.parse(JSON.stringify(EJSheat)),
processData : function(data) {
return {
eventType : data.substr(0, 1),
timeReading : data.substr(1, 6),
timeReadingMS : EJS.h2d(data.substr(1, 6)),
status : EJS.h2d(data.substr(7, 2)),
sensors : data.substr(9, 2),
mod : EJS.h2d(data.substr(11, 2)),
switches : EJS.h2d(data.substr(13, 2))
}
},
processSensorData : function(data) {
if (config.debug == true) { console.log(this.race) };
switch (data.sensors) {
// Left lane front sensors.
case '10':
case '11':
this.addSplitForLane('lane1', data.timeReadingMS);
break;
// Right lane front sensors.
case '40':
case '44':
this.addSplitForLane('lane2', data.timeReadingMS);
break;
}
},
addSplitForLane : function(lane, time) {
var lastUpdate = this.race[lane].lastUpdate;
var lastPassCalc = this.race[lane].lastPassCalc;
var update = false;
var uiLane = lane == 'lane1' ? 'L' : 'R';
if (time > lastUpdate + 2000 || lastUpdate == 0) {
// Set lastUpdate time as the race start if first dog is late.
if (lastUpdate == 0 && time > EJS.startSequenceTime) {
this.race[lane].lastUpdate = EJS.startSequenceTime;
}
else {
this.race[lane].lastUpdate = time;
}
update = true;
// If the start time then remove EJS start sequence from time.
if (lastUpdate == 0) {
// Store that calculated last pass.
this.race[lane].lastPassCalc = time;
time = time - EJS.startSequenceTime;
// Don't update entire dog line for starts but update last pass.
update = false;
var startTime = this.convertTime(time - lastUpdate);
this.race[lane].lastPassTime = startTime;
io.sockets.emit('receiveNewStartTime', { lane: uiLane, time: startTime });
}
else if (lastUpdate < EJS.startSequenceTime) {
lastUpdate = EJS.startSequenceTime;
}
var splitTime = this.convertTime(time - lastUpdate);
this.race[lane].splits.push(splitTime);
}
// Late pass, calculate.
else if (time > (lastUpdate + 50) && time > (lastPassCalc + 3000)) {
this.race[lane].lastPassCalc = time;
this.race[lane].lastPassTime = this.convertTime(time - lastUpdate);
}
if (update) {
// Update webclient.
var lastPassTime = this.race[lane].lastPassTime > 0 ? this.race[lane].lastPassTime : 0;
var perfectTime = this.roundNumber(splitTime - lastPassTime);
io.sockets.emit('receiveNewHeatTime', { lane: uiLane, time: splitTime, pass: this.race[lane].lastPassTime, perfect: perfectTime });
this.race[lane].lastPassTime = 0;
}
},
stopRace : function(data) {
// Ensure minimum time of 0.
this.race.lane1.lastUpdate = this.race.lane1.lastUpdate < EJS.startSequenceTime ? EJS.startSequenceTime : this.race.lane1.lastUpdate;
this.race.lane2.lastUpdate = this.race.lane2.lastUpdate < EJS.startSequenceTime ? EJS.startSequenceTime : this.race.lane2.lastUpdate;
// Get total times.
this.race.lane1.TotalTime = this.convertTime(this.race.lane1.lastUpdate, true);
this.race.lane2.TotalTime = this.convertTime(this.race.lane2.lastUpdate, true);
// Update webclient.
io.sockets.emit('receiveHeatTotalTime', { lane: 'L', time: this.race.lane1.TotalTime, pass: '0.00', perfect: '0.00' });
io.sockets.emit('receiveHeatTotalTime', { lane: 'R', time: this.race.lane2.TotalTime, pass: '0.00', perfect: '0.00' });
},
reset : function() {
this.race = JSON.parse(JSON.stringify(EJSheat));
io.sockets.emit('raceReset');
},
convertTime : function(time, removeStart) {
var removeStart = typeof removeStart !== 'undefined' ? removeStart : false;
if (removeStart) {
time = time - EJS.startSequenceTime;
}
time = time / 1000;
if (config.debug == true) {
console.log(time);
}
return this.roundNumber(time);
},
h2d : function(h) {
return parseInt(h,16);
},
roundNumber : function(num) {
return Number(Math.round(num+'e2')+'e-2').toFixed(2);
}
};
// io.sockets.on('connection', function (socket) {
// socket.emit('receiveNewHeatTime', { lane: 'L', time: '3.94', pass: '0.03', perfect: '3.91' });
// socket.on('my other event', function (data) {
// console.log(data);
// });
// });