-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
94 lines (77 loc) · 2.43 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
/* Magic Mirror Module: MMM-VTA-Live helper
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var urllib = require('urllib');
var moment = require('moment');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-VTA-Live helper, started...');
this.error = false;
this.body = null;
this.vtaTimes = new Map();
},
getVTAData: function(payload) {
var _this = this;
this.vtaTimes.clear();
this.error = false;
urllib.request(payload.url, {dataType: 'text'},function _json(err, data, res) {
if (!err) {
if (data) {
var vtaData = JSON.parse(data.trim());
var now = moment(vtaData.ServiceDelivery.ResponseTimestamp);
var visits = vtaData.ServiceDelivery.StopMonitoringDelivery.MonitoredStopVisit;
// Get the routes stopping here
for (var i in visits) {
var arrives = _this.durationInMins(now, visits[i].MonitoredVehicleJourney.MonitoredCall.ExpectedArrivalTime);
var times = _this.vtaTimes.get(visits[i].MonitoredVehicleJourney.LineRef);
if (times === undefined) {
_this.vtaTimes.set(visits[i].MonitoredVehicleJourney.LineRef, [visits[i].MonitoredVehicleJourney.PublishedLineName, arrives]);
} else {
times.push(arrives);
_this.vtaTimes.set(visits[i].MonitoredVehicleJourney.LineRef, times);
}
}
} else {
_this.error = true;
}
} else {
_this.error = true;
}
// We have the response figured out so lets fire off the notifiction
_this.sendSocketNotification('GOT-VTA-DATA', {url: payload.url, result: _this.error?null:_this.mapToObj(_this.vtaTimes)});
});
},
checkStatus: function(res) {
if (res.ok) { // res.status >= 200 && res.status < 300
return res;
} else {
throw 'Error';
}
},
durationInMins: function(startTime, endTime) {
var time = moment(endTime);
var duration = moment.duration(time.diff(startTime));
return Math.round(duration.asMinutes());
},
mapToObj: function(map) {
let obj = {};
map.forEach(function(value, key){
obj[key] = value
});
return obj;
},
socketNotificationReceived: function(notification, payload) {
// Check this is for us and if it is let's get the routes or the data
switch(notification) {
case 'GET-VTA-DATA':
this.getVTAData(payload);
break;
default:
break;
}
}
});