From d44f40acaf6d77c1988eccf7f076cc46fb00089e Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Thu, 3 Apr 2025 16:21:23 -0500 Subject: [PATCH] Add get of normal temperatures --- .eslintrc.js | 2 +- html/resources/js/config.mjs | 40 +++++++++---------- html/resources/js/forecast.mjs | 55 +++++++++++++++---------- html/resources/js/location.mjs | 73 +++++++++++++++++++++++++--------- html/resources/js/progress.mjs | 2 +- html/resources/js/utils.mjs | 28 ++++++------- 6 files changed, 124 insertions(+), 76 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 906605f..48a9d45 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,7 +15,7 @@ module.exports = { SharedArrayBuffer: 'readonly', }, parserOptions: { - ecmaVersion: 2021, + ecmaVersion: 2024, }, plugins: [ ], diff --git a/html/resources/js/config.mjs b/html/resources/js/config.mjs index 781d2cd..bfcd1bd 100644 --- a/html/resources/js/config.mjs +++ b/html/resources/js/config.mjs @@ -11,7 +11,7 @@ const trendConfig = (displayName, scale, units, yAxis, lineType, valueFunction) lineType, // 0=no fill, 1=fill to zero, 2=points only valueFunction, }; - // set default display units + // set default display units obj.scale.setUnit(units); return obj; }; @@ -23,25 +23,25 @@ const getLineType = (lineType, name, forceState = false) => { const opt = (visible === undefined) ? true : visible ?? forceState; switch (lineType) { - case 2: - // points only - return { - show: false, - }; - case 1: - // filled under line to zero - return { - fill: opt, - lineWidth: 0, - show: opt, - }; - case 0: - default: - // line only, no fill - return { - show: opt, - lineWidth: 2, - }; + case 2: + // points only + return { + show: false, + }; + case 1: + // filled under line to zero + return { + fill: opt, + lineWidth: 0, + show: opt, + }; + case 0: + default: + // line only, no fill + return { + show: opt, + lineWidth: 2, + }; } }; diff --git a/html/resources/js/forecast.mjs b/html/resources/js/forecast.mjs index 0d70461..0639a7e 100644 --- a/html/resources/js/forecast.mjs +++ b/html/resources/js/forecast.mjs @@ -183,35 +183,35 @@ const getInfo = (type) => { // can't get anything if the plot isn't drawn if (plot === null) return null; switch (type) { - // all available data - case 'xMin': - return plot.getAxes().xaxis.datamin; - case 'xMax': - return plot.getAxes().xaxis.datamax; + // all available data + case 'xMin': + return plot.getAxes().xaxis.datamin; + case 'xMax': + return plot.getAxes().xaxis.datamax; // visible data - case 'xMinVisible': - return plot.getAxes().xaxis.min; - case 'xMaxVisible': - return plot.getAxes().xaxis.max; + case 'xMinVisible': + return plot.getAxes().xaxis.min; + case 'xMaxVisible': + return plot.getAxes().xaxis.max; // oldest data - case 'oldestData': - return metaData.oldestData; - case 'forecastTimestamp': - return metaData.forecastTimestamp; - case 'lastUpdate': - return metaData.lastUpdate; + case 'oldestData': + return metaData.oldestData; + case 'forecastTimestamp': + return metaData.forecastTimestamp; + case 'lastUpdate': + return metaData.lastUpdate; // x axis limits - case 'xLimits': - return { - min: plot.getAxes().xaxis.min, - max: plot.getAxes().xaxis.max, - }; + case 'xLimits': + return { + min: plot.getAxes().xaxis.min, + max: plot.getAxes().xaxis.max, + }; - default: - return plot[type]; + default: + return plot[type]; } }; @@ -346,11 +346,22 @@ const updateCurrentTemperature = (dataset) => { } }; +// store and format the normal temperatures received +const formatNormalTemperatures = (data) => { + const { timeZone } = getSavedLocation(); + if (timeZone) { + const placeTime = (DateTime.now().setZone(timeZone).startOf('day')); + const userTime = (DateTime.now().startOf('day')); + convertTimestamp.timeZoneOffset = (new Date()).getTimezoneOffset() * 60_000 - (userTime - placeTime); // time zone offset in milliseconds + } +}; + export { getInfo, formatData, chartVisibility, setUnits, readVisibility, + formatNormalTemperatures, OLD_FORECAST_LIMIT, }; diff --git a/html/resources/js/location.mjs b/html/resources/js/location.mjs index 78aab40..dd18f91 100644 --- a/html/resources/js/location.mjs +++ b/html/resources/js/location.mjs @@ -67,24 +67,24 @@ const getPlaceFailed = (e, isRetry) => { ProgressBar.message('Geolocation lookup failed', true); ProgressBar.message(e, true); switch (e.code ?? 0) { - case 1: - // user denied - // turn off gps option on location screen and force a user input - Dialog.mustEnterPlaceName(); - break; + case 1: + // user denied + // turn off gps option on location screen and force a user input + Dialog.mustEnterPlaceName(); + break; // position unavailable (internal position source failed) // intentional fall-through to "force user input" below - case 2: - case 3: - // timeout expired - // retry once then force user to enter a location - if (isRetry) { - Dialog.mustEnterPlaceName(); - } else { - getPlace(true); - } - break; - default: + case 2: + case 3: + // timeout expired + // retry once then force user to enter a location + if (isRetry) { + Dialog.mustEnterPlaceName(); + } else { + getPlace(true); + } + break; + default: } }; @@ -144,7 +144,7 @@ const latLonReceived = (places, _place) => { const newLon = parseFloat(places[0].lon); // if less than 0.01 from previous location forgo location lookup and use cached point if (Math.abs(newLat - place.lat) <= 0.01 && Math.abs(newLon - place.lon) <= 0.01 - && place.pointX !== null && pointReceived.placeY !== null) { + && place.pointX !== null && pointReceived.placeY !== null) { Outlook.show([place.lon, place.lat]); // jump to point received pointReceived(false, place); @@ -243,6 +243,7 @@ const pointReceived = (point, _place) => { // pass point data to hourly forecast api getHourlyForecast(baseUrl); + getNormalTemperatures(place.office); // get text forecast Menu.getTextForecast(baseUrl); @@ -401,9 +402,45 @@ const visibilityChange = () => { const lastUpdate = Forecast.getInfo('lastUpdate'); const forecastTimestamp = Date.parse(Forecast.getInfo('forecastTimestamp')); if (now > lastUpdate + 15 * 60_000 - || now > forecastTimestamp + 60 * 60_000) { + || now > forecastTimestamp + 60 * 60_000) { // trigger an update getPlace(); } } }; + +// normal high/low temperatures for the WFO +const getNormalTemperatures = async (wfo) => { + // cancel previous request if present + getNormalTemperatures?.cancel?.(); + + // prepare the body of the request + const requestParams = { + elems: [{ + name: 'maxt', interval: 'dly', duration: 'dly', normal: '91', prec: 0, + }, { + name: 'mint', interval: 'dly', duration: 'dly', normal: '91', prec: 0, + }], + sid: wfo, + sDate: DateTime.now().minus({ days: 10 }).toISODate(), + eDate: DateTime.now().plus({ days: 10 }).toISODate(), + }; + const requestBody = new URLSearchParams(); + requestBody.append('output', 'json'); + requestBody.append('params', JSON.stringify(requestParams)); + + try { + const response = await fetch('https://data.rcc-acis.org/StnData', { + headers: { + accept: 'application/json', + }, + body: requestBody, + method: 'POST', + }); + const data = await response.json(); + ProgressBar.set('Normal temperatures received'); + Forecast.formatNormalTemperatures(data); + } catch (e) { + ProgressBar.message('Get normal temperatures failed', true); + } +}; diff --git a/html/resources/js/progress.mjs b/html/resources/js/progress.mjs index f9e3180..1dd75ba 100644 --- a/html/resources/js/progress.mjs +++ b/html/resources/js/progress.mjs @@ -1,4 +1,4 @@ -const max = 7; +const max = 8; let messageContainer; let dialog; let element; diff --git a/html/resources/js/utils.mjs b/html/resources/js/utils.mjs index 5bab401..589c947 100644 --- a/html/resources/js/utils.mjs +++ b/html/resources/js/utils.mjs @@ -34,14 +34,14 @@ const dark = () => isDark; const backoff = (iteration) => { switch (iteration) { - case 0: return 2; - case 1: return 2; - case 2: return 4; - case 3: return 8; - case 4: return 16; - case 5: - default: - return 32; + case 0: return 2; + case 1: return 2; + case 2: return 4; + case 3: return 8; + case 4: return 16; + case 5: + default: + return 32; } }; @@ -97,7 +97,7 @@ const getFile = async (url, name) => { if (response.status !== 200) throw new Error(`Status code ${response.status}`); const fileData = await response.json(); return fileData.features; - } catch (error) { + } catch (error) { ProgressBar.message(`Error getting ${name ?? ''} spc outlook: ${url}`, true); ProgressBar.message(error, true); return null; @@ -106,11 +106,11 @@ const getFile = async (url, name) => { const formatDay = (index) => { switch (index) { - case 0: - return 'Today'; - case 1: - return 'Tomorrow'; - default: + case 0: + return 'Today'; + case 1: + return 'Tomorrow'; + default: } // calculate date