-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-CTA.js
134 lines (112 loc) · 2.74 KB
/
MMM-CTA.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
126
127
128
129
130
131
132
133
134
/* global Module */
/* Magic Mirror
* Module: MMM-CTA
*
* By Jordan Welch
* MIT Licensed.
*/
Module.register('MMM-CTA', {
defaults: {
updateInterval: 60000,
trainApiKey: null,
busApiKey: null,
maxResultsBus: 5,
maxResultsTrain: 5,
routeIcons: true,
suffixStyle: 'long',
showHeaders: true,
stops: [],
},
requiresVersion: '2.28.0',
loading: true,
start () {
Log.info(`Starting module: ${this.name}`);
const self = this;
this.getData();
setInterval(() => {
self.getData();
}, this.config.updateInterval);
},
getData () {
this.sendSocketNotification('MMM-CTA-FETCH', {
trainApiKey: this.config.trainApiKey,
busApiKey: this.config.busApiKey,
stops: this.config.stops,
maxResultsTrain: this.config.maxResultsTrain,
maxResultsBus: this.config.maxResultsBus,
});
},
getTemplate () {
return 'templates/MMM-CTA.njk';
},
getTemplateData () {
return {
loading: this.loading,
routeIcons: this.config.routeIcons,
showHeaders: this.config.showHeaders,
stops: this.data.stops?.map((stop) => ({
...stop,
arrivals: stop.arrivals?.map((arrival) => ({
direction: arrival.direction,
routeColor: arrival.routeColor ? `cta-${arrival.routeColor}` : '',
arrival: arrival.arrival
? this.formatMinutes(arrival.arrival)
: this.getMinutesUntil(arrival.time),
})),
})),
};
},
getScripts () {
return [];
},
getStyles () {
return [
'font-awesome.css',
'MMM-CTA.css',
];
},
getTranslations () {
return {
en: 'translations/en.json',
es: 'translations/es.json',
};
},
socketNotificationReceived (notification, payload) {
if (notification !== 'MMM-CTA-DATA') {
return;
}
this.data.stops = payload.stops;
this.loading = false;
this.updateDom(300);
},
getMinutesUntil (arrivalTime) {
const now = new Date();
const diffInMilliseconds = new Date(arrivalTime) - now;
const diffInMinutes = Math.floor(diffInMilliseconds / 1000 / 60);
return this.formatMinutes(diffInMinutes);
},
formatMinutes (minutes) {
const minutesInt = parseInt(minutes, 10);
if (Number.isNaN(minutesInt)) {
return minutes;
}
if (minutesInt === 0) {
return 'DUE';
}
return this.minutesWithSuffix(minutesInt);
},
minutesWithSuffix (minutes) {
switch (this.config.suffixStyle) {
case 'none':
return minutes.toString();
case 'short':
return `${minutes.toString()}m`;
case 'long':
default:
if (minutes === 1) {
return `${minutes.toString()} min`;
}
return `${minutes.toString()} mins`;
}
},
});