mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-21 12:55:22 +00:00
Fix API version in yr weather provider call (#3223)
Fixes #3227 and also - removes unused code - de-duplicates code fragments - fixes typos - inlines code - adds more weather util tests @martingron and @Justheretoreportanissue would you be so kind to check if I didnt mess anything up?
This commit is contained in:
@@ -18,6 +18,8 @@ WeatherProvider.register("yr", {
|
||||
defaults: {
|
||||
useCorsProxy: true,
|
||||
apiBase: "https://api.met.no/weatherapi",
|
||||
forecastApiVersion: "3.0",
|
||||
sunriseApiVersion: "3.0",
|
||||
altitude: 0,
|
||||
currentForecastHours: 1 //1, 6 or 12
|
||||
},
|
||||
@@ -44,8 +46,7 @@ WeatherProvider.register("yr", {
|
||||
},
|
||||
|
||||
async getCurrentWeather() {
|
||||
const getRequests = [this.getWeatherData(), this.getStellarData()];
|
||||
const [weatherData, stellarData] = await Promise.all(getRequests);
|
||||
const [weatherData, stellarData] = await Promise.all([this.getWeatherData(), this.getStellarData()]);
|
||||
if (!stellarData) {
|
||||
Log.warn("No stellar data available.");
|
||||
}
|
||||
@@ -170,7 +171,7 @@ WeatherProvider.register("yr", {
|
||||
});
|
||||
},
|
||||
|
||||
getForecastUrl() {
|
||||
getConfigOptions() {
|
||||
if (!this.config.lat) {
|
||||
Log.error("Latitude not provided.");
|
||||
throw new Error("Latitude not provided.");
|
||||
@@ -183,6 +184,11 @@ WeatherProvider.register("yr", {
|
||||
let lat = this.config.lat.toString();
|
||||
let lon = this.config.lon.toString();
|
||||
const altitude = this.config.altitude ?? 0;
|
||||
return { lat, lon, altitude };
|
||||
},
|
||||
|
||||
getForecastUrl() {
|
||||
let { lat, lon, altitude } = this.getConfigOptions();
|
||||
|
||||
if (lat.includes(".") && lat.split(".")[1].length > 4) {
|
||||
Log.warn("Latitude is too specific for weather data. Do not use more than four decimals. Trimming to maximum length.");
|
||||
@@ -195,18 +201,13 @@ WeatherProvider.register("yr", {
|
||||
lon = `${lonParts[0]}.${lonParts[1].substring(0, 4)}`;
|
||||
}
|
||||
|
||||
return `${this.config.apiBase}/locationforecast/2.0/complete?&altitude=${altitude}&lat=${lat}&lon=${lon}`;
|
||||
return `${this.config.apiBase}/locationforecast/${this.config.forecastApiVersion}/complete?&altitude=${altitude}&lat=${lat}&lon=${lon}`;
|
||||
},
|
||||
|
||||
cacheWeatherData(weatherData) {
|
||||
localStorage.setItem("weatherData", JSON.stringify(weatherData));
|
||||
},
|
||||
|
||||
getAuthenticationString() {
|
||||
if (!this.config.authenticationEmail) throw new Error("Authentication email not provided.");
|
||||
return `${this.config.applicaitionName} ${this.config.authenticationEmail}`;
|
||||
},
|
||||
|
||||
getStellarData() {
|
||||
// If a user has several Yr-modules, for instance one current and one forecast, the API calls must be synchronized across classes.
|
||||
// This is to avoid multiple similar calls to the API.
|
||||
@@ -302,7 +303,7 @@ WeatherProvider.register("yr", {
|
||||
|
||||
getStellarDataFromYr(date, days = 1) {
|
||||
const requestHeaders = [{ name: "Accept", value: "application/json" }];
|
||||
return this.fetchData(this.getStellarDatatUrl(date, days), "json", requestHeaders)
|
||||
return this.fetchData(this.getStellarDataUrl(date, days), "json", requestHeaders)
|
||||
.then((data) => {
|
||||
Log.debug("Got stellar data from yr.");
|
||||
return data;
|
||||
@@ -313,19 +314,8 @@ WeatherProvider.register("yr", {
|
||||
});
|
||||
},
|
||||
|
||||
getStellarDatatUrl(date, days) {
|
||||
if (!this.config.lat) {
|
||||
Log.error("Latitude not provided.");
|
||||
throw new Error("Latitude not provided.");
|
||||
}
|
||||
if (!this.config.lon) {
|
||||
Log.error("Longitude not provided.");
|
||||
throw new Error("Longitude not provided.");
|
||||
}
|
||||
|
||||
let lat = this.config.lat.toString();
|
||||
let lon = this.config.lon.toString();
|
||||
const altitude = this.config.altitude ?? 0;
|
||||
getStellarDataUrl(date, days) {
|
||||
let { lat, lon, altitude } = this.getConfigOptions();
|
||||
|
||||
if (lat.includes(".") && lat.split(".")[1].length > 4) {
|
||||
Log.warn("Latitude is too specific for stellar data. Do not use more than four decimals. Trimming to maximum length.");
|
||||
@@ -352,7 +342,7 @@ WeatherProvider.register("yr", {
|
||||
if (hours.length < 2) {
|
||||
hours = `0${hours}`;
|
||||
}
|
||||
return `${this.config.apiBase}/sunrise/2.3/sun?lat=${lat}&lon=${lon}&date=${date}&offset=${utcOffsetPrefix}${hours}%3A${minutes}`;
|
||||
return `${this.config.apiBase}/sunrise/${this.config.sunriseApiVersion}/sun?lat=${lat}&lon=${lon}&date=${date}&offset=${utcOffsetPrefix}${hours}%3A${minutes}`;
|
||||
},
|
||||
|
||||
cacheStellarData(data) {
|
||||
@@ -472,15 +462,6 @@ WeatherProvider.register("yr", {
|
||||
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
|
||||
},
|
||||
|
||||
getStellarTimesFrom(stellarData, date) {
|
||||
for (const time of stellarData.location.time) {
|
||||
if (time.date === date) {
|
||||
return time;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
|
||||
getForecastForXHoursFrom(weather) {
|
||||
if (this.config.currentForecastHours === 1) {
|
||||
if (weather.next_1_hours) {
|
||||
@@ -522,8 +503,7 @@ WeatherProvider.register("yr", {
|
||||
},
|
||||
|
||||
async getWeatherForecast(type) {
|
||||
const getRequests = [this.getWeatherData(), this.getStellarData()];
|
||||
const [weatherData, stellarData] = await Promise.all(getRequests);
|
||||
const [weatherData, stellarData] = await Promise.all([this.getWeatherData(), this.getStellarData()]);
|
||||
if (!weatherData.properties.timeseries || !weatherData.properties.timeseries[0]) {
|
||||
Log.error("No weather data available.");
|
||||
return;
|
||||
@@ -573,7 +553,7 @@ WeatherProvider.register("yr", {
|
||||
return days;
|
||||
}, Object.create(null));
|
||||
|
||||
Object.keys(days).forEach(function (time, index) {
|
||||
Object.keys(days).forEach(function (time) {
|
||||
let minTemperature = undefined;
|
||||
let maxTemperature = undefined;
|
||||
|
||||
|
Reference in New Issue
Block a user