Added fetchWeatherHourly functionality to Weather.gov provider (#2933)

Added fetchWeatherHourly functionality to:
 modules/default/weather/providers/weathergov.js
This commit is contained in:
dWoolridge 2022-10-06 02:56:32 -05:00 committed by GitHub
parent f434be3d44
commit a86e27a12c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 2 deletions

View File

@ -9,9 +9,12 @@ This project adheres to [Semantic Versioning](https://semver.org/).
_This release is scheduled to be released on 2023-01-01._ _This release is scheduled to be released on 2023-01-01._
Special thanks to: @rejas, @sdetweil
### Added ### Added
- Added test for remoteFile option in compliments module - Added test for remoteFile option in compliments module
- Added hourlyWeather functionality to Weather.gov weather provider
### Removed ### Removed

View File

@ -57,7 +57,7 @@ WeatherProvider.register("weathergov", {
// Overwrite the fetchCurrentWeather method. // Overwrite the fetchCurrentWeather method.
fetchCurrentWeather() { fetchCurrentWeather() {
if (!this.configURLs) { if (!this.configURLs) {
Log.info("fetch wx waiting on config URLs"); Log.info("fetchCurrentWeather: fetch wx waiting on config URLs");
return; return;
} }
this.fetchData(this.stationObsURL) this.fetchData(this.stationObsURL)
@ -78,7 +78,7 @@ WeatherProvider.register("weathergov", {
// Overwrite the fetchWeatherForecast method. // Overwrite the fetchWeatherForecast method.
fetchWeatherForecast() { fetchWeatherForecast() {
if (!this.configURLs) { if (!this.configURLs) {
Log.info("fetch wx waiting on config URLs"); Log.info("fetchWeatherForecast: fetch wx waiting on config URLs");
return; return;
} }
this.fetchData(this.forecastURL) this.fetchData(this.forecastURL)
@ -96,6 +96,28 @@ WeatherProvider.register("weathergov", {
.finally(() => this.updateAvailable()); .finally(() => this.updateAvailable());
}, },
// Overwrite the fetchWeatherHourly method.
fetchWeatherHourly() {
if (!this.configURLs) {
Log.info("fetchWeatherHourly: fetch wx waiting on config URLs");
return;
}
this.fetchData(this.forecastHourlyURL)
.then((data) => {
if (!data) {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
const hourly = this.generateWeatherObjectsFromHourly(data.properties.periods);
this.setWeatherHourly(hourly);
})
.catch(function (request) {
Log.error("Could not load data ... ", request);
})
.finally(() => this.updateAvailable());
},
/** Weather.gov Specific Methods - These are not part of the default provider methods */ /** Weather.gov Specific Methods - These are not part of the default provider methods */
/* /*
@ -130,14 +152,49 @@ WeatherProvider.register("weathergov", {
.finally(() => { .finally(() => {
// excellent, let's fetch some actual wx data // excellent, let's fetch some actual wx data
this.configURLs = true; this.configURLs = true;
// handle 'forecast' config, fall back to 'current' // handle 'forecast' config, fall back to 'current'
if (config.type === "forecast") { if (config.type === "forecast") {
this.fetchWeatherForecast(); this.fetchWeatherForecast();
} else if (config.type === "hourly") {
this.fetchWeatherHourly();
} else { } else {
this.fetchCurrentWeather(); this.fetchCurrentWeather();
} }
}); });
}, },
/*
* Generate a WeatherObject based on hourlyWeatherInformation
* Weather.gov API uses specific units; API does not include choice of units
* ... object needs data in units based on config!
*/
generateWeatherObjectsFromHourly(forecasts) {
const days = [];
// variable for date
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
for (const forecast of forecasts) {
weather.date = moment(forecast.startTime.slice(0, 19));
if (forecast.windSpeed.search(" ") < 0) {
weather.windSpeed = forecast.windSpeed;
} else {
weather.windSpeed = forecast.windSpeed.slice(0, forecast.windSpeed.search(" "));
}
weather.windDirection = this.convertWindDirection(forecast.windDirection);
weather.temperature = forecast.temperature;
weather.tempUnits = forecast.temperatureUnit;
// use the forecast isDayTime attribute to help build the weatherType label
weather.weatherType = this.convertWeatherType(forecast.shortForecast, forecast.isDaytime);
days.push(weather);
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
}
// push weather information to days array
days.push(weather);
return days;
},
/* /*
* Generate a WeatherObject based on currentWeatherInformation * Generate a WeatherObject based on currentWeatherInformation