-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-CanadianPublicWeatherAlerts.js
141 lines (110 loc) · 4.63 KB
/
MMM-CanadianPublicWeatherAlerts.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
135
136
137
138
139
140
141
/* Magic Mirror
* Module: CanadianPublicWeatherAlerts
*
* By Alex Souchereau http://github.com/aSouchereau
* MIT Licensed.
*/
Module.register('MMM-CanadianPublicWeatherAlerts', {
defaults: {
lang: 'en',
regions: [
{
code: ""
}
],
updateInterval: 60000, // once every minute (ms)
animationSpeed: 1000, // one second (ms)
displayInterval: 5000, // displays each alert for 5 seconds
showNoAlertsMsg: false, // Displays "No alerts in Effect" message for each region if true
periodicSync: false, // If enabled, module will send config to node helper every user-defined interval (useful for server only setups)
syncInterval: 600000, // once every ten minutes (ms)
apiBase: 'weather.gc.ca'
},
getStyles() {
return ["MMM-CanadianPublicWeatherAlerts.css"];
},
getScripts() {
return ["moment.js"];
},
start() {
Log.log("Starting module: " + this.name);
this.loaded = false;
this.currentAlerts = [];
moment.locale(this.config.lang);
this.sendSocketNotification('CPWA_CONFIG', this.config);
if (this.config.periodicSync) {
setInterval( () => { this.syncClient() }, this.config.syncInterval);
}
this.scheduleUpdate(this.config.updateInterval);
},
// Sends update request to server every configured interval
scheduleUpdate(delay) {
this.sendSocketNotification('CPWA_REQUEST_UPDATE', true);
setInterval( () => { this.sendSocketNotification('CPWA_REQUEST_UPDATE', true) }, delay);
},
// Actions to be performed when a periodic sync is requested
syncClient() {
console.log("[" + this.name + "] Syncing with server");
this.sendSocketNotification('CPWA_CONFIG', this.config);
this.sendSocketNotification('CPWA_REQUEST_UPDATE', true);
},
getDom() {
let wrapper = document.createElement("div");
let innerElem = document.createElement("div");
if (!this.loaded) {
innerElem.innerHTML = "";
}
else {
innerElem.innerHTML = this.AlertTitle + this.AlertRegion + this.AlertTime;
}
wrapper.appendChild(innerElem);
return wrapper;
},
// Sets element variables to the current alert being displayed
displayAlerts() {
let timePrefix = (this.config.lang === "fr" ? "Publié" : "Issued"); // Sets prefix depending on configured language
let displayedAlert = this.currentAlerts[this.currentAlertID];
let title = displayedAlert['title'][0].split(", "); // Splits title so region can be a separate element
this.AlertTitle = `<div class="${this.name} alert-title bright">${title[0]}</div>`
this.AlertRegion = `<div class="${this.name} alert-region">${title[1]}</div>`
this.AlertTime = `<div class="${this.name} alert-time">${timePrefix} ${moment(displayedAlert['updated'][0], "YYYY-MM-DDTHH:mm:ssZ").fromNow()}</div>`
this.updateDom(this.config.animationSpeed);
},
// Iterates through currentAlerts, used instead of for loop to control speed
startDisplayTimer() {
this.currentAlertID = 0;
clearInterval(this.timer);
this.timer = setInterval( () => {
this.loaded = true;
this.displayAlerts();
// Check to see if were at the last alert
if (this.currentAlertID === this.currentAlerts.length - 1) {
this.startDisplayTimer(); // Restart Timer
} else {
this.currentAlertID++; // Increment to next alert
} }, this.config.displayInterval + this.config.animationSpeed
); // Time between each alert (speed + interval is used to prevent overlapping animations)
},
socketNotificationReceived(notification, payload) {
if (notification === "CPWA_STARTED") { // Updates dom after node_helper receives config
this.updateDom();
}
else if (notification === "CPWA_UPDATE") {
this.currentAlerts = [];
if (payload.length !== 0) {
// If only one alert, disable transition animation
if (payload.length === 1) {
this.config.animationSpeed = 0;
}
this.currentAlerts = payload;
this.startDisplayTimer();
} else {
this.AlertTitle = "";
this.AlertRegion = "";
this.AlertTime = "";
this.updateDom();
Log.log(`[${this.name}] No Alerts in effect for configured regions`);
}
}
}
});