-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
73 lines (62 loc) · 1.69 KB
/
node_helper.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
var NodeHelper = require('node_helper');
const Mta = require('mta-gtfs');
var reloadTimer = null;
module.exports = NodeHelper.create({
defaults: {
fetchInterval: 15 * 60 * 1000,
},
start: function() {
console.log('MTA helper started...');
this.mta = false;
},
getMTAData: function(self, config) {
if (!this.mta) {
console.log(config);
this.mta = new Mta({
key: console.api_key
});
}
if (!config.fetchInterval) {
config.fetchInterval = this.defaults.fetchInterval;
}
var timestamp = Date.now().toString();
this.mta.status()
.then(data => {
let lines = data.subway;
if (config.lines) {
lines = data.subway.filter(line => {
return config.lines.includes(line.name);
});
}
this.sendSocketNotification('LINE_DATA', {
data: lines,
});
})
.catch(err => {
console.log(err);
});
this.scheduleTimer(self, config);
},
startClock: function(nextTrainTimes) {
var currentTime = new Date().getTime();
var nextDepartTime = 0;
for (var i = 0; i < nextTrainTimes.length; i++) {
var nextTrain = nextTrainTimes[i];
if (nextTrain.departureTime * 1000 > currentTime) {
nextDepartTime = new Date(nextTrain.departureTime * 1000);
break;
}
}
},
scheduleTimer: function(self, config) {
clearTimeout(reloadTimer);
reloadTimer = setTimeout(() => {
this.getMTAData(self, config);
}, config.fetchInterval);
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_MTA_STATUS') {
this.getMTAData(this, payload);
}
},
});