-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
66 lines (58 loc) · 1.71 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
var express = require('express'),
app = express(),
request = require('request'),
rp = require('request-promise'),
http = require('http').Server(app),
io = require('socket.io')(http);
let busses = [];
const PORT = process.env.PORT || 3000;
const IP = process.env.IP || '0.0.0.0';
const APPID = process.env.APPID;
let options = {
uri: 'http://developer.trimet.org/ws/v2/vehicles',
qs: {
APPID
},
json: true
};
app.use(express.static('public'));
function fetchTriMet(){
rp(options)
.then(function (data) {
let tempArr = [];
let vehicles = data.resultSet.vehicle;
let Vehicle = function(bus){
this.longitude = bus.longitude;
this.latitude = bus.latitude;
this.routeNum = bus.routeNumber;
this.title = bus.signMessageLong;
this.direction = bus.direction; //busDirection
this.id = bus.vehicleID; //vehicleID
this.delay = bus.delay; //delayV
this.type = bus.type; // rail or bus
};
if(vehicles.length > 0){
for(let i = 0, len = vehicles.length; i < len; i++){
if(vehicles[i].nextStopSeq === null) continue;
tempArr.push(new Vehicle(vehicles[i]));
}
}
busses = tempArr.slice();
io.emit('busses_moved', busses);
})
.catch(function (err) {
console.log('Error fetching data from Trimet: ',err);
});
}
setInterval(fetchTriMet, 5000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.emit('busses_moved', busses);
console.log('a user connected');
});
//start the server
http.listen(PORT, IP, function() {
console.log(`Server is up on ${PORT}:${IP}`);
});