-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
executable file
·125 lines (100 loc) · 2.71 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
/* MagicMirror²
* Module: MMM-TMB
*
* By @jaumebosch
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const axios = require('axios');
const Log = require("../../js/logger");
module.exports = NodeHelper.create({
start: function () {
Log.log("Starting node helper for: " + this.name);
this.started = false;
this.config = null;
this.config = null;
},
getData: async function () {
const self = this;
let iBus = [];
let dataStop = {};
for (let i = 0; i < self.config.busStops.length; i++) {
let busStopCode = Number(self.config.busStops[i].busStopCode);
let busLine = null;
if (self.config.busStops[i].busLine){
busLine = self.config.busStops[i].busLine;
}
dataStop = await self.fetchDataStop(busStopCode);
dataStop.dataLines = await self.fetchDataLine(busStopCode, busLine);
if (dataStop.dataLines.length > 0) {
iBus.push(dataStop);
}
}
this.sendSocketNotification("DATA", iBus);
setTimeout(function () {
self.getData();
}, self.config.refreshInterval);
},
fetchDataStop: async function(busStopCode) {
const self = this;
let dataStop = {};
let busStopInfoUrl = "https://api.tmb.cat/v1/transit" +
"/parades/" + busStopCode +
"?app_id=" + self.config.appId +
"&app_key=" + self.config.appKey;
await axios.get(busStopInfoUrl)
.then(response => {
let stopInfoData = response.data.features[0].properties;
dataStop = {
busStopCode: stopInfoData['CODI_PARADA'],
busStopName: stopInfoData['NOM_PARADA'],
};
})
.catch(error => {
console.log(error);
});
return dataStop;
},
fetchDataLine: async function(busStopCode, busLine = null) {
const self = this;
let dataLine = [];
let busLineInfoUrl = "https://api.tmb.cat/v1/ibus";
if (busLine){
busLineInfoUrl += "/lines/" + busLine;
}
busLineInfoUrl += "/stops/" + busStopCode +
"?app_id=" + self.config.appId +
"&app_key=" + self.config.appKey;
await axios.get(busLineInfoUrl)
.then(response => {
let lineInfoData = response.data.data.ibus;
for (let k = 0; k < lineInfoData.length; ++k) {
let busLineCode = lineInfoData[k]['line']
if (busLine){
busLineCode = busLine
}
dataLine.push(
{
lineCode:busLineCode,
tInS:lineInfoData[k]['t-in-s'],
tInText:lineInfoData[k]['text-ca'],
tInMin:lineInfoData[k]['t-in-min'],
}
);
}
})
.catch(error => {
console.log(error);
});
return dataLine;
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'CONFIG' && this.started == false) {
this.config = payload;
this.sendSocketNotification("STARTED", true);
this.getData();
this.started = true;
}
}
});