Skip to content

Commit

Permalink
Merge pull request #2 from aSouchereau/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
aSouchereau authored Feb 11, 2023
2 parents c76a99a + 73c1563 commit 6ea429d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
48 changes: 28 additions & 20 deletions MMM-CanadianPublicWeatherAlerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Module.register('MMM-CanadianPublicWeatherAlerts', {
updateInterval: 60000, // once every minute (ms)
animationSpeed: 1000, // one second (ms)
displayInterval: 5000, // displays each alert for 5 seconds
showNoAlerts: false, // Displays "No alerts in Effect" message for each region if true
showNoAlertsMsg: false, // Displays "No alerts in Effect" message for each region if true

apiBase: 'https://weather.gc.ca/rss/battleboard/'

Expand All @@ -33,13 +33,13 @@ Module.register('MMM-CanadianPublicWeatherAlerts', {
this.loaded = false;
this.currentAlerts = [];
moment.locale(this.config.lang); // Determines whether time since element is in english or french
this.sendSocketNotification('CONFIG', this.config); // Sends config to node helper, so node helper can produce initial data
this.sendSocketNotification('CPWA_CONFIG', this.config); // Sends config to node helper, so node helper can produce initial data
this.scheduleUpdate(this.config.updateInterval);
},
// Sends update request to server every configured interval
scheduleUpdate(delay) {
this.sendSocketNotification('REQUEST_UPDATE', true); // Sends on initial load
setInterval( () => { this.sendSocketNotification('REQUEST_UPDATE', true) }, delay); // sends update request
this.sendSocketNotification('CPWA_REQUEST_UPDATE', true); // Sends on initial load
setInterval( () => { this.sendSocketNotification('CPWA_REQUEST_UPDATE', true) }, delay); // sends update request
},
getDom() {
let wrapper = document.createElement("div");
Expand All @@ -55,45 +55,53 @@ Module.register('MMM-CanadianPublicWeatherAlerts', {
},
// Sets element variables to the current alert being displayed
displayAlerts() {
let alert = this.currentAlerts[this.currentAlertID];
let title = alert['title'][0].split(", ");
let timePrefix = (this.config.lang === "fr" ? "Publié" : "Issued"); // Sets prefix depending on configured language
let cAlert = this.currentAlerts[this.currentAlertID];
let title = cAlert['title'][0].split(", "); // Splits title so region can be a separate element
// Sets element vars
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">Issued ${moment(alert['updated'][0], "YYYY-MM-DDTHH:mm:ssZ").fromNow()}</div>`
// Check to see if were at the last alert
if (this.currentAlertID === this.currentAlerts.length - 1) {
this.startDisplayTimer(); // Restart Timer

} else {
this.currentAlertID++;
}
this.AlertTime = `<div class="${this.name} alert-time">${timePrefix} ${moment(cAlert['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; // Makes sure we start from first index
clearInterval(this.timer); // Removed old timer
clearInterval(this.timer); // Removes old timer
// Starts new timer
this.timer = setInterval( () => {
this.loaded = true; // Sets loaded to true so getDom can create elements
this.displayAlerts();
this.updateDom(this.config.animationSpeed);
}, this.config.displayInterval + this.config.animationSpeed);
// 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 === "STARTED") {

if (notification === "CPWA_STARTED") { // Updates dom after node_helper receives config
this.updateDom();
} else if (notification === "UPDATE") {
} else if (notification === "CPWA_UPDATE") { // Received every "updateInterval"
this.currentAlerts = [];
// If notification payload contains alerts
if (payload.length !== 0) {
// If only one alert, disable transition animation
if (payload.length === 1) {
this.config.animationSpeed = 0; // If only one alert, remove transition animation
this.config.animationSpeed = 0;
}
// Updates current alerts and resets display timer
this.currentAlerts = payload;
this.startDisplayTimer();
} else {
// Clear displayed alerts and display console message
this.AlertTitle = "";
this.AlertRegion = "";
this.AlertTime = "";
this.updateDom();

Log.log(`[${this.name}] No Alerts in effect for configured regions`);
}

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Gets weather watches, warnings, and advisories for user specified regions. Using
| `updateInterval` | Sets the interval between each alert update. By default, this module will get new alert data every `60 seconds`. | 60000 | |
| `displayInterval` | Sets the amount of time each alert is displayed for. Default value is `5 seconds`. | 5000 | |
| `animationSpeed` | Sets the speed of the cross-fade between each alert. Set to `0` to disable. Default value is `1 second`. | 1000 | |
| `showNoAlertsMsg` | If set to true, the module will display a "No alerts in effect" message for each region. | false | |


### Regions
Environment Canada publishes weather alerts for regions across the country. Regions are represented by a region code that follows the format of `aa00`.
Expand Down
Binary file removed img/CssExample.png
Binary file not shown.
10 changes: 5 additions & 5 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = NodeHelper.create({
!e.title.includes(invalidTitleFr)
);
if (this.config.showNoAlerts) {
this.sendSocketNotification("UPDATE", validEntries);
this.sendSocketNotification("CPWA_UPDATE", validEntries);
} else {
// Filter out unimportant alert entries
let noAlertsEn = "No alerts in effect";
Expand All @@ -53,7 +53,7 @@ module.exports = NodeHelper.create({
!e.summary[0]._.includes(noAlertsEn) &&
!e.summary[0]._.includes(noAlertsFr)
);
this.sendSocketNotification("UPDATE", filteredEntries);
this.sendSocketNotification("CPWA_UPDATE", filteredEntries);
}
}
});
Expand Down Expand Up @@ -95,11 +95,11 @@ module.exports = NodeHelper.create({
});
},
socketNotificationReceived(notification, payload) {
if (notification === 'CONFIG' && this.started == false) {
if (notification === 'CPWA_CONFIG' && this.started == false) {
this.config = payload;
this.sendSocketNotification("STARTED", true);
this.sendSocketNotification("CPWA_STARTED", true);
this.started = true;
} else if (notification === 'REQUEST_UPDATE') {
} else if (notification === 'CPWA_REQUEST_UPDATE') {
this.startUpdate();
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "MMM-CanadianPublicWeatherAlerts",
"version": "1.0.0",
"version": "1.0.2",
"displayName": "MMM-CanadianPublicWeatherAlerts",
"description": "",
"description": "Gets weather watches, warnings, and advisories for user specified regions. Using data published by Environment Canada. ",
"main": "",
"repository": {
"type": "git",
Expand Down

0 comments on commit 6ea429d

Please sign in to comment.