mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-26 08:01:29 +00:00
Use metric units internally in all weatherproviders (#2849)
So finally I think this refactorin is ready to be reviewed :-) DONE: - [x] Removed all conversion functions for wind and temperature from specific weatherproviders - [x] Use internally only metric units: celsius for temperature, meters per seconds for wind - [x] Convert temp and wind into the configured units when displaying data on the UI - [x] look how beaufort calculation uses metrics, added knots as new windunit - [x] add more e2e tests Checked providers: - [x] Darksky - [x] EnvCanada - [x] OpenWeatherMap - [x] SMHI provider - [x] UK Met Office - [x] UK Met Office DataHub - [x] WeatherBit - [x] WeatherFlow - [x] WeatherGov TODO in different tickets: - check weatherproviders for usage of weatherEndpoint (as seen in https://github.com/MichMich/MagicMirror-Documentation/issues/131) -> see #2926 - cleanup precipations -> #2953 Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
@@ -131,8 +131,8 @@ WeatherProvider.register("weathergov", {
|
||||
}
|
||||
this.fetchedLocationName = data.properties.relativeLocation.properties.city + ", " + data.properties.relativeLocation.properties.state;
|
||||
Log.log("Forecast location is " + this.fetchedLocationName);
|
||||
this.forecastURL = data.properties.forecast;
|
||||
this.forecastHourlyURL = data.properties.forecastHourly;
|
||||
this.forecastURL = data.properties.forecast + "?units=si";
|
||||
this.forecastHourlyURL = data.properties.forecastHourly + "?units=si";
|
||||
this.forecastGridDataURL = data.properties.forecastGridData;
|
||||
this.observationStationsURL = data.properties.observationStations;
|
||||
// with this URL, we chain another promise for the station obs URL
|
||||
@@ -171,7 +171,7 @@ WeatherProvider.register("weathergov", {
|
||||
const days = [];
|
||||
|
||||
// variable for date
|
||||
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||
let weather = new WeatherObject();
|
||||
for (const forecast of forecasts) {
|
||||
weather.date = moment(forecast.startTime.slice(0, 19));
|
||||
if (forecast.windSpeed.search(" ") < 0) {
|
||||
@@ -187,7 +187,7 @@ WeatherProvider.register("weathergov", {
|
||||
|
||||
days.push(weather);
|
||||
|
||||
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||
weather = new WeatherObject();
|
||||
}
|
||||
|
||||
// push weather information to days array
|
||||
@@ -201,24 +201,24 @@ WeatherProvider.register("weathergov", {
|
||||
* ... object needs data in units based on config!
|
||||
*/
|
||||
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
|
||||
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||
const currentWeather = new WeatherObject();
|
||||
|
||||
currentWeather.date = moment(currentWeatherData.timestamp);
|
||||
currentWeather.temperature = this.convertTemp(currentWeatherData.temperature.value);
|
||||
currentWeather.windSpeed = this.convertSpeed(currentWeatherData.windSpeed.value);
|
||||
currentWeather.temperature = currentWeatherData.temperature.value;
|
||||
currentWeather.windSpeed = currentWeather.convertWindToMs(currentWeatherData.windSpeed.value);
|
||||
currentWeather.windDirection = currentWeatherData.windDirection.value;
|
||||
currentWeather.minTemperature = this.convertTemp(currentWeatherData.minTemperatureLast24Hours.value);
|
||||
currentWeather.maxTemperature = this.convertTemp(currentWeatherData.maxTemperatureLast24Hours.value);
|
||||
currentWeather.minTemperature = currentWeatherData.minTemperatureLast24Hours.value;
|
||||
currentWeather.maxTemperature = currentWeatherData.maxTemperatureLast24Hours.value;
|
||||
currentWeather.humidity = Math.round(currentWeatherData.relativeHumidity.value);
|
||||
currentWeather.rain = null;
|
||||
currentWeather.snow = null;
|
||||
currentWeather.precipitation = this.convertLength(currentWeatherData.precipitationLastHour.value);
|
||||
if (currentWeatherData.heatIndex.value !== null) {
|
||||
currentWeather.feelsLikeTemp = this.convertTemp(currentWeatherData.heatIndex.value);
|
||||
currentWeather.feelsLikeTemp = currentWeatherData.heatIndex.value;
|
||||
} else if (currentWeatherData.windChill.value !== null) {
|
||||
currentWeather.feelsLikeTemp = this.convertTemp(currentWeatherData.windChill.value);
|
||||
currentWeather.feelsLikeTemp = currentWeatherData.windChill.value;
|
||||
} else {
|
||||
currentWeather.feelsLikeTemp = this.convertTemp(currentWeatherData.temperature.value);
|
||||
currentWeather.feelsLikeTemp = currentWeatherData.temperature.value;
|
||||
}
|
||||
// determine the sunrise/sunset times - not supplied in weather.gov data
|
||||
currentWeather.updateSunTime(this.config.lat, this.config.lon);
|
||||
@@ -247,7 +247,7 @@ WeatherProvider.register("weathergov", {
|
||||
let maxTemp = [];
|
||||
// variable for date
|
||||
let date = "";
|
||||
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||
let weather = new WeatherObject();
|
||||
weather.precipitation = 0;
|
||||
|
||||
for (const forecast of forecasts) {
|
||||
@@ -259,7 +259,7 @@ WeatherProvider.register("weathergov", {
|
||||
// push weather information to days array
|
||||
days.push(weather);
|
||||
// create new weather-object
|
||||
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
||||
weather = new WeatherObject();
|
||||
|
||||
minTemp = [];
|
||||
maxTemp = [];
|
||||
@@ -298,26 +298,6 @@ WeatherProvider.register("weathergov", {
|
||||
/*
|
||||
* Unit conversions
|
||||
*/
|
||||
// conversion to fahrenheit
|
||||
convertTemp(temp) {
|
||||
if (this.config.tempUnits === "imperial") {
|
||||
return (9 / 5) * temp + 32;
|
||||
} else {
|
||||
return temp;
|
||||
}
|
||||
},
|
||||
// conversion to mph or kmh
|
||||
convertSpeed(metSec) {
|
||||
if (this.config.windUnits === "imperial") {
|
||||
return metSec * 2.23694;
|
||||
} else {
|
||||
if (this.config.useKmh) {
|
||||
return metSec * 3.6;
|
||||
} else {
|
||||
return metSec;
|
||||
}
|
||||
}
|
||||
},
|
||||
// conversion to inches
|
||||
convertLength(meters) {
|
||||
if (this.config.units === "imperial") {
|
||||
@@ -395,31 +375,5 @@ WeatherProvider.register("weathergov", {
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
/*
|
||||
Convert the direction into Degrees
|
||||
*/
|
||||
convertWindDirection(windDirection) {
|
||||
const windCardinals = {
|
||||
N: 0,
|
||||
NNE: 22,
|
||||
NE: 45,
|
||||
ENE: 67,
|
||||
E: 90,
|
||||
ESE: 112,
|
||||
SE: 135,
|
||||
SSE: 157,
|
||||
S: 180,
|
||||
SSW: 202,
|
||||
SW: 225,
|
||||
WSW: 247,
|
||||
W: 270,
|
||||
WNW: 292,
|
||||
NW: 315,
|
||||
NNW: 337
|
||||
};
|
||||
|
||||
return windCardinals.hasOwnProperty(windDirection) ? windCardinals[windDirection] : null;
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user