-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (67 loc) · 2.03 KB
/
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
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
'use strict';
// modules
var spawn = require('open'),
express = require('express'),
http = require('http'),
path = require('path'),
dgram = require('dgram'),
socketIO = require('socket.io'),
fs = require('fs');
// server
var app = express(),
httpServer = http.Server(app),
io = socketIO(httpServer),
udpServer = dgram.createSocket('udp4');
// config
var config = require('./config.json');
// configure static files
app.use(express.static(path.join(__dirname, 'public')));
// dynamic frontendlist
app.get('/frontendlist', function(req, res){
res.send(
JSON.stringify(
fs.readdirSync('public').filter(function(file){
return fs.statSync(path.join('public', file)).isDirectory() && file != 'bower_components';
})
)
);
});
// start the http server
httpServer.listen(config.webPort, function(){
console.log('HTTP Server listening on port ' + config.webPort);
spawn('http://127.0.0.1:'+config.webPort);
});
udpServer.on('listening', function () {
console.log('UDP Server listening on port ' + udpServer.address().port);
});
// converting the outgauge packages to JSON transmitted via Socket.IO
udpServer.on('message', function (message) {
var buff = new Buffer(message);
var outgaugeData = {
time: buff.readInt32LE(0),
car: buff.toString('ascii',4,7),
flags: buff.readUInt16LE(7),
plid: buff.readUInt8(9),
gear: buff.readUInt8(10),
speed: buff.readFloatLE(12),
rpm: buff.readFloatLE(16),
turbo: buff.readFloatLE(20),
engtemp: buff.readFloatLE(24),
fuel: buff.readFloatLE(28),
oilpressure: buff.readFloatLE(32),
oiltemp: buff.readFloatLE(36),
dashlights: buff.readInt32LE(40),
showlights: buff.readInt32LE(44),
throttle: buff.readFloatLE(48),
brake: buff.readFloatLE(52),
clutch: buff.readFloatLE(56),
// TODO: Those aren't completly right
display: [
buff.toString('ascii', 60, 76),
buff.toString('ascii', 76, 92)
]
};
io.emit('outgauge',outgaugeData);
});
// start the udp server
udpServer.bind(config.outgaugePort);