-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (88 loc) · 2.64 KB
/
index.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
// testing api
var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
var request = require('request');
//const fetch = import('node-fetch');
//import fetch from 'node-fetch'
const fetchPromise = import('node-fetch').then(mod => mod.default)
const fetch = (...args) => fetchPromise.then(fetch => fetch(...args))
const secrets = require("./secrets.json");
const apikey = secrets.apikey;
const myStopSequence = secrets.myStopSequence;
const callOrigin = secrets.callOrigin;
const statusApiURL = secrets.statusApiURL;
const vehiclePositionsApiURL = secrets.vehiclePositionsApiURL;
const myRouteID = secrets.myRouteID;
const statusCodes = {
0: "INCOMING_AT",
1: "STOPPED_AT",
2: "IN_TRANSIT_TO",
}
get57Pos();
//getServiceStatus();
function getServiceStatus(){
fetch(statusApiURL, {
method: 'get',
headers: {'apikey': apikey,
'origin': callOrigin },
})
.then(res => res.json())
.then(json => console.log(JSON.stringify(json, null, " ")));
}
function get57Pos(){
var requestSettings = {
method: 'GET',
url: vehiclePositionsApiURL,
headers : {
'apikey': apikey
},
encoding: null
};
var closestBus = false;
var closestCurrentSequence = 0;
var closesStatus = 0;
request(requestSettings, function (error, response, body) {
if (!error && response.statusCode == 200) {
var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
feed.entity.forEach(function(entity) {
var routeId = entity.vehicle.trip.routeId;
if (routeId == myRouteID){
var currentStopSequence = entity.vehicle.currentStopSequence;
if(currentStopSequence <= myStopSequence && currentStopSequence > closestCurrentSequence){
closestCurrentSequence = currentStopSequence;
closesStatus = entity.vehicle.currentStatus;
closestBus = entity;
}
}
});
console.log("closest bus");
console.log(closestBus);
console.log("closest bus is " + statusCodes[closesStatus]+ " stop " + closestCurrentSequence );
}else{
console.log(error + " : status code " +response.statusCode);
}
});
}
/*
FeedEntity {
id: '40237',
vehicle: VehiclePosition {
trip: TripDescriptor {
tripId: '243237129',
startTime: '16:58:00',
startDate: '20211203',
routeId: '112'
},
position: Position {
latitude: 45.424156188964844,
longitude: -73.6461181640625,
bearing: 288,
speed: 10.000080108642578
},
currentStopSequence: 39,
currentStatus: 2,
timestamp: Long { low: 1638570336, high: 0, unsigned: true },
vehicle: VehicleDescriptor { id: '40237' },
occupancyStatus: 1
}
}
*/