-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
88 lines (77 loc) · 2.55 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
const NodeHelper = require('node_helper'); // eslint-disable-line import/no-unresolved
const request = require('request'); // eslint-disable-line import/no-extraneous-dependencies
module.exports = NodeHelper.create({
start() {
this._startedModules = {};
},
socketNotificationReceived(notificationName, payload) {
const self = this;
if (notificationName === 'MMM-ELVIRA.INIT') {
if (!self._startedModules[payload.moduleId]) {
self._init(payload.moduleId, payload.config);
self.sendSocketNotification('MMM-ELVIRA.STARTED', true);
self._startedModules[payload.moduleId] = true;
}
}
},
_init(moduleId, config) {
const self = this;
// Get the data immediately right after the module initialisation has completed.
setTimeout(() => {
self._getData(moduleId, config);
}, 0);
setInterval(() => {
self._getData(moduleId, config);
}, config.updateInterval);
},
_getData(moduleId, config) {
const self = this;
const requestOptions = {
method: 'POST',
url: 'https://jegy-a.mav.hu/IK_API_PROD/api/OfferRequestApi/GetOfferRequest',
headers: {
'Content-Type': 'application/json; charset=utf-8',
UserSessionId: "''",
Language: 'hu'
},
json: true,
body: {
offerkind: '1',
isOneWayTicket: true,
startStationCode: config.startStationCode,
endStationCode: config.endStationCode,
travelStartDate: new Date(),
passangers: [
{
passengerCount: 1,
passengerId: 0,
customerTypeKey: 'HU_44_025-065',
customerDiscountsKeys: []
}
],
selectedServices: [
// 52
],
selectedSearchServices: [],
isTravelEndTime: false,
innerStationsCodes: [],
isOfDetailedSearch: false
}
};
request(requestOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
self._processResponse(moduleId, body);
} else {
console.error(`MMM-Elvira Node helper: Failed to load data in the background. Error: ${error}. Status code: ${response.statusCode}. Body: ${body}`); // eslint-disable-line no-console
}
});
},
_processResponse(moduleId, responseBody) {
const payload = {
// eslint-disable-next-line object-shorthand -- Property shorthand may not be supported in older Node versions.
moduleId: moduleId,
data: responseBody
};
this.sendSocketNotification('MMM-ELVIRA.VALUE_RECEIVED', payload);
}
});