151 lines
3.3 KiB
JavaScript
Raw Normal View History

/* global Module, WeatherProvider */
/* Magic Mirror
* Module: Weather
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.register("weather",{
// Default module config.
defaults: {
updateInterval: 10 * 60 * 1000,
weatherProvider: "openweathermap",
units: config.units,
2017-09-22 13:26:44 +02:00
roundTemp: false,
2017-10-18 11:58:45 +02:00
type: "current", //current, forecast
//
location: false,
locationID: false,
appid: "",
units: config.units,
updateInterval: 10 * 60 * 1000, // every 10 minutes
animationSpeed: 1000,
timeFormat: config.timeFormat,
showPeriod: true,
showPeriodUpper: false,
showWindDirection: true,
showWindDirectionAsArrow: false,
useBeaufort: true,
lang: config.language,
showHumidity: false,
degreeLabel: false,
showIndoorTemperature: false,
showIndoorHumidity: false,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
apiVersion: "2.5",
apiBase: "http://api.openweathermap.org/data/",
weatherEndpoint: "weather",
appendLocationNameToHeader: true,
calendarClass: "calendar",
onlyTemp: false,
roundTemp: false
},
// Module properties.
weatherProvider: null,
// Define required scripts.
getStyles: function() {
2017-10-18 13:38:56 +02:00
return ["font-awesome.css", "weather-icons.css", "weather.css"];
},
// Return the scripts that are nessecery for the weather module.
getScripts: function () {
var scripts = [
"weatherprovider.js",
"weatherobject.js"
];
// Add the provider file to the required scripts.
scripts.push(this.file("providers/" + this.config.weatherProvider.toLowerCase() + ".js"))
return scripts
},
// Start the weather module.
start: function () {
// Initialize the weather provider.
this.weatherProvider = WeatherProvider.initialize(this.config.weatherProvider, this)
// Let the weather provider know we are starting.
this.weatherProvider.start()
2017-10-18 13:38:56 +02:00
// Add custom filters
this.addFilters()
// Schedule the first update.
this.scheduleUpdate(0)
},
2017-10-01 13:50:15 +02:00
// Select the template depending on the display type.
getTemplate: function () {
return this.config.type.toLowerCase() + ".html"
},
// Add all the data to the template.
getTemplateData: function () {
return {
config: this.config,
current: this.weatherProvider.currentWeather(),
2017-10-18 12:19:02 +02:00
forecast: this.weatherProvider.weatherForecast()
2017-09-22 13:26:44 +02:00
}
},
// What to do when the weather provider has new information available?
updateAvailable: function() {
Log.log("New weather information available.")
console.info(this.weatherProvider.currentWeather())
this.updateDom(0)
this.scheduleUpdate()
},
scheduleUpdate: function(delay = null) {
var nextLoad = this.config.updateInterval;
if (delay !== null && delay >= 0) {
nextLoad = delay;
}
setTimeout(() => {
2017-10-01 13:50:15 +02:00
switch (this.config.type) {
2017-09-22 13:26:44 +02:00
case "forecast":
this.weatherProvider.fetchWeatherForecast()
break;
default:
2017-10-01 13:50:15 +02:00
case "current":
2017-09-22 13:26:44 +02:00
this.weatherProvider.fetchCurrentWeather()
break;
}
}, nextLoad);
2017-10-18 13:38:56 +02:00
},
addFilters() {
var self = this
this.nunjucksEnvironment().addFilter("formatTime", function(date) {
date = moment(date)
if (self.config.timeFormat !== 24) {
if (self.config.showPeriod) {
if (self.config.showPeriodUpper) {
return date.format("h:mm A")
} else {
return date.format("h:mm a")
}
} else {
return date.format("h:mm")
}
}
return date.format("HH:mm")
});
2017-10-01 16:19:14 +02:00
}
});