This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
server.js
97 lines (70 loc) · 2.78 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var path = require('path');
var http = require('http');
//var nko = require('nko')('R8N+nroFbZPS6D4n');
var express = require('express');
var morgan = require('morgan');
var errorhandler = require('errorhandler');
//var ejs = require('ejs');
//var io = require('socket.io');
//var events = require(path.join(__dirname,"lib","event-simulator","event-simulator.js"));
var gtfsEvents = require(path.join(__dirname,"lib","gtfs-parser","gtfs-timetable-parser.js"));
var mapDataGenerator = require(path.join(__dirname,"lib","map-data-generator","map-data-generator.js"));
var PathNormalizer = require(path.join(__dirname,"lib","path-normalizer","path-normalizer.js"));
var Gtfs = require(path.join(__dirname, "lib", "gtfs-parser", "gtfs-loader"));
var app = express();
var server = http.createServer(app)
var io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/public'));
app.use(morgan());
app.use(errorhandler({dumpExceptions: true, showStack: true}));
app.set('views', __dirname + '/views');
app.engine('.html', require('ejs').__express);
//app.register('.html', ejs);
app.set('view engine', 'html');
var gtfsdir = "ulm";
var gtfs = Gtfs(process.env.GTFS_PATH || path.join(__dirname,"gtfs",gtfsdir), function(gtfsData){
mapDataGenerator.gen(gtfsData, process.env.GTFS_PATH || path.join(__dirname,"gtfs",gtfsdir), function(mapData) {
//calculate normalized shapes
var pathNormalizer = PathNormalizer(mapData.getShapes());
console.dir(mapData.getTrips());
require(path.join(__dirname, '/routes/site'))(app, mapData.getStops(), mapData.getShapes(),mapData.getTrips());
io.sockets.on('connection', function (socket) {
});
/* event simulator, throws an event every 10 secs. */
gtfsEvents.init(gtfsData, 10000, function(data) {
var trips = data.trips;
var pushData = {};
for(var i in trips){
if(trips.hasOwnProperty(i)){
var delta = (trips[i].progressThen - trips[i].progressNow) / 10;
//console.log(delta);
var pointList = [];
var shapeId = mapData.getShapeIdFromTripId(i);
if (!shapeId) continue;
for(var j = 0;j<10;j++){
var idx = Math.floor((trips[i].progressNow + j*delta)*1000);
if(idx === 1000 || idx ===0){
pointList.push([0,0]);
}
else{
pointList.push(pathNormalizer.getNormalizedPath(shapeId)[idx]);
}
}
pushData[i] = pointList;
}
}
/*
var p = Math.floor(step.progress * 10);
step['pointList'] = [];
step['foo'] = p;
for(var i = 0;i<10;i++){
step['pointList'].push(pathNormalizer.getNormalizedPath("87001")[p+i]);
}
*/
io.sockets.emit('event', pushData);
});
server.listen(process.env.PORT || 7777);
//var appServer = app.listen(parseInt(process.env.PORT) || 7777);
console.log('Listening on ' + server.address().port);
});
});