-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
111 lines (83 loc) · 3.8 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
var NodeHelper = require("node_helper");
const http = require('http');
const xml2js = require('xml2js');
const parser = new xml2js.Parser({ attrkey: "ATTR" });
var fs = require("fs");
module.exports = NodeHelper.create({
start: function(){
console.log("Starting stop fetcher for: " + this.name);
},
socketNotificationReceived: function(notification, payload){
var self = this;
switch (notification){
case "EMT_LANGUAGE":
var lFile = "./modules/MMM-EMTValencia/translations/" + payload + ".json";
var lCont;
if (fs.existsSync(lFile)){
lCont = fs.readFileSync("./modules/MMM-EMTValencia/translations/" + payload + ".json");
} else {
lCont = fs.readFileSync("./modules/MMM-EMTValencia/translations/en.json");
}
this.langFile = JSON.parse(lCont);
break;
case "EMT_INIT":
this.config = payload;
setInterval(function(){
self.updateBusInfo();
}, this.config.updateInterval * 1000);
break;
default:
console.log(this.name + ": Unknown notification received (helper).");
}
},
updateBusInfo: function(){
console.log(this.name + ': Received request to update info.');
var self = this;
var url = "";
if (this.config.stopLine == null){
url = "http://servicios.emtvalencia.es/pre/estimaciones/estimacion.php?parada=" + this.config.stopNumber;
} else {
url = "http://servicios.emtvalencia.es/pre/estimaciones/estimacion.php?parada=" + this.config.stopNumber + "&linea=" + this.config.stopLine;
}
let req = http.get(url, function(res) {
let data = '';
res.on('data', function(stream) {
data += stream;
});
res.on('end', function(){
parser.parseString(data, function(error, result) {
if(error === null) {
var buses = []
p = result["estimacion"]["solo_parada"][0]["bus"]
for (var i=0; i<p.length; i++){
elem = p[i]
// console.log(self.langFile)
// console.log(elem);
resp = elem["linea"] + " - " + elem["destino"] + " | ";
if ("minutos" in elem && elem["minutos"][0].trim() != ""){
resp += elem["minutos"];
} else if ("horaLlegada" in elem && elem["horaLlegada"][0].trim() != ""){
resp += elem["horaLlegada"];
} else if ("error" in elem && elem["error"][0].trim() != ""){
if (elem["error"] == "SENSE ESTIMACIONS"){
resp += self.langFile["NO_DATA"];
} else if (elem["error"] == "LÍNIA DESVIADA"){
resp += self.langFile["DIVERTED_LINE"];
} else {
resp += elem["error"];
}
} else {
resp += self.langFile["GENERIC_ERROR"];
}
buses.push(resp);
}
self.sendSocketNotification("BUS_INFO", {data: buses});
}
else {
console.log(error);
}
});
});
});
},
});