| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | /* global Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Magic Mirror | 
					
						
							|  |  |  |  * Module: WeatherForecast | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * By Michael Teeuw http://michaelteeuw.nl
 | 
					
						
							|  |  |  |  * MIT Licensed. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | Module.register("weatherforecast",{ | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Default module config.
 | 
					
						
							|  |  |  | 	defaults: { | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		location: false, | 
					
						
							|  |  |  | 		locationID: false, | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		appid: "", | 
					
						
							| 
									
										
										
										
											2016-04-24 15:24:44 -05:00
										 |  |  | 		units: config.units, | 
					
						
							| 
									
										
										
										
											2016-04-29 12:52:10 -06:00
										 |  |  | 		maxNumberOfDays: 7, | 
					
						
							| 
									
										
										
										
											2016-10-14 23:07:13 +02:00
										 |  |  | 		showRainAmount: false, | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		updateInterval: 10 * 60 * 1000, // every 10 minutes
 | 
					
						
							|  |  |  | 		animationSpeed: 1000, | 
					
						
							|  |  |  | 		timeFormat: config.timeFormat, | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		lang: config.language, | 
					
						
							| 
									
										
										
										
											2016-04-03 19:52:13 +02:00
										 |  |  | 		fade: true, | 
					
						
							| 
									
										
										
										
											2016-03-29 16:10:50 +02:00
										 |  |  | 		fadePoint: 0.25, // Start on 1/4th of the list.
 | 
					
						
							| 
									
										
										
										
											2017-01-29 00:59:38 +01:00
										 |  |  | 		colored: false, | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-30 15:13:02 +02:00
										 |  |  | 		initialLoadDelay: 2500, // 2.5 seconds delay. This delay is used to keep the OpenWeather API happy.
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		retryDelay: 2500, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		apiVersion: "2.5", | 
					
						
							|  |  |  | 		apiBase: "http://api.openweathermap.org/data/", | 
					
						
							|  |  |  | 		forecastEndpoint: "forecast/daily", | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		appendLocationNameToHeader: true, | 
					
						
							|  |  |  | 		calendarClass: "calendar", | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:33:41 +01:00
										 |  |  | 		roundTemp: false, | 
					
						
							| 
									
										
										
										
											2016-12-29 22:23:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		iconTable: { | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 			"01d": "wi-day-sunny", | 
					
						
							|  |  |  | 			"02d": "wi-day-cloudy", | 
					
						
							|  |  |  | 			"03d": "wi-cloudy", | 
					
						
							|  |  |  | 			"04d": "wi-cloudy-windy", | 
					
						
							|  |  |  | 			"09d": "wi-showers", | 
					
						
							|  |  |  | 			"10d": "wi-rain", | 
					
						
							|  |  |  | 			"11d": "wi-thunderstorm", | 
					
						
							|  |  |  | 			"13d": "wi-snow", | 
					
						
							|  |  |  | 			"50d": "wi-fog", | 
					
						
							|  |  |  | 			"01n": "wi-night-clear", | 
					
						
							|  |  |  | 			"02n": "wi-night-cloudy", | 
					
						
							|  |  |  | 			"03n": "wi-night-cloudy", | 
					
						
							|  |  |  | 			"04n": "wi-night-cloudy", | 
					
						
							|  |  |  | 			"09n": "wi-night-showers", | 
					
						
							|  |  |  | 			"10n": "wi-night-rain", | 
					
						
							|  |  |  | 			"11n": "wi-night-thunderstorm", | 
					
						
							|  |  |  | 			"13n": "wi-night-snow", | 
					
						
							|  |  |  | 			"50n": "wi-night-alt-cloudy-windy" | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 	// 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: "", | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	// Define required scripts.
 | 
					
						
							|  |  |  | 	getScripts: function() { | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		return ["moment.js"]; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Define required scripts.
 | 
					
						
							|  |  |  | 	getStyles: function() { | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		return ["weather-icons.css", "weatherforecast.css"]; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-11 12:38:41 +02:00
										 |  |  | 	// Define required translations.
 | 
					
						
							|  |  |  | 	getTranslations: function() { | 
					
						
							|  |  |  | 		// The translations for the defaut modules are defined in the core translation files.
 | 
					
						
							|  |  |  | 		// Therefor we can just return false. Otherwise we should have returned a dictionairy.
 | 
					
						
							|  |  |  | 		// If you're trying to build yiur own module including translations, check out the documentation.
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	// Define start sequence.
 | 
					
						
							|  |  |  | 	start: function() { | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		Log.info("Starting module: " + this.name); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Set locale.
 | 
					
						
							|  |  |  | 		moment.locale(config.language); | 
					
						
							| 
									
										
										
										
											2016-04-03 19:52:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		this.forecast = []; | 
					
						
							|  |  |  | 		this.loaded = false; | 
					
						
							|  |  |  | 		this.scheduleUpdate(this.config.initialLoadDelay); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.updateTimer = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Override dom generator.
 | 
					
						
							|  |  |  | 	getDom: function() { | 
					
						
							|  |  |  | 		var wrapper = document.createElement("div"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		if (this.config.appid === "") { | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			wrapper.innerHTML = "Please set the correct openweather <i>appid</i> in the config for module: " + this.name + "."; | 
					
						
							|  |  |  | 			wrapper.className = "dimmed light small"; | 
					
						
							|  |  |  | 			return wrapper; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!this.loaded) { | 
					
						
							| 
									
										
										
										
											2016-09-18 10:54:20 -07:00
										 |  |  | 			wrapper.innerHTML = this.translate("LOADING"); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			wrapper.className = "dimmed light small"; | 
					
						
							|  |  |  | 			return wrapper; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var table = document.createElement("table"); | 
					
						
							|  |  |  | 		table.className = "small"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var f in this.forecast) { | 
					
						
							|  |  |  | 			var forecast = this.forecast[f]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var row = document.createElement("tr"); | 
					
						
							| 
									
										
										
										
											2017-01-29 00:59:38 +01:00
										 |  |  | 			if (this.config.colored) { | 
					
						
							|  |  |  | 				row.className = "colored"; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			table.appendChild(row); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var dayCell = document.createElement("td"); | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 			dayCell.className = "day"; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			dayCell.innerHTML = forecast.day; | 
					
						
							|  |  |  | 			row.appendChild(dayCell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var iconCell = document.createElement("td"); | 
					
						
							|  |  |  | 			iconCell.className = "bright weather-icon"; | 
					
						
							|  |  |  | 			row.appendChild(iconCell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var icon = document.createElement("span"); | 
					
						
							| 
									
										
										
										
											2016-08-03 11:43:28 +02:00
										 |  |  | 			icon.className = "wi weathericon " + forecast.icon; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			iconCell.appendChild(icon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var maxTempCell = document.createElement("td"); | 
					
						
							| 
									
										
										
										
											2016-12-02 20:52:36 +01:00
										 |  |  | 			maxTempCell.innerHTML = forecast.maxTemp; | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 			maxTempCell.className = "align-right bright max-temp"; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			row.appendChild(maxTempCell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var minTempCell = document.createElement("td"); | 
					
						
							| 
									
										
										
										
											2016-12-02 20:52:36 +01:00
										 |  |  | 			minTempCell.innerHTML = forecast.minTemp; | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 			minTempCell.className = "align-right min-temp"; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			row.appendChild(minTempCell); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 23:07:13 +02:00
										 |  |  | 			if (this.config.showRainAmount) { | 
					
						
							|  |  |  | 				var rainCell = document.createElement("td"); | 
					
						
							|  |  |  | 				if (isNaN(forecast.rain)) { | 
					
						
							|  |  |  | 					rainCell.innerHTML = ""; | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2017-01-15 21:16:01 +01:00
										 |  |  | 					if(config.units !== "imperial") { | 
					
						
							|  |  |  | 						rainCell.innerHTML = forecast.rain + " mm"; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						rainCell.innerHTML = (parseFloat(forecast.rain) / 25.4).toFixed(2) + " in"; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-10-14 23:07:13 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				rainCell.className = "align-right bright rain"; | 
					
						
							|  |  |  | 				row.appendChild(rainCell); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 16:10:50 +02:00
										 |  |  | 			if (this.config.fade && this.config.fadePoint < 1) { | 
					
						
							|  |  |  | 				if (this.config.fadePoint < 0) { | 
					
						
							|  |  |  | 					this.config.fadePoint = 0; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				var startingPoint = this.forecast.length * this.config.fadePoint; | 
					
						
							|  |  |  | 				var steps = this.forecast.length - startingPoint; | 
					
						
							|  |  |  | 				if (f >= startingPoint) { | 
					
						
							|  |  |  | 					var currentStep = f - startingPoint; | 
					
						
							|  |  |  | 					row.style.opacity = 1 - (1 / steps * currentStep); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-03 19:52:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		return table; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 	// 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(" "); | 
					
						
							| 
									
										
										
										
											2016-12-29 22:23:08 -03:00
										 |  |  | 			if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) { | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 				var lastEvent =  this.firstEvent; | 
					
						
							|  |  |  | 				this.firstEvent = false; | 
					
						
							| 
									
										
										
										
											2016-12-29 22:23:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 				for (e in payload) { | 
					
						
							|  |  |  | 					var event = payload[e]; | 
					
						
							|  |  |  | 					if (event.location || event.geo) { | 
					
						
							|  |  |  | 						this.firstEvent = event; | 
					
						
							| 
									
										
										
										
											2016-12-29 22:23:08 -03:00
										 |  |  | 						//Log.log("First upcoming event with location: ", event);
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	/* updateWeather(compliments) | 
					
						
							|  |  |  | 	 * Requests new data from openweather.org. | 
					
						
							|  |  |  | 	 * Calls processWeather on succesfull response. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateWeather: function() { | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		if (this.config.appid === "") { | 
					
						
							|  |  |  | 			Log.error("WeatherForecast: APPID not set!"); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 10:54:20 -07:00
										 |  |  | 		var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.forecastEndpoint + this.getParams(); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		var self = this; | 
					
						
							|  |  |  | 		var retry = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var weatherRequest = new XMLHttpRequest(); | 
					
						
							|  |  |  | 		weatherRequest.open("GET", url, true); | 
					
						
							|  |  |  | 		weatherRequest.onreadystatechange = function() { | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 			if (this.readyState === 4) { | 
					
						
							|  |  |  | 				if (this.status === 200) { | 
					
						
							|  |  |  | 					self.processWeather(JSON.parse(this.response)); | 
					
						
							|  |  |  | 				} else if (this.status === 401) { | 
					
						
							|  |  |  | 					self.updateDom(self.config.animationSpeed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Log.error(self.name + ": Incorrect APPID."); | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 					retry = true; | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					Log.error(self.name + ": Could not load weather."); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 				if (retry) { | 
					
						
							|  |  |  | 					self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		weatherRequest.send(); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* getParams(compliments) | 
					
						
							|  |  |  | 	 * Generates an url with api parameters based on the config. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * return String - URL params. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getParams: function() { | 
					
						
							|  |  |  | 		var params = "?"; | 
					
						
							| 
									
										
										
										
											2016-10-26 18:59:56 +00:00
										 |  |  | 		if(this.config.locationID) { | 
					
						
							| 
									
										
										
										
											2016-05-25 15:23:29 -05:00
										 |  |  | 			params += "id=" + this.config.locationID; | 
					
						
							| 
									
										
										
										
											2016-10-26 18:59:56 +00:00
										 |  |  | 		} else if(this.config.location) { | 
					
						
							| 
									
										
										
										
											2016-05-25 15:23:29 -05:00
										 |  |  | 			params += "q=" + this.config.location; | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		} 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; | 
					
						
							| 
									
										
										
										
											2016-05-25 15:23:29 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		params += "&units=" + this.config.units; | 
					
						
							|  |  |  | 		params += "&lang=" + this.config.lang; | 
					
						
							| 
									
										
										
										
											2016-04-29 12:52:10 -06:00
										 |  |  | 		/* | 
					
						
							|  |  |  | 		 * Submit a specific number of days to forecast, between 1 to 16 days. | 
					
						
							|  |  |  | 		 * The OpenWeatherMap API properly handles values outside of the 1 - 16 range and returns 7 days by default. | 
					
						
							|  |  |  | 		 * This is simply being pedantic and doing it ourselves. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		params += "&cnt=" + (((this.config.maxNumberOfDays < 1) || (this.config.maxNumberOfDays > 16)) ? 7 : this.config.maxNumberOfDays); | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		params += "&APPID=" + this.config.appid; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return params; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* processWeather(data) | 
					
						
							|  |  |  | 	 * Uses the received data to set the various values. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * argument data object - Weather information received form openweather.org. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	processWeather: function(data) { | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		this.fetchedLocatioName = data.city.name + ", " + data.city.country; | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		this.forecast = []; | 
					
						
							|  |  |  | 		for (var i = 0, count = data.list.length; i < count; i++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var forecast = data.list[i]; | 
					
						
							|  |  |  | 			this.forecast.push({ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 23:05:45 +02:00
										 |  |  | 				day: moment(forecast.dt, "X").format("ddd"), | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 				icon: this.config.iconTable[forecast.weather[0].icon], | 
					
						
							|  |  |  | 				maxTemp: this.roundValue(forecast.temp.max), | 
					
						
							| 
									
										
										
										
											2016-10-14 23:07:13 +02:00
										 |  |  | 				minTemp: this.roundValue(forecast.temp.min), | 
					
						
							|  |  |  | 				rain: this.roundValue(forecast.rain) | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//Log.log(this.forecast);
 | 
					
						
							| 
									
										
										
										
											2016-10-14 17:42:07 +02:00
										 |  |  | 		this.show(this.config.animationSpeed, {lockString:this.identifier}); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 		this.loaded = true; | 
					
						
							|  |  |  | 		this.updateDom(this.config.animationSpeed); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* scheduleUpdate() | 
					
						
							|  |  |  | 	 * Schedule next update. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	scheduleUpdate: function(delay) { | 
					
						
							|  |  |  | 		var nextLoad = this.config.updateInterval; | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 		if (typeof delay !== "undefined" && delay >= 0) { | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 			nextLoad = delay; | 
					
						
							| 
									
										
										
										
											2016-04-03 19:52:13 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var self = this; | 
					
						
							|  |  |  | 		clearTimeout(this.updateTimer); | 
					
						
							|  |  |  | 		this.updateTimer = setTimeout(function() { | 
					
						
							|  |  |  | 			self.updateWeather(); | 
					
						
							|  |  |  | 		}, nextLoad); | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ms2Beaufort(ms) | 
					
						
							|  |  |  | 	 * Converts m2 to beaufort (windspeed). | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * argument ms number - Windspeed in m/s. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * return number - Windspeed in beaufort. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	ms2Beaufort: function(ms) { | 
					
						
							|  |  |  | 		var kmh = ms * 60 * 60 / 1000; | 
					
						
							|  |  |  | 		var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000]; | 
					
						
							|  |  |  | 		for (var beaufort in speeds) { | 
					
						
							|  |  |  | 			var speed = speeds[beaufort]; | 
					
						
							|  |  |  | 			if (speed > kmh) { | 
					
						
							|  |  |  | 				return beaufort; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 12; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* function(temperature) | 
					
						
							| 
									
										
										
										
											2016-12-02 20:52:36 +01:00
										 |  |  | 	 * Rounds a temperature to 1 decimal or integer (depending on config.roundTemp). | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	 * | 
					
						
							|  |  |  | 	 * argument temperature number - Temperature. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * return number - Rounded Temperature. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-04-05 14:35:11 -04:00
										 |  |  | 	roundValue: function(temperature) { | 
					
						
							| 
									
										
										
										
											2016-12-02 20:52:36 +01:00
										 |  |  | 		var decimals = this.config.roundTemp ? 0 : 1; | 
					
						
							|  |  |  | 		return parseFloat(temperature).toFixed(decimals); | 
					
						
							| 
									
										
										
										
											2016-03-29 13:28:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); |