2020-09-04 13:02:20 -05:00
|
|
|
// display text based local forecast
|
|
|
|
|
|
2022-11-22 16:19:10 -06:00
|
|
|
import STATUS from './status.mjs';
|
2022-11-22 16:29:10 -06:00
|
|
|
import WeatherDisplay from './weatherdisplay.mjs';
|
2022-12-06 16:14:56 -06:00
|
|
|
import { registerDisplay } from './navigation.mjs';
|
2026-04-07 14:57:23 -04:00
|
|
|
import { temperature, windSpeed } from './utils/units.mjs';
|
|
|
|
|
import { directionToNSEW } from './utils/calc.mjs';
|
|
|
|
|
import { getConditionText } from './utils/weather.mjs';
|
2020-09-04 13:02:20 -05:00
|
|
|
|
|
|
|
|
class LocalForecast extends WeatherDisplay {
|
2020-10-29 16:44:28 -05:00
|
|
|
constructor(navId, elemId) {
|
2022-11-21 21:50:22 -06:00
|
|
|
super(navId, elemId, 'Local Forecast', true);
|
2026-04-07 14:57:23 -04:00
|
|
|
this.timing.baseDelay = 5000;
|
2020-09-04 13:02:20 -05:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
async getData(weatherParameters) {
|
|
|
|
|
if (!super.getData(weatherParameters)) return;
|
2020-09-04 13:02:20 -05:00
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
const conditions = buildLocalForecastPages(this.weatherParameters);
|
|
|
|
|
if (!conditions.length) {
|
2025-06-24 23:08:25 -04:00
|
|
|
if (this.isEnabled) this.setStatus(STATUS.failed);
|
2020-09-09 15:23:19 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2020-10-29 16:44:28 -05:00
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
this.screenTexts = conditions.map((condition) => `${condition.DayName.toUpperCase()}...${condition.Text.toUpperCase()}`);
|
2022-08-04 11:07:35 -05:00
|
|
|
const templates = this.screenTexts.map((text) => this.fillTemplate('forecast', { text }));
|
|
|
|
|
const forecastsElem = this.elem.querySelector('.forecasts');
|
|
|
|
|
forecastsElem.innerHTML = '';
|
|
|
|
|
forecastsElem.append(...templates);
|
|
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
this.pageHeight = forecastsElem.parentNode.scrollHeight;
|
|
|
|
|
templates.forEach((forecast) => {
|
|
|
|
|
const newHeight = Math.ceil(forecast.scrollHeight / this.pageHeight) * this.pageHeight;
|
|
|
|
|
forecast.style.height = `${newHeight}px`;
|
|
|
|
|
});
|
2025-06-24 23:08:25 -04:00
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
this.timing.totalScreens = forecastsElem.scrollHeight / this.pageHeight;
|
2020-09-12 23:49:51 -05:00
|
|
|
this.calcNavTiming();
|
2020-09-09 15:23:19 -05:00
|
|
|
this.setStatus(STATUS.loaded);
|
2020-09-04 13:02:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async drawCanvas() {
|
|
|
|
|
super.drawCanvas();
|
2022-08-04 11:07:35 -05:00
|
|
|
const top = -this.screenIndex * this.pageHeight;
|
|
|
|
|
this.elem.querySelector('.forecasts').style.top = `${top}px`;
|
2020-09-04 13:02:20 -05:00
|
|
|
this.finishDraw();
|
|
|
|
|
}
|
2026-04-07 14:57:23 -04:00
|
|
|
}
|
2025-06-24 23:08:25 -04:00
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
const buildLocalForecastPages = (weatherParameters) => {
|
|
|
|
|
const days = Object.entries(weatherParameters.forecast).slice(0, 3);
|
|
|
|
|
const temperatureConverter = temperature();
|
|
|
|
|
const windConverter = windSpeed();
|
|
|
|
|
|
|
|
|
|
return days.map(([date, day]) => {
|
|
|
|
|
const dayName = new Date(`${date}T12:00:00`).toLocaleDateString('en-US', { weekday: 'long', timeZone: weatherParameters.timeZone });
|
|
|
|
|
const high = temperatureConverter(day.temperature_2m_max);
|
|
|
|
|
const low = temperatureConverter(day.temperature_2m_min);
|
|
|
|
|
const precip = Math.round(day.precipitation_probability ?? 0);
|
|
|
|
|
const windDirection = directionToNSEW(day.wind_direction_10m ?? 0);
|
|
|
|
|
const wind = windConverter(day.wind_speed_10m ?? 0);
|
|
|
|
|
const condition = getConditionText(day.weather_code ?? 0);
|
|
|
|
|
let text = `${condition}. High ${high}. Low ${low}.`;
|
|
|
|
|
if (precip > 20) {
|
|
|
|
|
text += ` Chance of precipitation ${precip} percent.`;
|
2025-06-24 23:08:25 -04:00
|
|
|
}
|
2026-04-07 14:57:23 -04:00
|
|
|
if (wind > 0) {
|
|
|
|
|
text += ` Wind ${windDirection} ${wind} ${windConverter.units}.`;
|
2025-06-24 23:08:25 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
return {
|
|
|
|
|
DayName: dayName,
|
|
|
|
|
Text: text,
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-06-24 23:08:25 -04:00
|
|
|
};
|
2026-04-07 14:57:23 -04:00
|
|
|
|
2022-12-14 16:28:33 -06:00
|
|
|
registerDisplay(new LocalForecast(7, 'local-forecast'));
|