From 5ca3fbeaea6d5c6c11b2f2e8a5b2959db7f4fcc9 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Sat, 9 Feb 2019 12:42:42 -0800 Subject: [PATCH 1/4] Added autoLocation option for weather modules. --- CHANGELOG.md | 1 + .../default/currentweather/currentweather.js | 14 ++++++++- modules/default/currentweather/node_helper.js | 29 +++++++++++++++++++ .../default/weatherforecast/node_helper.js | 29 +++++++++++++++++++ .../weatherforecast/weatherforecast.js | 15 ++++++++-- 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 modules/default/currentweather/node_helper.js create mode 100644 modules/default/weatherforecast/node_helper.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4787a3..3ff24c38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Italian translation for "Feels" - Disabled the screensaver on raspbian with installation script - Added option to truncate the number of vertical lines a calendar item can span if `wrapEvents` is enabled. +- Added autoLocation options for weather forcast and current weather modules. ### Updated - Bumped the Electron dependency to v3.0.13 to support the most recent Raspbian. [#1500](https://github.com/MichMich/MagicMirror/issues/1500) diff --git a/modules/default/currentweather/currentweather.js b/modules/default/currentweather/currentweather.js index 2a6d24b9..0472b7a1 100644 --- a/modules/default/currentweather/currentweather.js +++ b/modules/default/currentweather/currentweather.js @@ -11,6 +11,7 @@ Module.register("currentweather",{ // Default module config. defaults: { + autoLocation: false, location: false, locationID: false, appid: "", @@ -109,8 +110,19 @@ Module.register("currentweather",{ this.weatherType = null; this.feelsLike = null; this.loaded = false; - this.scheduleUpdate(this.config.initialLoadDelay); + if (this.config.autoLocation) { + this.sendSocketNotification("AUTO_LOCATION"); + } else { + this.scheduleUpdate(this.config.initialLoadDelay); + } + }, + + socketNotificationReceived: function (notification, payload) { + if (notification === "UPDATE_LOCATION") { + this.config.location = payload.location; + this.scheduleUpdate(this.config.initialLoadDelay); + } }, // add extra information of current weather diff --git a/modules/default/currentweather/node_helper.js b/modules/default/currentweather/node_helper.js new file mode 100644 index 00000000..f650cdb8 --- /dev/null +++ b/modules/default/currentweather/node_helper.js @@ -0,0 +1,29 @@ +var http = require("http"); +var NodeHelper = require("node_helper"); + +module.exports = NodeHelper.create({ + start: function () { + }, + + socketNotificationReceived: function (notification, payload) { + var self = this; + + if (notification === "AUTO_LOCATION") { + console.log("Loading timezone..."); + http.get("http://ip-api.com/json", function (req) { + var data = ""; + req.on("data", function (d) { + data += d; + }); + req.on("end", function () { + var body = JSON.parse(data); + payload.location = body.city + ", " + body.regionName; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + }).on("error", function () { + payload.error = "Could not figure out the timezone."; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + } + } +}); diff --git a/modules/default/weatherforecast/node_helper.js b/modules/default/weatherforecast/node_helper.js new file mode 100644 index 00000000..f650cdb8 --- /dev/null +++ b/modules/default/weatherforecast/node_helper.js @@ -0,0 +1,29 @@ +var http = require("http"); +var NodeHelper = require("node_helper"); + +module.exports = NodeHelper.create({ + start: function () { + }, + + socketNotificationReceived: function (notification, payload) { + var self = this; + + if (notification === "AUTO_LOCATION") { + console.log("Loading timezone..."); + http.get("http://ip-api.com/json", function (req) { + var data = ""; + req.on("data", function (d) { + data += d; + }); + req.on("end", function () { + var body = JSON.parse(data); + payload.location = body.city + ", " + body.regionName; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + }).on("error", function () { + payload.error = "Could not figure out the timezone."; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + } + } +}); diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index 67193696..1867da36 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -11,6 +11,7 @@ Module.register("weatherforecast",{ // Default module config. defaults: { + autoLocation: false, location: false, locationID: false, appid: "", @@ -95,10 +96,20 @@ Module.register("weatherforecast",{ this.forecast = []; this.loaded = false; - this.scheduleUpdate(this.config.initialLoadDelay); - this.updateTimer = null; + + if (this.config.autoLocation) { + this.sendSocketNotification("AUTO_LOCATION"); + } else { + this.scheduleUpdate(this.config.initialLoadDelay); + } + }, + socketNotificationReceived: function (notification, payload) { + if (notification === "UPDATE_LOCATION") { + this.config.location = payload.location; + this.scheduleUpdate(this.config.initialLoadDelay); + } }, // Override dom generator. From 1a97107b2d1ee5e0d3216ad290d976cf1ec9b568 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Sat, 9 Feb 2019 12:49:47 -0800 Subject: [PATCH 2/4] - Converted indentation to tabs. --- modules/default/currentweather/node_helper.js | 38 +++++++++---------- .../default/weatherforecast/node_helper.js | 38 +++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/modules/default/currentweather/node_helper.js b/modules/default/currentweather/node_helper.js index f650cdb8..53956f62 100644 --- a/modules/default/currentweather/node_helper.js +++ b/modules/default/currentweather/node_helper.js @@ -2,28 +2,28 @@ var http = require("http"); var NodeHelper = require("node_helper"); module.exports = NodeHelper.create({ - start: function () { - }, + start: function () { + }, - socketNotificationReceived: function (notification, payload) { - var self = this; + socketNotificationReceived: function (notification, payload) { + var self = this; if (notification === "AUTO_LOCATION") { - console.log("Loading timezone..."); - http.get("http://ip-api.com/json", function (req) { - var data = ""; - req.on("data", function (d) { - data += d; - }); - req.on("end", function () { - var body = JSON.parse(data); - payload.location = body.city + ", " + body.regionName; - self.sendSocketNotification("UPDATE_LOCATION", payload); - }); - }).on("error", function () { - payload.error = "Could not figure out the timezone."; - self.sendSocketNotification("UPDATE_LOCATION", payload); - }); + console.log("Loading timezone..."); + http.get("http://ip-api.com/json", function (req) { + var data = ""; + req.on("data", function (d) { + data += d; + }); + req.on("end", function () { + var body = JSON.parse(data); + payload.location = body.city + ", " + body.regionName; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + }).on("error", function () { + payload.error = "Could not figure out the timezone."; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); } } }); diff --git a/modules/default/weatherforecast/node_helper.js b/modules/default/weatherforecast/node_helper.js index f650cdb8..53956f62 100644 --- a/modules/default/weatherforecast/node_helper.js +++ b/modules/default/weatherforecast/node_helper.js @@ -2,28 +2,28 @@ var http = require("http"); var NodeHelper = require("node_helper"); module.exports = NodeHelper.create({ - start: function () { - }, + start: function () { + }, - socketNotificationReceived: function (notification, payload) { - var self = this; + socketNotificationReceived: function (notification, payload) { + var self = this; if (notification === "AUTO_LOCATION") { - console.log("Loading timezone..."); - http.get("http://ip-api.com/json", function (req) { - var data = ""; - req.on("data", function (d) { - data += d; - }); - req.on("end", function () { - var body = JSON.parse(data); - payload.location = body.city + ", " + body.regionName; - self.sendSocketNotification("UPDATE_LOCATION", payload); - }); - }).on("error", function () { - payload.error = "Could not figure out the timezone."; - self.sendSocketNotification("UPDATE_LOCATION", payload); - }); + console.log("Loading timezone..."); + http.get("http://ip-api.com/json", function (req) { + var data = ""; + req.on("data", function (d) { + data += d; + }); + req.on("end", function () { + var body = JSON.parse(data); + payload.location = body.city + ", " + body.regionName; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); + }).on("error", function () { + payload.error = "Could not figure out the timezone."; + self.sendSocketNotification("UPDATE_LOCATION", payload); + }); } } }); From c608636b7adbb6e40ea920199825277b99da03c0 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Sat, 9 Feb 2019 13:41:42 -0800 Subject: [PATCH 3/4] - Added autoTimezone property to the clock --- CHANGELOG.md | 1 + modules/default/clock/clock.js | 26 ++++++++++++++++++++----- modules/default/clock/node_helper.js | 29 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 modules/default/clock/node_helper.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff24c38..7d90b047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Disabled the screensaver on raspbian with installation script - Added option to truncate the number of vertical lines a calendar item can span if `wrapEvents` is enabled. - Added autoLocation options for weather forcast and current weather modules. +- Added autoTimezone option for the default clock module. ### Updated - Bumped the Electron dependency to v3.0.13 to support the most recent Raspbian. [#1500](https://github.com/MichMich/MagicMirror/issues/1500) diff --git a/modules/default/clock/clock.js b/modules/default/clock/clock.js index 23b801d0..03528bce 100644 --- a/modules/default/clock/clock.js +++ b/modules/default/clock/clock.js @@ -26,6 +26,7 @@ Module.register("clock",{ analogShowDate: "top", // options: false, 'top', or 'bottom' secondsColor: "#888888", timezone: null, + autoTimezone: false }, // Define required scripts. getScripts: function() { @@ -39,16 +40,31 @@ Module.register("clock",{ start: function() { Log.info("Starting module: " + this.name); - // Schedule update interval. - var self = this; - setInterval(function() { - self.updateDom(); - }, 1000); + if (this.config.autoTimezone) { + this.sendSocketNotification("AUTO_TIMEZONE"); + } else { + // Schedule update interval. + var self = this; + setInterval(function() { + self.updateDom(); + }, 1000); + } // Set locale. moment.locale(config.language); }, + + socketNotificationReceived: function (notification, payload) { + if (notification === "UPDATE_TIMEZONE") { + var self = this; + self.config.timezone = payload.timezone; + setInterval(function() { + self.updateDom(); + }, 1000); + } + }, + // Override dom generator. getDom: function() { diff --git a/modules/default/clock/node_helper.js b/modules/default/clock/node_helper.js new file mode 100644 index 00000000..68258b0a --- /dev/null +++ b/modules/default/clock/node_helper.js @@ -0,0 +1,29 @@ +var http = require("http"); +var NodeHelper = require("node_helper"); + +module.exports = NodeHelper.create({ + start: function () { + }, + + socketNotificationReceived: function (notification, payload) { + var self = this; + + if (notification === "AUTO_TIMEZONE") { + console.log("Loading timezone..."); + http.get("http://ip-api.com/json", function (req) { + var data = ""; + req.on("data", function (d) { + data += d; + }); + req.on("end", function () { + var body = JSON.parse(data); + payload.timezone = body.timezone; + self.sendSocketNotification("UPDATE_TIMEZONE", payload); + }); + }).on("error", function () { + payload.error = "Could not figure out the timezone."; + self.sendSocketNotification("UPDATE_TIMEZONE", payload); + }); + } + } +}); From 3d5ad29eacda9936e10d056a268a67b3c3ac40d3 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Sat, 9 Feb 2019 13:51:23 -0800 Subject: [PATCH 4/4] - Removed trailing space --- modules/default/weatherforecast/weatherforecast.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index 1867da36..c32821db 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -97,7 +97,7 @@ Module.register("weatherforecast",{ this.forecast = []; this.loaded = false; this.updateTimer = null; - + if (this.config.autoLocation) { this.sendSocketNotification("AUTO_LOCATION"); } else {