mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
Use es6 notation in weather module and ukmet provider
This commit is contained in:
parent
bda8f26511
commit
2ababa521d
@ -81,6 +81,7 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
*/
|
*/
|
||||||
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
|
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
|
||||||
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||||
|
const location = currentWeatherData.SiteRep.DV.Location;
|
||||||
|
|
||||||
// data times are always UTC
|
// data times are always UTC
|
||||||
let nowUtc = moment.utc();
|
let nowUtc = moment.utc();
|
||||||
@ -88,8 +89,8 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
let timeInMins = nowUtc.diff(midnightUtc, "minutes");
|
let timeInMins = nowUtc.diff(midnightUtc, "minutes");
|
||||||
|
|
||||||
// loop round each of the (5) periods, look for today (the first period may be yesterday)
|
// loop round each of the (5) periods, look for today (the first period may be yesterday)
|
||||||
for (var i in currentWeatherData.SiteRep.DV.Location.Period) {
|
for (const period of location.Period) {
|
||||||
let periodDate = moment.utc(currentWeatherData.SiteRep.DV.Location.Period[i].value.substr(0, 10), "YYYY-MM-DD");
|
const periodDate = moment.utc(period.value.substr(0, 10), "YYYY-MM-DD");
|
||||||
|
|
||||||
// ignore if period is before today
|
// ignore if period is before today
|
||||||
if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) {
|
if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) {
|
||||||
@ -97,17 +98,17 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
if (moment().diff(periodDate, "minutes") > 0) {
|
if (moment().diff(periodDate, "minutes") > 0) {
|
||||||
// loop round the reports looking for the one we are in
|
// loop round the reports looking for the one we are in
|
||||||
// $ value specifies the time in minutes-of-the-day: 0, 180, 360,...1260
|
// $ value specifies the time in minutes-of-the-day: 0, 180, 360,...1260
|
||||||
for (var j in currentWeatherData.SiteRep.DV.Location.Period[i].Rep) {
|
for (const rep of period.Rep) {
|
||||||
let p = currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].$;
|
const p = rep.$;
|
||||||
if (timeInMins >= p && timeInMins - 180 < p) {
|
if (timeInMins >= p && timeInMins - 180 < p) {
|
||||||
// finally got the one we want, so populate weather object
|
// finally got the one we want, so populate weather object
|
||||||
currentWeather.humidity = currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].H;
|
currentWeather.humidity = rep.H;
|
||||||
currentWeather.temperature = this.convertTemp(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].T);
|
currentWeather.temperature = this.convertTemp(rep.T);
|
||||||
currentWeather.feelsLikeTemp = this.convertTemp(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].F);
|
currentWeather.feelsLikeTemp = this.convertTemp(rep.F);
|
||||||
currentWeather.precipitation = parseInt(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].Pp);
|
currentWeather.precipitation = parseInt(rep.Pp);
|
||||||
currentWeather.windSpeed = this.convertWindSpeed(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].S);
|
currentWeather.windSpeed = this.convertWindSpeed(rep.S);
|
||||||
currentWeather.windDirection = this.convertWindDirection(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].D);
|
currentWeather.windDirection = this.convertWindDirection(rep.D);
|
||||||
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].W);
|
currentWeather.weatherType = this.convertWeatherType(rep.W);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,7 +116,7 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// determine the sunrise/sunset times - not supplied in UK Met Office data
|
// determine the sunrise/sunset times - not supplied in UK Met Office data
|
||||||
let times = this.calcAstroData(currentWeatherData.SiteRep.DV.Location);
|
let times = this.calcAstroData(location);
|
||||||
currentWeather.sunrise = times[0];
|
currentWeather.sunrise = times[0];
|
||||||
currentWeather.sunset = times[1];
|
currentWeather.sunset = times[1];
|
||||||
|
|
||||||
@ -130,21 +131,21 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
|
|
||||||
// loop round the (5) periods getting the data
|
// loop round the (5) periods getting the data
|
||||||
// for each period array, Day is [0], Night is [1]
|
// for each period array, Day is [0], Night is [1]
|
||||||
for (var j in forecasts.SiteRep.DV.Location.Period) {
|
for (const period of forecasts.SiteRep.DV.Location.Period) {
|
||||||
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||||
|
|
||||||
// data times are always UTC
|
// data times are always UTC
|
||||||
const dateStr = forecasts.SiteRep.DV.Location.Period[j].value;
|
const dateStr = period.value;
|
||||||
let periodDate = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD");
|
let periodDate = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD");
|
||||||
|
|
||||||
// ignore if period is before today
|
// ignore if period is before today
|
||||||
if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) {
|
if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) {
|
||||||
// populate the weather object
|
// populate the weather object
|
||||||
weather.date = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD");
|
weather.date = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD");
|
||||||
weather.minTemperature = this.convertTemp(forecasts.SiteRep.DV.Location.Period[j].Rep[1].Nm);
|
weather.minTemperature = this.convertTemp(period.Rep[1].Nm);
|
||||||
weather.maxTemperature = this.convertTemp(forecasts.SiteRep.DV.Location.Period[j].Rep[0].Dm);
|
weather.maxTemperature = this.convertTemp(period.Rep[0].Dm);
|
||||||
weather.weatherType = this.convertWeatherType(forecasts.SiteRep.DV.Location.Period[j].Rep[0].W);
|
weather.weatherType = this.convertWeatherType(period.Rep[0].W);
|
||||||
weather.precipitation = parseInt(forecasts.SiteRep.DV.Location.Period[j].Rep[0].PPd);
|
weather.precipitation = parseInt(period.Rep[0].PPd);
|
||||||
|
|
||||||
days.push(weather);
|
days.push(weather);
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,9 @@ Module.register("weather", {
|
|||||||
// Module properties.
|
// Module properties.
|
||||||
weatherProvider: null,
|
weatherProvider: null,
|
||||||
|
|
||||||
|
// Can be used by the provider to display location of event if nothing else is specified
|
||||||
|
firstEvent: null,
|
||||||
|
|
||||||
// Define required scripts.
|
// Define required scripts.
|
||||||
getStyles: function () {
|
getStyles: function () {
|
||||||
return ["font-awesome.css", "weather-icons.css", "weather.css"];
|
return ["font-awesome.css", "weather-icons.css", "weather.css"];
|
||||||
@ -88,15 +91,13 @@ Module.register("weather", {
|
|||||||
// Override notification handler.
|
// Override notification handler.
|
||||||
notificationReceived: function (notification, payload, sender) {
|
notificationReceived: function (notification, payload, sender) {
|
||||||
if (notification === "CALENDAR_EVENTS") {
|
if (notification === "CALENDAR_EVENTS") {
|
||||||
var senderClasses = sender.data.classes.toLowerCase().split(" ");
|
const senderClasses = sender.data.classes.toLowerCase().split(" ");
|
||||||
if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) {
|
if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) {
|
||||||
this.firstEvent = false;
|
this.firstEvent = null;
|
||||||
|
for (let event of payload) {
|
||||||
for (var e in payload) {
|
|
||||||
var event = payload[e];
|
|
||||||
if (event.location || event.geo) {
|
if (event.location || event.geo) {
|
||||||
this.firstEvent = event;
|
this.firstEvent = event;
|
||||||
//Log.log("First upcoming event with location: ", event);
|
Log.debug("First upcoming event with location: ", event);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,15 +115,15 @@ Module.register("weather", {
|
|||||||
getTemplate: function () {
|
getTemplate: function () {
|
||||||
switch (this.config.type.toLowerCase()) {
|
switch (this.config.type.toLowerCase()) {
|
||||||
case "current":
|
case "current":
|
||||||
return `current.njk`;
|
return "current.njk";
|
||||||
case "hourly":
|
case "hourly":
|
||||||
return `hourly.njk`;
|
return "hourly.njk";
|
||||||
case "daily":
|
case "daily":
|
||||||
case "forecast":
|
case "forecast":
|
||||||
return `forecast.njk`;
|
return "forecast.njk";
|
||||||
//Make the invalid values use the "Loading..." from forecast
|
//Make the invalid values use the "Loading..." from forecast
|
||||||
default:
|
default:
|
||||||
return `forecast.njk`;
|
return "forecast.njk";
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -152,7 +153,7 @@ Module.register("weather", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
scheduleUpdate: function (delay = null) {
|
scheduleUpdate: function (delay = null) {
|
||||||
var nextLoad = this.config.updateInterval;
|
let nextLoad = this.config.updateInterval;
|
||||||
if (delay !== null && delay >= 0) {
|
if (delay !== null && delay >= 0) {
|
||||||
nextLoad = delay;
|
nextLoad = delay;
|
||||||
}
|
}
|
||||||
@ -176,8 +177,8 @@ Module.register("weather", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
roundValue: function (temperature) {
|
roundValue: function (temperature) {
|
||||||
var decimals = this.config.roundTemp ? 0 : 1;
|
const decimals = this.config.roundTemp ? 0 : 1;
|
||||||
var roundValue = parseFloat(temperature).toFixed(decimals);
|
const roundValue = parseFloat(temperature).toFixed(decimals);
|
||||||
return roundValue === "-0" ? 0 : roundValue;
|
return roundValue === "-0" ? 0 : roundValue;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -272,8 +273,8 @@ Module.register("weather", {
|
|||||||
if (this.config.fadePoint < 0) {
|
if (this.config.fadePoint < 0) {
|
||||||
this.config.fadePoint = 0;
|
this.config.fadePoint = 0;
|
||||||
}
|
}
|
||||||
var startingPoint = numSteps * this.config.fadePoint;
|
const startingPoint = numSteps * this.config.fadePoint;
|
||||||
var numFadesteps = numSteps - startingPoint;
|
const numFadesteps = numSteps - startingPoint;
|
||||||
if (currentStep >= startingPoint) {
|
if (currentStep >= startingPoint) {
|
||||||
return 1 - (currentStep - startingPoint) / numFadesteps;
|
return 1 - (currentStep - startingPoint) / numFadesteps;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user