From f7a15a93c627dd80ee951c8c37e994c7b0b346b5 Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Wed, 2 Apr 2025 20:58:53 -0500 Subject: [PATCH] extended forecast silent refresh --- server/scripts/modules/almanac.mjs | 7 +++-- server/scripts/modules/currentweather.mjs | 2 ++ server/scripts/modules/extendedforecast.mjs | 13 ++++----- server/scripts/modules/radar.mjs | 30 ++++++++++----------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/server/scripts/modules/almanac.mjs b/server/scripts/modules/almanac.mjs index e8c3e18..8c0d7fe 100644 --- a/server/scripts/modules/almanac.mjs +++ b/server/scripts/modules/almanac.mjs @@ -21,12 +21,11 @@ class Almanac extends WeatherDisplay { this.timing.totalScreens = 1; } - async getData(_weatherParameters, refresh) { - const superResponse = super.getData(_weatherParameters, refresh); - const weatherParameters = _weatherParameters ?? this.weatherParameters; + async getData(weatherParameters, refresh) { + const superResponse = super.getData(weatherParameters, refresh); // get sun/moon data - const { sun, moon } = this.calcSunMoonData(weatherParameters); + const { sun, moon } = this.calcSunMoonData(this.weatherParameters); // store the data this.data = { diff --git a/server/scripts/modules/currentweather.mjs b/server/scripts/modules/currentweather.mjs index 1ac6652..12a9fab 100644 --- a/server/scripts/modules/currentweather.mjs +++ b/server/scripts/modules/currentweather.mjs @@ -24,6 +24,8 @@ class CurrentWeather extends WeatherDisplay { async getData(weatherParameters, refresh) { // always load the data for use in the lower scroll const superResult = super.getData(weatherParameters, refresh); + // note: current weather does not use old data on a silent refresh + // this is deliberate because it can pull data from more than one station in sequence // filter for 4-letter observation stations, only those contain sky conditions and thus an icon const filteredStations = this.weatherParameters.stations.filter((station) => station?.properties?.stationIdentifier?.length === 4 && !skipStations.includes(station.properties.stationIdentifier.slice(0, 1))); diff --git a/server/scripts/modules/extendedforecast.mjs b/server/scripts/modules/extendedforecast.mjs index 74ab169..a5974d1 100644 --- a/server/scripts/modules/extendedforecast.mjs +++ b/server/scripts/modules/extendedforecast.mjs @@ -22,9 +22,8 @@ class ExtendedForecast extends WeatherDisplay { if (!super.getData(weatherParameters, refresh)) return; // request us or si units - let forecast; try { - forecast = await json(this.weatherParameters.forecast, { + this.data = await json(this.weatherParameters.forecast, { data: { units: settings.units.value, }, @@ -34,11 +33,13 @@ class ExtendedForecast extends WeatherDisplay { } catch (error) { console.error('Unable to get extended forecast'); console.error(error.status, error.responseJSON); - this.setStatus(STATUS.failed); - return; + // if there's no previous data, fail + if (!this.data) { + this.setStatus(STATUS.failed); + return; + } } // we only get here if there was no error above - this.data = parse(forecast.properties.periods); this.screenIndex = 0; this.setStatus(STATUS.loaded); } @@ -48,7 +49,7 @@ class ExtendedForecast extends WeatherDisplay { // determine bounds // grab the first three or second set of three array elements - const forecast = this.data.slice(0 + 3 * this.screenIndex, 3 + this.screenIndex * 3); + const forecast = parse(this.data.properties.periods).slice(0 + 3 * this.screenIndex, 3 + this.screenIndex * 3); // create each day template const days = forecast.map((Day) => { diff --git a/server/scripts/modules/radar.mjs b/server/scripts/modules/radar.mjs index 1ed10e3..4982d1f 100644 --- a/server/scripts/modules/radar.mjs +++ b/server/scripts/modules/radar.mjs @@ -42,19 +42,17 @@ class Radar extends WeatherDisplay { ]; } - async getData(_weatherParameters, refresh) { - if (!super.getData(_weatherParameters, refresh)) return; - const weatherParameters = _weatherParameters ?? this.weatherParameters; + async getData(weatherParameters, refresh) { + if (!super.getData(weatherParameters, refresh)) return; // ALASKA AND HAWAII AREN'T SUPPORTED! - if (weatherParameters.state === 'AK' || weatherParameters.state === 'HI') { + if (this.weatherParameters.state === 'AK' || this.weatherParameters.state === 'HI') { this.setStatus(STATUS.noData); return; } // get the base map - let src = 'images/4000RadarMap2.jpg'; - if (weatherParameters.State === 'HI') src = 'images/HawaiiRadarMap2.png'; + const src = 'images/4000RadarMap2.jpg'; this.baseMap = await loadImg(src); const baseUrl = 'https://mesonet.agron.iastate.edu/archive/data/'; @@ -110,19 +108,12 @@ class Radar extends WeatherDisplay { const height = 1600; offsetX *= 2; offsetY *= 2; - const sourceXY = utils.getXYFromLatitudeLongitudeMap(weatherParameters, offsetX, offsetY); - - // create working context for manipulation - const workingCanvas = document.createElement('canvas'); - workingCanvas.width = width; - workingCanvas.height = height; - const workingContext = workingCanvas.getContext('2d'); - workingContext.imageSmoothingEnabled = false; + const sourceXY = utils.getXYFromLatitudeLongitudeMap(this.weatherParameters, offsetX, offsetY); // calculate radar offsets const radarOffsetX = 120; const radarOffsetY = 70; - const radarSourceXY = utils.getXYFromLatitudeLongitudeDoppler(weatherParameters, offsetX, offsetY); + const radarSourceXY = utils.getXYFromLatitudeLongitudeDoppler(this.weatherParameters, offsetX, offsetY); const radarSourceX = radarSourceXY.x / 2; const radarSourceY = radarSourceXY.y / 2; @@ -135,6 +126,13 @@ class Radar extends WeatherDisplay { const context = canvas.getContext('2d'); context.imageSmoothingEnabled = false; + // create working context for manipulation + const workingCanvas = document.createElement('canvas'); + workingCanvas.width = width; + workingCanvas.height = height; + const workingContext = workingCanvas.getContext('2d'); + workingContext.imageSmoothingEnabled = false; + // get the image const response = await fetch(rewriteUrl(url)); @@ -170,7 +168,7 @@ class Radar extends WeatherDisplay { workingContext.drawImage(imgBlob, 0, 0, width, 1600); // get the base map - context.drawImage(await this.baseMap, sourceXY.x, sourceXY.y, offsetX * 2, offsetY * 2, 0, 0, 640, 367); + context.drawImage(this.baseMap, sourceXY.x, sourceXY.y, offsetX * 2, offsetY * 2, 0, 0, 640, 367); // crop the radar image const cropCanvas = document.createElement('canvas');