mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-22 19:49:31 -07:00
fix for stale forecast data on regional forecasts #84
This commit is contained in:
@@ -7,7 +7,7 @@ import { json } from './utils/fetch.mjs';
|
|||||||
import { temperature as temperatureUnit } from './utils/units.mjs';
|
import { temperature as temperatureUnit } from './utils/units.mjs';
|
||||||
import { getSmallIcon } from './icons.mjs';
|
import { getSmallIcon } from './icons.mjs';
|
||||||
import { preloadImg } from './utils/image.mjs';
|
import { preloadImg } from './utils/image.mjs';
|
||||||
import { DateTime } from '../vendor/auto/luxon.mjs';
|
import { DateTime, Interval } from '../vendor/auto/luxon.mjs';
|
||||||
import WeatherDisplay from './weatherdisplay.mjs';
|
import WeatherDisplay from './weatherdisplay.mjs';
|
||||||
import { registerDisplay } from './navigation.mjs';
|
import { registerDisplay } from './navigation.mjs';
|
||||||
import * as utils from './regionalforecast-utils.mjs';
|
import * as utils from './regionalforecast-utils.mjs';
|
||||||
@@ -77,6 +77,9 @@ class RegionalForecast extends WeatherDisplay {
|
|||||||
// get a unit converter
|
// get a unit converter
|
||||||
const temperatureConverter = temperatureUnit();
|
const temperatureConverter = temperatureUnit();
|
||||||
|
|
||||||
|
// get now as DateTime for calculations below
|
||||||
|
const now = DateTime.now();
|
||||||
|
|
||||||
// get regional forecasts and observations (the two are intertwined due to the design of api.weather.gov)
|
// get regional forecasts and observations (the two are intertwined due to the design of api.weather.gov)
|
||||||
const regionalDataAll = await Promise.all(regionalCities.map(async (city) => {
|
const regionalDataAll = await Promise.all(regionalCities.map(async (city) => {
|
||||||
try {
|
try {
|
||||||
@@ -110,14 +113,24 @@ class RegionalForecast extends WeatherDisplay {
|
|||||||
preloadImg(getSmallIcon(regionalObservation.icon, !regionalObservation.daytime));
|
preloadImg(getSmallIcon(regionalObservation.icon, !regionalObservation.daytime));
|
||||||
|
|
||||||
// return a pared-down forecast
|
// return a pared-down forecast
|
||||||
// 0th object is the current conditions
|
// 0th object should contain the current conditions, but when WFOs go offline or otherwise don't post
|
||||||
// first object is the next period i.e. if it's daytime then it's the "tonight" forecast
|
// an updated forecast it's possible that the 0th object is in the past.
|
||||||
// second object is the following period
|
// so we go on a search for the current time in the start/end times provided in the forecast periods
|
||||||
// always skip the first forecast index because it's what's going on right now
|
const { periods } = forecast.properties;
|
||||||
|
const currentPeriod = periods.reduce((prev, period, index) => {
|
||||||
|
const start = DateTime.fromISO(period.startTime);
|
||||||
|
const end = DateTime.fromISO(period.endTime);
|
||||||
|
const interval = Interval.fromDateTimes(start, end);
|
||||||
|
if (interval.contains(now)) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}, 0);
|
||||||
|
// group together the current observation and next two periods
|
||||||
return [
|
return [
|
||||||
regionalObservation,
|
regionalObservation,
|
||||||
utils.buildForecast(forecast.properties.periods[1], city, cityXY),
|
utils.buildForecast(forecast.properties.periods[currentPeriod + 1], city, cityXY),
|
||||||
utils.buildForecast(forecast.properties.periods[2], city, cityXY),
|
utils.buildForecast(forecast.properties.periods[currentPeriod + 2], city, cityXY),
|
||||||
];
|
];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`No regional forecast data for '${city.name ?? city.city}'`);
|
console.log(`No regional forecast data for '${city.name ?? city.city}'`);
|
||||||
|
|||||||
Reference in New Issue
Block a user