2020-09-04 13:02:20 -05:00
|
|
|
// display extended forecast graphically
|
|
|
|
|
|
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 } from './utils/units.mjs';
|
|
|
|
|
import { getConditionText } from './utils/weather.mjs';
|
|
|
|
|
import { getLargeIconFromWmoCode } from './icons.mjs';
|
|
|
|
|
import { preloadImg } from './utils/image.mjs';
|
2020-09-04 13:02:20 -05:00
|
|
|
|
|
|
|
|
class ExtendedForecast extends WeatherDisplay {
|
2020-10-29 16:44:28 -05:00
|
|
|
constructor(navId, elemId) {
|
2022-11-21 21:50:22 -06:00
|
|
|
super(navId, elemId, 'Extended Forecast', true);
|
2020-09-04 13:02:20 -05:00
|
|
|
this.timing.totalScreens = 2;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
async getData(weatherParameters) {
|
|
|
|
|
if (!super.getData(weatherParameters)) return;
|
|
|
|
|
this.data = parseForecast(this.weatherParameters);
|
|
|
|
|
this.screenIndex = 0;
|
|
|
|
|
this.setStatus(STATUS.loaded);
|
2020-09-04 13:02:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async drawCanvas() {
|
|
|
|
|
super.drawCanvas();
|
2026-04-07 14:57:23 -04:00
|
|
|
const forecast = this.data.slice(0 + 3 * this.screenIndex, 3 + this.screenIndex * 3);
|
|
|
|
|
const days = forecast.map((day) => this.fillTemplate('day', {
|
|
|
|
|
icon: { type: 'img', src: day.icon },
|
|
|
|
|
condition: day.text,
|
|
|
|
|
date: day.dayName,
|
|
|
|
|
'value-hi': Math.round(day.high),
|
|
|
|
|
...(day.low !== undefined ? { 'value-lo': Math.round(day.low) } : {}),
|
|
|
|
|
}));
|
2020-09-04 13:02:20 -05:00
|
|
|
|
2022-08-04 22:03:59 -05:00
|
|
|
const dayContainer = this.elem.querySelector('.day-container');
|
|
|
|
|
dayContainer.innerHTML = '';
|
|
|
|
|
dayContainer.append(...days);
|
2020-09-04 13:02:20 -05:00
|
|
|
this.finishDraw();
|
|
|
|
|
}
|
2020-10-29 16:44:28 -05:00
|
|
|
}
|
2022-11-22 16:19:10 -06:00
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
const parseForecast = (weatherParameters) => {
|
|
|
|
|
const temperatureConverter = temperature();
|
|
|
|
|
return Object.entries(weatherParameters.forecast).slice(0, 6).map(([date, period]) => {
|
|
|
|
|
const text = shortenExtendedForecastText(getConditionText(period.weather_code ?? 0));
|
|
|
|
|
const icon = getLargeIconFromWmoCode(period.weather_code, true);
|
|
|
|
|
preloadImg(icon);
|
|
|
|
|
return {
|
|
|
|
|
text,
|
|
|
|
|
icon,
|
|
|
|
|
dayName: new Date(`${date}T12:00:00`).toLocaleDateString('en-US', { weekday: 'short', timeZone: weatherParameters.timeZone }),
|
|
|
|
|
high: temperatureConverter(period.temperature_2m_max),
|
|
|
|
|
low: temperatureConverter(period.temperature_2m_min),
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-12-09 13:51:51 -06:00
|
|
|
};
|
|
|
|
|
|
2026-04-07 14:57:23 -04:00
|
|
|
const shortenExtendedForecastText = (text) => text
|
|
|
|
|
.replace(/Slight /gi, '')
|
|
|
|
|
.replace(/Moderate /gi, '')
|
|
|
|
|
.replace(/Thunderstorm/gi, 'T\'Storm')
|
|
|
|
|
.split(' ')
|
|
|
|
|
.slice(0, 2)
|
|
|
|
|
.join(' ');
|
2022-12-09 13:51:51 -06:00
|
|
|
|
2022-12-14 16:28:33 -06:00
|
|
|
registerDisplay(new ExtendedForecast(8, 'extended-forecast'));
|