Add common method for determining if it is daytime

This commit is contained in:
rejas 2021-08-31 21:55:43 +02:00
parent 8949aa3bec
commit a7684e3e9f
2 changed files with 16 additions and 14 deletions

View File

@ -105,7 +105,8 @@ WeatherProvider.register("smhi", {
* @returns {WeatherObject} The converted weatherdata at the specified location * @returns {WeatherObject} The converted weatherdata at the specified location
*/ */
convertWeatherDataToObject(weatherData, coordinates) { convertWeatherDataToObject(weatherData, coordinates) {
let currentWeather = new WeatherObject("metric", "metric", "metric"); //Weather data is only for Sweden and nobody in Sweden would use imperial // Weather data is only for Sweden and nobody in Sweden would use imperial
let currentWeather = new WeatherObject("metric", "metric", "metric");
currentWeather.date = moment(weatherData.validTime); currentWeather.date = moment(weatherData.validTime);
let times = SunCalc.getTimes(currentWeather.date.toDate(), coordinates.lat, coordinates.lon); let times = SunCalc.getTimes(currentWeather.date.toDate(), coordinates.lat, coordinates.lon);
@ -115,9 +116,11 @@ WeatherProvider.register("smhi", {
currentWeather.temperature = this.paramValue(weatherData, "t"); currentWeather.temperature = this.paramValue(weatherData, "t");
currentWeather.windSpeed = this.paramValue(weatherData, "ws"); currentWeather.windSpeed = this.paramValue(weatherData, "ws");
currentWeather.windDirection = this.paramValue(weatherData, "wd"); currentWeather.windDirection = this.paramValue(weatherData, "wd");
currentWeather.weatherType = this.convertWeatherType(this.paramValue(weatherData, "Wsymb2"), this.isDayTime(currentWeather)); currentWeather.weatherType = this.convertWeatherType(this.paramValue(weatherData, "Wsymb2"), currentWeather.isDayTime());
//Determine the precipitation amount and category and update the weatherObject with it, the valuetype to use can be configured or uses median as default. // Determine the precipitation amount and category and update the
// weatherObject with it, the valuetype to use can be configured or uses
// median as default.
let precipitationValue = this.paramValue(weatherData, this.config.precipitationValue); let precipitationValue = this.paramValue(weatherData, this.config.precipitationValue);
switch (this.paramValue(weatherData, "pcat")) { switch (this.paramValue(weatherData, "pcat")) {
// 0 = No precipitation // 0 = No precipitation
@ -171,7 +174,7 @@ WeatherProvider.register("smhi", {
} }
//Keep track of what icons has been used for each hour of daytime and use the middle one for the forecast //Keep track of what icons has been used for each hour of daytime and use the middle one for the forecast
if (this.isDayTime(weatherObject)) { if (weatherObject.isDayTime()) {
dayWeatherTypes.push(weatherObject.weatherType); dayWeatherTypes.push(weatherObject.weatherType);
} }
if (dayWeatherTypes.length > 0) { if (dayWeatherTypes.length > 0) {
@ -201,16 +204,6 @@ WeatherProvider.register("smhi", {
return { lat: data.geometry.coordinates[0][1], lon: data.geometry.coordinates[0][0] }; return { lat: data.geometry.coordinates[0][1], lon: data.geometry.coordinates[0][0] };
}, },
/**
* Checks if the weatherObject is at dayTime.
*
* @param {WeatherObject} weatherObject The weatherObject to look at
* @returns {boolean} true if it is at dayTime
*/
isDayTime(weatherObject) {
return weatherObject.date.isBetween(weatherObject.sunrise, weatherObject.sunset, undefined, "[]");
},
/** /**
* The distance between the data points is increasing in the data the more distant the prediction is. * The distance between the data points is increasing in the data the more distant the prediction is.
* Find these gaps and fill them with the previous hours data to make the data returned a complete set. * Find these gaps and fill them with the previous hours data to make the data returned a complete set.

View File

@ -116,6 +116,15 @@ class WeatherObject {
return this.tempUnits === "imperial" ? feelsLike : ((feelsLike - 32) * 5) / 9; return this.tempUnits === "imperial" ? feelsLike : ((feelsLike - 32) * 5) / 9;
} }
/**
* Checks if the weatherObject is at dayTime.
*
* @returns {boolean} true if it is at dayTime
*/
isDayTime() {
return this.date.isBetween(this.sunrise, this.sunset, undefined, "[]");
}
/** /**
* Update the sunrise / sunset time depending on the location * Update the sunrise / sunset time depending on the location
* *