213 lines
5.2 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",
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/",
2018-12-27 17:13:49 +01:00
weatherEndpoint: "/weather",
2017-10-18 11:58:45 +02:00
appendLocationNameToHeader: true,
calendarClass: "calendar",
2018-12-27 18:52:35 +01:00
tableClass: "small",
2017-10-18 11:58:45 +02:00
onlyTemp: false,
2018-12-27 18:52:35 +01:00
showRainAmount: true,
colored: false,
showFeelsLike: true
},
// 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 () {
2018-12-27 19:37:02 +01:00
return [
2018-05-21 10:56:46 +02:00
"moment.js",
"weatherprovider.js",
2018-12-27 19:37:02 +01:00
"weatherobject.js",
this.file("providers/" + this.config.weatherProvider.toLowerCase() + ".js")
];
},
2018-12-27 17:13:49 +01:00
// Override getHeader method.
getHeader: function() {
if (this.config.appendLocationNameToHeader && this.weatherProvider) {
return this.data.header + " " + this.weatherProvider.fetchedLocation();
}
return this.data.header;
},
// Start the weather module.
start: function () {
2018-12-27 17:13:49 +01:00
moment.locale(this.config.lang);
// Initialize the weather provider.
2018-05-21 10:56:46 +02:00
this.weatherProvider = WeatherProvider.initialize(this.config.weatherProvider, this);
// Let the weather provider know we are starting.
2018-05-21 10:56:46 +02:00
this.weatherProvider.start();
2017-10-18 13:38:56 +02:00
// Add custom filters
2018-05-21 10:56:46 +02:00
this.addFilters();
2017-10-18 13:38:56 +02:00
// Schedule the first update.
2018-12-27 19:37:02 +01:00
this.scheduleUpdate(this.config.initialLoadDelay);
},
2018-07-02 15:43:24 +02:00
// Override notification handler.
notificationReceived: function(notification, payload, sender) {
if (notification === "CALENDAR_EVENTS") {
var senderClasses = sender.data.classes.toLowerCase().split(" ");
if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) {
this.firstEvent = false;
for (var e in payload) {
var event = payload[e];
if (event.location || event.geo) {
this.firstEvent = event;
//Log.log("First upcoming event with location: ", event);
break;
}
}
}
2018-12-27 19:37:02 +01:00
} else if (notification === "INDOOR_TEMPERATURE") {
2018-07-02 15:43:24 +02:00
this.indoorTemperature = this.roundValue(payload);
this.updateDom(300);
2018-12-27 19:37:02 +01:00
} else if (notification === "INDOOR_HUMIDITY") {
2018-07-02 15:43:24 +02:00
this.indoorHumidity = this.roundValue(payload);
this.updateDom(300);
}
},
2017-10-01 13:50:15 +02:00
// Select the template depending on the display type.
getTemplate: function () {
2018-12-27 19:37:02 +01:00
return `${this.config.type.toLowerCase()}.njk`;
2017-10-01 13:50:15 +02:00
},
// Add all the data to the template.
getTemplateData: function () {
return {
config: this.config,
current: this.weatherProvider.currentWeather(),
2018-07-02 15:43:24 +02:00
forecast: this.weatherProvider.weatherForecast(),
indoor: {
humidity: this.indoorHumidity,
temperature: this.indoorTemperature
}
2017-09-22 13:26:44 +02:00
}
},
// What to do when the weather provider has new information available?
updateAvailable: function() {
2018-05-21 10:56:46 +02:00
Log.log("New weather information available.");
2018-12-27 19:37:02 +01:00
this.updateDom(0);
this.scheduleUpdate();
},
scheduleUpdate: function(delay = null) {
var nextLoad = this.config.updateInterval;
if (delay !== null && delay >= 0) {
nextLoad = delay;
}
setTimeout(() => {
2018-07-02 15:43:24 +02:00
if (this.config.type === "forecast") {
2018-05-21 10:56:46 +02:00
this.weatherProvider.fetchWeatherForecast();
2018-07-02 15:43:24 +02:00
} else {
2018-05-21 10:56:46 +02:00
this.weatherProvider.fetchCurrentWeather();
2017-09-22 13:26:44 +02:00
}
}, nextLoad);
2017-10-18 13:38:56 +02:00
},
2018-07-02 15:43:24 +02:00
roundValue: function(temperature) {
var decimals = this.config.roundTemp ? 0 : 1;
return parseFloat(temperature).toFixed(decimals);
2018-12-27 17:13:49 +01:00
},
2018-07-02 15:43:24 +02:00
2017-10-18 13:38:56 +02:00
addFilters() {
this.nunjucksEnvironment().addFilter("formatTime", function(date) {
2018-05-21 10:56:46 +02:00
date = moment(date);
2017-10-18 13:38:56 +02:00
2018-05-21 10:56:46 +02:00
if (this.config.timeFormat !== 24) {
if (this.config.showPeriod) {
if (this.config.showPeriodUpper) {
return date.format("h:mm A");
2017-10-18 13:38:56 +02:00
} else {
2018-05-21 10:56:46 +02:00
return date.format("h:mm a");
2017-10-18 13:38:56 +02:00
}
} else {
2018-05-21 10:56:46 +02:00
return date.format("h:mm");
}
}
return date.format("HH:mm");
}.bind(this));
this.nunjucksEnvironment().addFilter("unit", function (value, type) {
if (type === "temperature") {
value += "°";
2018-12-27 18:52:35 +01:00
if (this.config.degreeLabel) {
2018-05-21 10:56:46 +02:00
if (this.config.units === "metric") {
value += "C";
} else if (this.config.units === "imperial") {
value += "F";
} else {
value += "K";
}
2017-10-18 13:38:56 +02:00
}
} else if (type === "rain") {
if (isNaN(value)) {
value = "";
} else {
value = `${value.toFixed(2)} ${this.config.units === "imperial" ? "in" : "mm"}`;
}
2017-10-18 13:38:56 +02:00
}
2018-05-21 10:56:46 +02:00
return value;
}.bind(this));
2018-07-02 15:43:24 +02:00
this.nunjucksEnvironment().addFilter("roundValue", function(value) {
return this.roundValue(value);
}.bind(this));
2017-10-01 16:19:14 +02:00
}
});