2017-09-21 16:38:18 +02:00
|
|
|
/* global Module, WeatherProvider */
|
|
|
|
|
|
|
|
/* Magic Mirror
|
|
|
|
* Module: Weather
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Module.register("weather",{
|
|
|
|
// Default module config.
|
|
|
|
defaults: {
|
2017-09-22 12:26:47 +02:00
|
|
|
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/",
|
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 17:13:49 +01:00
|
|
|
tableClass: 'small',
|
2017-10-18 11:58:45 +02:00
|
|
|
|
|
|
|
onlyTemp: false,
|
2018-12-27 17:13:49 +01:00
|
|
|
roundTemp: false,
|
|
|
|
showRainAmount: true
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Module properties.
|
|
|
|
weatherProvider: null,
|
|
|
|
|
2017-09-22 12:26:47 +02:00
|
|
|
// Define required scripts.
|
|
|
|
getStyles: function() {
|
2017-10-18 13:38:56 +02:00
|
|
|
return ["font-awesome.css", "weather-icons.css", "weather.css"];
|
2017-09-22 12:26:47 +02:00
|
|
|
},
|
|
|
|
|
2017-09-21 16:38:18 +02:00
|
|
|
// Return the scripts that are nessecery for the weather module.
|
|
|
|
getScripts: function () {
|
|
|
|
var scripts = [
|
2018-05-21 10:56:46 +02:00
|
|
|
"moment.js",
|
2017-09-21 16:38:18 +02:00
|
|
|
"weatherprovider.js",
|
2017-09-22 12:26:47 +02:00
|
|
|
"weatherobject.js"
|
2017-09-21 16:38:18 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// Add the provider file to the required scripts.
|
2018-05-21 10:56:46 +02:00
|
|
|
scripts.push(this.file("providers/" + this.config.weatherProvider.toLowerCase() + ".js"));
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
return scripts
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-09-21 16:38:18 +02:00
|
|
|
// Start the weather module.
|
|
|
|
start: function () {
|
2018-12-27 17:13:49 +01:00
|
|
|
moment.locale(this.config.lang);
|
2017-09-21 16:38:18 +02:00
|
|
|
// Initialize the weather provider.
|
2018-05-21 10:56:46 +02:00
|
|
|
this.weatherProvider = WeatherProvider.initialize(this.config.weatherProvider, this);
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
// Let the weather provider know we are starting.
|
2018-05-21 10:56:46 +02:00
|
|
|
this.weatherProvider.start();
|
2017-09-21 16:38:18 +02:00
|
|
|
|
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
|
|
|
|
2017-09-22 12:26:47 +02:00
|
|
|
// Schedule the first update.
|
2018-05-21 10:56:46 +02:00
|
|
|
this.scheduleUpdate(0);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notification === "INDOOR_TEMPERATURE") {
|
|
|
|
this.indoorTemperature = this.roundValue(payload);
|
|
|
|
this.updateDom(300);
|
|
|
|
}
|
|
|
|
if (notification === "INDOOR_HUMIDITY") {
|
|
|
|
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 () {
|
2017-10-18 13:52:11 +02: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
|
|
|
}
|
2017-09-21 16:38:18 +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-07-02 15:43:24 +02:00
|
|
|
this.updateDom(300);
|
2018-12-27 17:13:49 +01:00
|
|
|
this.scheduleUpdate(60000);
|
2017-09-22 12:26:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-09-22 12:26:47 +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 += "°";
|
|
|
|
if (this.config.scale || this.config.degreeLabel) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2018-12-27 17:13:49 +01:00
|
|
|
|
|
|
|
this.nunjucksEnvironment().addFilter("formatRain", function(value) {
|
|
|
|
if (isNaN(value)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.config.units === "imperial") {
|
|
|
|
return (parseFloat(value) / 25.4).toFixed(2) + " in";
|
|
|
|
} else {
|
|
|
|
return parseFloat(value).toFixed(1) + " mm";
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2017-10-01 16:19:14 +02:00
|
|
|
}
|
2017-09-21 16:38:18 +02:00
|
|
|
});
|