mirror of
				https://github.com/MichMich/MagicMirror.git
				synced 2025-10-31 02:36:47 +00:00 
			
		
		
		
	Possibility to use the the calendar feed as the source for the weather.
This commit is contained in:
		| @@ -35,19 +35,20 @@ The following properties can be configured: | ||||
| 		</tr> | ||||
| 	<thead> | ||||
| 	<tbody> | ||||
| 	 | ||||
| 		<tr> | ||||
| 			<td><code>location</code></td> | ||||
| 			<td>The location used for weather information.<br> | ||||
| 				<br><b>Example:</b> <code>Amsterdam,Netherlands</code> | ||||
| 				<br><b>Default value:</b> <code>New York</code> | ||||
| 				<br><b>Example:</b> <code>'Amsterdam,Netherlands'</code> | ||||
| 				<br><b>Default value:</b> <code>New York</code><br><br> | ||||
| 				<strong>Note:</strong> When the <code>location</code> and <code>locationID</code> 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. | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>locationID</code></td> | ||||
| 			<td>Location ID from <a href="http://bulk.openweather.org/sample/">OpenWeather</a> <b>This will override anything you put in location.</b><br>Leave blank if you want to use location. | ||||
| 				<br><b>Example:</b> <code>1234567</code> | ||||
| 				<br><b>Default value:</b> <code></code> | ||||
| 				<br><b>Default value:</b> <code></code><br><br> | ||||
| 				<strong>Note:</strong> When the <code>location</code> and <code>locationID</code> 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. | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| @@ -158,6 +159,18 @@ The following properties can be configured: | ||||
| 				<br><b>Default value:</b>  <code>'weather'</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>appendLocationNameToHeader</code></td> | ||||
| 			<td>If set to <code>true</code>, 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.<br> | ||||
| 				<br><b>Default value:</b>  <code>true</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>calendarClass</code></td> | ||||
| 			<td>The class for the calender module to base the event based weather information on.<br> | ||||
| 				<br><b>Default value:</b>  <code>'calendar'</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>iconTable</code></td> | ||||
| 			<td>The conversion table to convert the weather conditions to weather-icons.<br> | ||||
|   | ||||
| @@ -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 <i>location</i> 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); | ||||
| 	}, | ||||
|   | ||||
| @@ -35,19 +35,20 @@ The following properties can be configured: | ||||
| 		</tr> | ||||
| 	<thead> | ||||
| 	<tbody> | ||||
|  | ||||
| 		<tr> | ||||
| 			<td><code>location</code></td> | ||||
| 			<td>The location used for weather information.<br> | ||||
| 				<br><b>Example:</b> <code>Amsterdam,Netherlands</code> | ||||
| 				<br><b>Default value:</b> <code>New York</code> | ||||
| 				<br><b>Example:</b> <code>'Amsterdam,Netherlands'</code> | ||||
| 				<br><b>Default value:</b> <code>New York</code><br><br> | ||||
| 				<strong>Note:</strong> When the <code>location</code> and <code>locationID</code> 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. | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>locationID</code></td> | ||||
| 			<td>Location ID from <a href="http://bulk.openweather.org/sample/">OpenWeather</a> <b>This will override anything you put in location.</b><br>Leave blank if you want to use location. | ||||
| 				<br><b>Example:</b> <code>1234567</code> | ||||
| 				<br><b>Default value:</b> <code></code> | ||||
| 				<br><b>Default value:</b> <code></code><br><br> | ||||
| 				<strong>Note:</strong> When the <code>location</code> and <code>locationID</code> 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. | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| @@ -85,7 +86,6 @@ The following properties can be configured: | ||||
| 				<br><b>Default value:</b> <code>2000</code> (2 seconds) | ||||
| 			</td> | ||||
| 		</tr> | ||||
|  | ||||
| 		<tr> | ||||
| 			<td><code>lang</code></td> | ||||
| 			<td>The language of the days.<br> | ||||
| @@ -139,6 +139,18 @@ The following properties can be configured: | ||||
| 				<br><b>Default value:</b>  <code>'forecast/daily'</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>appendLocationNameToHeader</code></td> | ||||
| 			<td>If set to <code>true</code>, 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.<br> | ||||
| 				<br><b>Default value:</b>  <code>true</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>calendarClass</code></td> | ||||
| 			<td>The class for the calender module to base the event based weather information on.<br> | ||||
| 				<br><b>Default value:</b>  <code>'calendar'</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
| 		<tr> | ||||
| 			<td><code>iconTable</code></td> | ||||
| 			<td>The conversion table to convert the weather conditions to weather-icons.<br> | ||||
| @@ -164,6 +176,5 @@ The following properties can be configured: | ||||
| 		}</code> | ||||
| 			</td> | ||||
| 		</tr> | ||||
|  | ||||
| 	</tbody> | ||||
| </table> | ||||
|   | ||||
| @@ -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 <i>location</i> 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); | ||||
| 	}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user