From 84dc1fa15194a0921925837b2b4229b7c6e1043c Mon Sep 17 00:00:00 2001 From: Michael Teeuw Date: Fri, 14 Oct 2016 17:42:07 +0200 Subject: [PATCH] Possibility to use the the calendar feed as the source for the weather. --- CHANGELOG.md | 3 +- js/main.js | 2 +- js/module.js | 2 - modules/default/currentweather/README.md | 21 ++++- .../default/currentweather/currentweather.js | 75 ++++++++++++++---- modules/default/weatherforecast/README.md | 23 ++++-- .../weatherforecast/weatherforecast.js | 76 +++++++++++++++---- 7 files changed, 162 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efdd1aac..bcb7b9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Module API: Add Visibility locking to module system. [See documentation](https://github.com/MichMich/MagicMirror/tree/develop/modules#visibility-locking) for more information. - Module API: Method to overwrite the module's header. [See documentation](https://github.com/MichMich/MagicMirror/tree/develop/modules#getheader) for more information. - Module API: Option to define the minimumn MagicMirror version to run a module. [See documentation](https://github.com/MichMich/MagicMirror/tree/develop/modules#requiresversion) for more information. -- Calendar module now broadcasts the event list to all other modules using the notification system. [See documentation](https://github.com/MichMich/MagicMirror/tree/master/modules/default/calendar) for more information. +- Calendar module now broadcasts the event list to all other modules using the notification system. [See documentation](https://github.com/MichMich/MagicMirror/tree/develop/modules/default/calendar) for more information. +- Possibility to use the the calendar feed as the source for the weather (currentweather & weatherforecast) location data. [See documentation](https://github.com/MichMich/MagicMirror/tree/develop/modules/default/weatherforecast) for more information. ### Updated - Modified translations for Frysk. diff --git a/js/main.js b/js/main.js index a8572c6a..e97f3e10 100644 --- a/js/main.js +++ b/js/main.js @@ -179,7 +179,7 @@ var MM = (function() { // set lockString if set in options. if (options.lockString) { - Log.log("Has lockstring: " + options.lockString); + // Log.log("Has lockstring: " + options.lockString); if (module.lockStrings.indexOf(options.lockString) === -1) { module.lockStrings.push(options.lockString); } diff --git a/js/module.js b/js/module.js index 397d4799..4815895e 100644 --- a/js/module.js +++ b/js/module.js @@ -337,8 +337,6 @@ var Module = Class.extend({ self.suspend(); callback(); }, options); - - Log.log(options); }, /* showModule(module, speed, callback) diff --git a/modules/default/currentweather/README.md b/modules/default/currentweather/README.md index 3df88173..067dce54 100644 --- a/modules/default/currentweather/README.md +++ b/modules/default/currentweather/README.md @@ -35,19 +35,20 @@ The following properties can be configured: - location The location used for weather information.
-
Example: Amsterdam,Netherlands -
Default value: New York +
Example: 'Amsterdam,Netherlands' +
Default value: New York

+ Note: When the location and locationID are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. locationID Location ID from OpenWeather This will override anything you put in location.
Leave blank if you want to use location.
Example: 1234567 -
Default value: +
Default value:

+ Note: When the location and locationID are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. @@ -158,6 +159,18 @@ The following properties can be configured:
Default value: 'weather' + + appendLocationNameToHeader + If set to true, the returned location name will be appended to the header of the module, if the header is enabled. This is mainly intresting when using calender based weather.
+
Default value: true + + + + calendarClass + The class for the calender module to base the event based weather information on.
+
Default value: 'calendar' + + iconTable The conversion table to convert the weather conditions to weather-icons.
diff --git a/modules/default/currentweather/currentweather.js b/modules/default/currentweather/currentweather.js index 0662ef62..39db3f09 100644 --- a/modules/default/currentweather/currentweather.js +++ b/modules/default/currentweather/currentweather.js @@ -11,8 +11,8 @@ Module.register("currentweather",{ // Default module config. defaults: { - location: "", - locationID: "", + location: false, + locationID: false, appid: "", units: config.units, updateInterval: 10 * 60 * 1000, // every 10 minutes @@ -32,6 +32,9 @@ Module.register("currentweather",{ apiBase: "http://api.openweathermap.org/data/", weatherEndpoint: "weather", + appendLocationNameToHeader: true, + calendarClass: "calendar", + iconTable: { "01d": "wi-day-sunny", "02d": "wi-day-cloudy", @@ -54,6 +57,12 @@ Module.register("currentweather",{ }, }, + // create a variable for the first upcoming calendaar event. Used if no location is specified. + firstEvent: false, + + // create a variable to hold the location name based on the API result. + fetchedLocatioName: "", + // Define required scripts. getScripts: function() { return ["moment.js"]; @@ -103,12 +112,6 @@ Module.register("currentweather",{ return wrapper; } - if (this.config.location === "") { - wrapper.innerHTML = "Please set the openweather location in the config for module: " + this.name + "."; - wrapper.className = "dimmed light small"; - return wrapper; - } - if (!this.loaded) { wrapper.innerHTML = this.translate('LOADING'); wrapper.className = "dimmed light small"; @@ -178,11 +181,50 @@ Module.register("currentweather",{ return wrapper; }, + // Override getHeader method. + getHeader: function() { + if (this.config.appendLocationNameToHeader) { + return this.data.header + " " + this.fetchedLocatioName; + } + + return this.data.header; + }, + + // Override notification handler. + notificationReceived: function(notification, payload, sender) { + if (notification === "DOM_OBJECTS_CREATED") { + if (this.config.appendLocationNameToHeader) { + this.hide(0, {lockString: this.identifier}); + } + } + if (notification === "CALENDAR_EVENTS") { + var senderClasses = sender.data.classes.toLowerCase().split(" "); + if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) { + var lastEvent = this.firstEvent; + this.firstEvent = false; + + for (e in payload) { + var event = payload[e]; + if (event.location || event.geo) { + this.firstEvent = event; + //Log.log("First upcoming event with location: ", event); + break; + } + } + } + } + }, + /* updateWeather(compliments) * Requests new data from openweather.org. * Calls processWeather on succesfull response. */ updateWeather: function() { + if (this.config.appid === "") { + Log.error("CurrentWeather: APPID not set!"); + return; + } + var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.weatherEndpoint + this.getParams(); var self = this; var retry = true; @@ -194,11 +236,10 @@ Module.register("currentweather",{ if (this.status === 200) { self.processWeather(JSON.parse(this.response)); } else if (this.status === 401) { - self.config.appid = ""; self.updateDom(self.config.animationSpeed); Log.error(self.name + ": Incorrect APPID."); - retry = false; + retry = true; } else { Log.error(self.name + ": Could not load weather."); } @@ -218,11 +259,19 @@ Module.register("currentweather",{ */ getParams: function() { var params = "?"; - if(this.config.locationID !== "") { + if(this.config.locationID !== false) { params += "id=" + this.config.locationID; - } else { + } else if(this.config.location !== false) { params += "q=" + this.config.location; + } else if (this.firstEvent && this.firstEvent.geo) { + params += "lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon + } else if (this.firstEvent && this.firstEvent.location) { + params += "q=" + this.firstEvent.location; + } else { + this.hide(this.config.animationSpeed, {lockString:this.identifier}); + return; } + params += "&units=" + this.config.units; params += "&lang=" + this.config.lang; params += "&APPID=" + this.config.appid; @@ -285,7 +334,7 @@ Module.register("currentweather",{ this.sunriseSunsetIcon = (sunrise < now && sunset > now) ? "wi-sunset" : "wi-sunrise"; - + this.show(this.config.animationSpeed, {lockString:this.identifier}); this.loaded = true; this.updateDom(this.config.animationSpeed); }, diff --git a/modules/default/weatherforecast/README.md b/modules/default/weatherforecast/README.md index aba03a41..a1df57c1 100644 --- a/modules/default/weatherforecast/README.md +++ b/modules/default/weatherforecast/README.md @@ -35,19 +35,20 @@ The following properties can be configured: - location The location used for weather information.
-
Example: Amsterdam,Netherlands -
Default value: New York +
Example: 'Amsterdam,Netherlands' +
Default value: New York

+ Note: When the location and locationID are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. locationID Location ID from OpenWeather This will override anything you put in location.
Leave blank if you want to use location.
Example: 1234567 -
Default value: +
Default value:

+ Note: When the location and locationID are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. @@ -85,7 +86,6 @@ The following properties can be configured:
Default value: 2000 (2 seconds) - lang The language of the days.
@@ -139,6 +139,18 @@ The following properties can be configured:
Default value: 'forecast/daily' + + appendLocationNameToHeader + If set to true, the returned location name will be appended to the header of the module, if the header is enabled. This is mainly intresting when using calender based weather.
+
Default value: true + + + + calendarClass + The class for the calender module to base the event based weather information on.
+
Default value: 'calendar' + + iconTable The conversion table to convert the weather conditions to weather-icons.
@@ -164,6 +176,5 @@ The following properties can be configured: } - diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index a8a8ca6c..7119f79e 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -11,8 +11,8 @@ Module.register("weatherforecast",{ // Default module config. defaults: { - location: "", - locationID: "", + location: false, + locationID: false, appid: "", units: config.units, maxNumberOfDays: 7, @@ -30,6 +30,9 @@ Module.register("weatherforecast",{ apiBase: "http://api.openweathermap.org/data/", forecastEndpoint: "forecast/daily", + appendLocationNameToHeader: true, + calendarClass: "calendar", + iconTable: { "01d": "wi-day-sunny", "02d": "wi-day-cloudy", @@ -52,6 +55,12 @@ Module.register("weatherforecast",{ }, }, + // create a variable for the first upcoming calendaar event. Used if no location is specified. + firstEvent: false, + + // create a variable to hold the location name based on the API result. + fetchedLocatioName: "", + // Define required scripts. getScripts: function() { return ["moment.js"]; @@ -95,12 +104,6 @@ Module.register("weatherforecast",{ return wrapper; } - if (this.config.location === "") { - wrapper.innerHTML = "Please set the openweather location in the config for module: " + this.name + "."; - wrapper.className = "dimmed light small"; - return wrapper; - } - if (!this.loaded) { wrapper.innerHTML = this.translate("LOADING"); wrapper.className = "dimmed light small"; @@ -156,11 +159,50 @@ Module.register("weatherforecast",{ return table; }, + // Override getHeader method. + getHeader: function() { + if (this.config.appendLocationNameToHeader) { + return this.data.header + " " + this.fetchedLocatioName; + } + + return this.data.header; + }, + + // Override notification handler. + notificationReceived: function(notification, payload, sender) { + if (notification === "DOM_OBJECTS_CREATED") { + if (this.config.appendLocationNameToHeader) { + this.hide(0, {lockString: this.identifier}); + } + } + if (notification === "CALENDAR_EVENTS") { + var senderClasses = sender.data.classes.toLowerCase().split(" "); + if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) { + var lastEvent = this.firstEvent; + this.firstEvent = false; + + for (e in payload) { + var event = payload[e]; + if (event.location || event.geo) { + this.firstEvent = event; + //Log.log("First upcoming event with location: ", event); + break; + } + } + } + } + }, + /* updateWeather(compliments) * Requests new data from openweather.org. * Calls processWeather on succesfull response. */ updateWeather: function() { + if (this.config.appid === "") { + Log.error("WeatherForecast: APPID not set!"); + return; + } + var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.forecastEndpoint + this.getParams(); var self = this; var retry = true; @@ -172,11 +214,10 @@ Module.register("weatherforecast",{ if (this.status === 200) { self.processWeather(JSON.parse(this.response)); } else if (this.status === 401) { - self.config.appid = ""; self.updateDom(self.config.animationSpeed); Log.error(self.name + ": Incorrect APPID."); - retry = false; + retry = true; } else { Log.error(self.name + ": Could not load weather."); } @@ -196,11 +237,19 @@ Module.register("weatherforecast",{ */ getParams: function() { var params = "?"; - if(this.config.locationID !== "") { + if(this.config.locationID !== false) { params += "id=" + this.config.locationID; - } else { + } else if(this.config.location !== false) { params += "q=" + this.config.location; + } else if (this.firstEvent && this.firstEvent.geo) { + params += "lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon + } else if (this.firstEvent && this.firstEvent.location) { + params += "q=" + this.firstEvent.location; + } else { + this.hide(this.config.animationSpeed, {lockString:this.identifier}); + return; } + params += "&units=" + this.config.units; params += "&lang=" + this.config.lang; /* @@ -220,6 +269,7 @@ Module.register("weatherforecast",{ * argument data object - Weather information received form openweather.org. */ processWeather: function(data) { + this.fetchedLocatioName = data.city.name + ", " + data.city.country; this.forecast = []; for (var i = 0, count = data.list.length; i < count; i++) { @@ -236,7 +286,7 @@ Module.register("weatherforecast",{ } //Log.log(this.forecast); - + this.show(this.config.animationSpeed, {lockString:this.identifier}); this.loaded = true; this.updateDom(this.config.animationSpeed); },