better latest observation get data routine

This commit is contained in:
Matt Walsh 2022-12-22 14:46:58 -06:00
parent 25291efff5
commit 03dfbc462b
3 changed files with 43 additions and 21 deletions

View file

@ -32,26 +32,20 @@ class LatestObservations extends WeatherDisplay {
const regionalStations = sortedStations.slice(0, 30); const regionalStations = sortedStations.slice(0, 30);
// get data for regional stations // get data for regional stations
const allConditions = await Promise.all(regionalStations.map(async (station) => { // get first 7 stations
try { const actualConditions = [];
const data = await json(`https://api.weather.gov/stations/${station.id}/observations/latest`, { retryCount: 3, stillWaiting: () => this.stillWaiting() }); let lastStation = Math.min(regionalStations.length, 7);
// test for temperature, weather and wind values present let firstStation = 0;
if (data.properties.temperature.value === null while (actualConditions.length < 7 && (lastStation) <= regionalStations.length) {
|| data.properties.textDescription === '' // eslint-disable-next-line no-await-in-loop
|| data.properties.windSpeed.value === null) return false; const someStations = await getStations(regionalStations.slice(firstStation, lastStation));
// format the return values
return { actualConditions.push(...someStations);
...data.properties, // update counters
StationId: station.id, firstStation += lastStation;
city: station.city, lastStation = Math.min(regionalStations.length + 1, firstStation + 7 - actualConditions.length);
}; }
} catch (e) {
console.log(`Unable to get latest observations for ${station.id}`);
return false;
}
}));
// remove and stations that did not return data
const actualConditions = allConditions.filter((condition) => condition);
// cut down to the maximum of 7 // cut down to the maximum of 7
this.data = actualConditions.slice(0, this.MaximumRegionalStations); this.data = actualConditions.slice(0, this.MaximumRegionalStations);
@ -119,5 +113,28 @@ const shortenCurrentConditions = (_condition) => {
condition = condition.replace(/ with /, '/'); condition = condition.replace(/ with /, '/');
return condition; return condition;
}; };
const getStations = async (stations) => {
const stationData = await Promise.all(stations.map(async (station) => {
try {
const data = await json(`https://api.weather.gov/stations/${station.id}/observations/latest`, { retryCount: 1, stillWaiting: () => this.stillWaiting() });
// test for temperature, weather and wind values present
if (data.properties.temperature.value === null
|| data.properties.textDescription === ''
|| data.properties.windSpeed.value === null) return false;
// format the return values
return {
...data.properties,
StationId: station.id,
city: station.city,
};
} catch (e) {
console.log(`Unable to get latest observations for ${station.id}`);
return false;
}
}));
// filter false (no data or other error)
return stationData.filter((d) => d);
};
// register display // register display
registerDisplay(new LatestObservations(2, 'latest-observations')); registerDisplay(new LatestObservations(2, 'latest-observations'));

View file

@ -23,7 +23,9 @@ const getRegionalObservation = async (point, city) => {
const observation = await json(`${station}/observations/latest`); const observation = await json(`${station}/observations/latest`);
// preload the image // preload the image
if (!observation.properties.icon) return false; if (!observation.properties.icon) return false;
preloadImg(getWeatherRegionalIconFromIconLink(observation.properties.icon, !observation.properties.daytime)); const icon = getWeatherRegionalIconFromIconLink(observation.properties.icon, !observation.properties.daytime);
if (!icon) return false;
preloadImg(icon);
// return the observation // return the observation
return observation.properties; return observation.properties;
} catch (e) { } catch (e) {

View file

@ -87,6 +87,9 @@ class RegionalForecast extends WeatherDisplay {
// wait for the regional observation if it's not done yet // wait for the regional observation if it's not done yet
const observation = await observationPromise; const observation = await observationPromise;
if (!observation) return false;
// format the observation the same as the forecast // format the observation the same as the forecast
const regionalObservation = { const regionalObservation = {
daytime: !!observation.icon.match(/\/day\//), daytime: !!observation.icon.match(/\/day\//),