forked from Arve/MMM-Entur-tavle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
77 lines (69 loc) · 2.39 KB
/
node_helper.js
File metadata and controls
77 lines (69 loc) · 2.39 KB
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
let NodeHelper = require("node_helper");
let request = require("request");
module.exports = NodeHelper.create({
start: function(){
console.log("Starting node helper for: " + this.name);
},
getFullId: function(id, type, authority){
return `${authority}:${type}:${id}`;
},
prepareQuery: function(data){
let startTime = "";
let queryInit = "";
const fullId = this.getFullId(data.id, data.stopType, data.authorityId);
if (data.startDate) {
let startTime = `startTime: "${data.startTime}", `;
};
if (data.stopType === "StopPlace"){
queryInit = `stopPlace(id: "${fullId}")`;
} else if (data.stopType === "Quay"){
queryInit = `quay (id: "${fullId}")`;
};
return `{
${queryInit} {
id
name
estimatedCalls(${startTime} timeRange: 72100, numberOfDepartures: ${data.numResults}) {
aimedDepartureTime
expectedDepartureTime
actualDepartureTime
realtime
realtimeState
forBoarding
destinationDisplay {
frontText
}
serviceJourney {
journeyPattern {
line {
id
name
transportMode
publicCode
}
}
}
}
}
}`;
},
socketNotificationReceived: function(message, payload){
if (message === "GET_DEPARTURES"){
let options = {
url: payload.url,
method: "POST",
headers: {
"ETClientName": payload.ETClientName
},
json:{ query: this.prepareQuery(payload) },
};
var self = this;
request.post(options, function(error, response, message){
if (!error && (response.statusCode === 200 || response.statusCode === 304)) {
let path = (!!response.body.data.stopPlace)?response.body.data.stopPlace:response.body.data.quay;
self.sendSocketNotification("DEPARTURE_LIST", path);
}
});
}
}
});