From 2a9e5b370e63d7a4b6b6e28aed67733ab4f010c4 Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Tue, 17 Jan 2023 11:26:57 -0600 Subject: [PATCH] don't re-parse current conditions --- server/scripts/modules/currentweather.mjs | 149 +++++++++++----------- server/scripts/modules/icons.mjs | 3 + 2 files changed, 75 insertions(+), 77 deletions(-) diff --git a/server/scripts/modules/currentweather.mjs b/server/scripts/modules/currentweather.mjs index bde1f20..61bbf00 100644 --- a/server/scripts/modules/currentweather.mjs +++ b/server/scripts/modules/currentweather.mjs @@ -30,8 +30,9 @@ class CurrentWeather extends WeatherDisplay { const filteredStations = weatherParameters.stations.filter((station) => station?.properties?.stationIdentifier?.length === 4 && !skipStations.includes(station.properties.stationIdentifier.slice(0, 1))); // Load the observations - let observations; let - station; + let observations; + let station; + // station number counter let stationNum = 0; while (!observations && stationNum < filteredStations.length) { @@ -73,7 +74,7 @@ class CurrentWeather extends WeatherDisplay { } // we only get here if there was no error above - this.data = { ...observations, station }; + this.data = parseData({ ...observations, station }); this.getDataCallback(); // stop here if we're disabled @@ -84,89 +85,37 @@ class CurrentWeather extends WeatherDisplay { this.setStatus(STATUS.loaded); } - // format the data for use outside this function - parseData() { - if (!this.data) return false; - const data = {}; - const observations = this.data.features[0].properties; - // values from api are provided in metric - data.observations = observations; - data.Temperature = Math.round(observations.temperature.value); - data.TemperatureUnit = 'C'; - data.DewPoint = Math.round(observations.dewpoint.value); - data.Ceiling = Math.round(observations.cloudLayers[0]?.base?.value ?? 0); - data.CeilingUnit = 'm.'; - data.Visibility = Math.round(observations.visibility.value / 1000); - data.VisibilityUnit = ' km.'; - data.WindSpeed = Math.round(observations.windSpeed.value); - data.WindDirection = directionToNSEW(observations.windDirection.value); - data.Pressure = Math.round(observations.barometricPressure.value); - data.HeatIndex = Math.round(observations.heatIndex.value); - data.WindChill = Math.round(observations.windChill.value); - data.WindGust = Math.round(observations.windGust.value); - data.WindUnit = 'KPH'; - data.Humidity = Math.round(observations.relativeHumidity.value); - data.Icon = getWeatherIconFromIconLink(observations.icon); - data.PressureDirection = ''; - data.TextConditions = observations.textDescription; - data.station = this.data.station; - - // difference since last measurement (pascals, looking for difference of more than 150) - const pressureDiff = (observations.barometricPressure.value - this.data.features[1].properties.barometricPressure.value); - if (pressureDiff > 150) data.PressureDirection = 'R'; - if (pressureDiff < -150) data.PressureDirection = 'F'; - - data.Temperature = celsiusToFahrenheit(data.Temperature); - data.TemperatureUnit = 'F'; - data.DewPoint = celsiusToFahrenheit(data.DewPoint); - data.Ceiling = Math.round(metersToFeet(data.Ceiling) / 100) * 100; - data.CeilingUnit = 'ft.'; - data.Visibility = kilometersToMiles(observations.visibility.value / 1000); - data.VisibilityUnit = ' mi.'; - data.WindSpeed = kphToMph(data.WindSpeed); - data.WindUnit = 'MPH'; - data.Pressure = pascalToInHg(data.Pressure).toFixed(2); - data.HeatIndex = celsiusToFahrenheit(data.HeatIndex); - data.WindChill = celsiusToFahrenheit(data.WindChill); - data.WindGust = kphToMph(data.WindGust); - return data; - } - async drawCanvas() { super.drawCanvas(); - const fill = {}; - // parse each time to deal with a change in units if necessary - const data = this.parseData(); - fill.temp = data.Temperature + String.fromCharCode(176); - - let Conditions = data.observations.textDescription; - if (Conditions.length > 15) { - Conditions = shortConditions(Conditions); + let condition = this.data.observations.textDescription; + if (condition.length > 15) { + condition = shortConditions(condition); } - fill.condition = Conditions; - fill.wind = data.WindDirection.padEnd(3, '') + data.WindSpeed.toString().padStart(3, ' '); - if (data.WindGust) fill['wind-gusts'] = `Gusts to ${data.WindGust}`; + const fill = { + temp: this.data.Temperature + String.fromCharCode(176), + condition, + wind: this.data.WindDirection.padEnd(3, '') + this.data.WindSpeed.toString().padStart(3, ' '), + location: locationCleanup(this.data.station.properties.name).substr(0, 20), + humidity: `${this.data.Humidity}%`, + dewpoint: this.data.DewPoint + String.fromCharCode(176), + ceiling: (this.data.Ceiling === 0 ? 'Unlimited' : this.data.Ceiling + this.data.CeilingUnit), + visibility: this.data.Visibility + this.data.VisibilityUnit, + pressure: `${this.data.Pressure} ${this.data.PressureDirection}`, + icon: { type: 'img', src: this.data.Icon }, + }; - fill.location = locationCleanup(this.data.station.properties.name).substr(0, 20); + if (this.data.WindGust) fill['wind-gusts'] = `Gusts to ${this.data.WindGust}`; - fill.humidity = `${data.Humidity}%`; - fill.dewpoint = data.DewPoint + String.fromCharCode(176); - fill.ceiling = (data.Ceiling === 0 ? 'Unlimited' : data.Ceiling + data.CeilingUnit); - fill.visibility = data.Visibility + data.VisibilityUnit; - fill.pressure = `${data.Pressure} ${data.PressureDirection}`; - - if (data.observations.heatIndex.value && data.HeatIndex !== data.Temperature) { + if (this.data.observations.heatIndex.value && this.data.HeatIndex !== this.data.Temperature) { fill['heat-index-label'] = 'Heat Index:'; - fill['heat-index'] = data.HeatIndex + String.fromCharCode(176); - } else if (data.observations.windChill.value && data.WindChill !== '' && data.WindChill < data.Temperature) { + fill['heat-index'] = this.data.HeatIndex + String.fromCharCode(176); + } else if (this.data.observations.windChill.value && this.data.WindChill !== '' && this.data.WindChill < this.data.Temperature) { fill['heat-index-label'] = 'Wind Chill:'; - fill['heat-index'] = data.WindChill + String.fromCharCode(176); + fill['heat-index'] = this.data.WindChill + String.fromCharCode(176); } - fill.icon = { type: 'img', src: data.Icon }; - const area = this.elem.querySelector('.main'); area.innerHTML = ''; @@ -180,9 +129,9 @@ class CurrentWeather extends WeatherDisplay { async getCurrentWeather(stillWaiting) { if (stillWaiting) this.stillWaitingCallbacks.push(stillWaiting); return new Promise((resolve) => { - if (this.data) resolve(this.parseData()); + if (this.data) resolve(this.data); // data not available, put it into the data callback queue - this.getDataCallbacks.push(() => resolve(this.parseData())); + this.getDataCallbacks.push(() => resolve(this.data)); }); } } @@ -206,6 +155,52 @@ const shortConditions = (_condition) => { return condition; }; +// format the received data +const parseData = (data) => { + const observations = data.features[0].properties; + // values from api are provided in metric + data.observations = observations; + data.Temperature = Math.round(observations.temperature.value); + data.TemperatureUnit = 'C'; + data.DewPoint = Math.round(observations.dewpoint.value); + data.Ceiling = Math.round(observations.cloudLayers[0]?.base?.value ?? 0); + data.CeilingUnit = 'm.'; + data.Visibility = Math.round(observations.visibility.value / 1000); + data.VisibilityUnit = ' km.'; + data.WindSpeed = Math.round(observations.windSpeed.value); + data.WindDirection = directionToNSEW(observations.windDirection.value); + data.Pressure = Math.round(observations.barometricPressure.value); + data.HeatIndex = Math.round(observations.heatIndex.value); + data.WindChill = Math.round(observations.windChill.value); + data.WindGust = Math.round(observations.windGust.value); + data.WindUnit = 'KPH'; + data.Humidity = Math.round(observations.relativeHumidity.value); + data.Icon = getWeatherIconFromIconLink(observations.icon); + data.PressureDirection = ''; + data.TextConditions = observations.textDescription; + + // difference since last measurement (pascals, looking for difference of more than 150) + const pressureDiff = (observations.barometricPressure.value - data.features[1].properties.barometricPressure.value); + if (pressureDiff > 150) data.PressureDirection = 'R'; + if (pressureDiff < -150) data.PressureDirection = 'F'; + + // convert to us units + data.Temperature = celsiusToFahrenheit(data.Temperature); + data.TemperatureUnit = 'F'; + data.DewPoint = celsiusToFahrenheit(data.DewPoint); + data.Ceiling = Math.round(metersToFeet(data.Ceiling) / 100) * 100; + data.CeilingUnit = 'ft.'; + data.Visibility = kilometersToMiles(observations.visibility.value / 1000); + data.VisibilityUnit = ' mi.'; + data.WindSpeed = kphToMph(data.WindSpeed); + data.WindUnit = 'MPH'; + data.Pressure = pascalToInHg(data.Pressure).toFixed(2); + data.HeatIndex = celsiusToFahrenheit(data.HeatIndex); + data.WindChill = celsiusToFahrenheit(data.WindChill); + data.WindGust = kphToMph(data.WindGust); + return data; +}; + const display = new CurrentWeather(1, 'current-weather'); registerDisplay(display); diff --git a/server/scripts/modules/icons.mjs b/server/scripts/modules/icons.mjs index 229fe77..f29705f 100644 --- a/server/scripts/modules/icons.mjs +++ b/server/scripts/modules/icons.mjs @@ -127,6 +127,9 @@ const getWeatherRegionalIconFromIconLink = (link, _isNightTime) => { case 'wind_sct': case 'wind_bkn': case 'wind_ovc': + case 'wind_few-n': + case 'wind_bkn-n': + case 'wind_ovc-n': return addPath('Wind.gif'); case 'wind_skc':