2020-09-04 18:02:20 +00:00
|
|
|
// display text based local forecast
|
|
|
|
|
2022-11-22 22:29:10 +00:00
|
|
|
/* globals navigation */
|
2022-11-22 22:19:10 +00:00
|
|
|
import STATUS from './status.mjs';
|
|
|
|
import { UNITS } from './config.mjs';
|
|
|
|
import { json } from './utils/fetch.mjs';
|
2022-11-22 22:29:10 +00:00
|
|
|
import WeatherDisplay from './weatherdisplay.mjs';
|
2020-09-04 18:02:20 +00:00
|
|
|
|
|
|
|
class LocalForecast extends WeatherDisplay {
|
2020-10-29 21:44:28 +00:00
|
|
|
constructor(navId, elemId) {
|
2022-11-22 03:50:22 +00:00
|
|
|
super(navId, elemId, 'Local Forecast', true);
|
2020-09-04 18:02:20 +00:00
|
|
|
|
|
|
|
// set timings
|
2020-10-29 21:44:28 +00:00
|
|
|
this.timing.baseDelay = 5000;
|
2020-09-04 18:02:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 21:44:28 +00:00
|
|
|
async getData(_weatherParameters) {
|
|
|
|
super.getData(_weatherParameters);
|
|
|
|
const weatherParameters = _weatherParameters ?? this.weatherParameters;
|
2020-09-04 18:02:20 +00:00
|
|
|
|
|
|
|
// get raw data
|
|
|
|
const rawData = await this.getRawData(weatherParameters);
|
2020-09-09 20:23:19 +00:00
|
|
|
// check for data
|
|
|
|
if (!rawData) {
|
|
|
|
this.setStatus(STATUS.failed);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-04 18:02:20 +00:00
|
|
|
// parse raw data
|
2020-10-29 21:44:28 +00:00
|
|
|
const conditions = LocalForecast.parse(rawData);
|
2020-09-04 18:02:20 +00:00
|
|
|
|
|
|
|
// read each text
|
2022-08-04 16:07:35 +00:00
|
|
|
this.screenTexts = conditions.map((condition) => {
|
2020-09-04 18:02:20 +00:00
|
|
|
// process the text
|
2020-10-29 21:44:28 +00:00
|
|
|
let text = `${condition.DayName.toUpperCase()}...`;
|
2020-09-04 18:02:20 +00:00
|
|
|
let conditionText = condition.Text;
|
|
|
|
if (navigation.units() === UNITS.metric) {
|
|
|
|
conditionText = condition.TextC;
|
|
|
|
}
|
|
|
|
text += conditionText.toUpperCase().replace('...', ' ');
|
|
|
|
|
2022-08-04 16:07:35 +00:00
|
|
|
return text;
|
|
|
|
});
|
2020-10-29 21:44:28 +00:00
|
|
|
|
2022-08-04 16:07:35 +00:00
|
|
|
// fill the forecast texts
|
|
|
|
const templates = this.screenTexts.map((text) => this.fillTemplate('forecast', { text }));
|
|
|
|
const forecastsElem = this.elem.querySelector('.forecasts');
|
|
|
|
forecastsElem.innerHTML = '';
|
|
|
|
forecastsElem.append(...templates);
|
|
|
|
|
|
|
|
// increase each forecast height to a multiple of container height
|
|
|
|
this.pageHeight = forecastsElem.parentNode.getBoundingClientRect().height;
|
|
|
|
templates.forEach((forecast) => {
|
|
|
|
const newHeight = Math.ceil(forecast.scrollHeight / this.pageHeight) * this.pageHeight;
|
|
|
|
forecast.style.height = `${newHeight}px`;
|
2020-09-04 18:02:20 +00:00
|
|
|
});
|
|
|
|
|
2022-08-04 16:07:35 +00:00
|
|
|
this.timing.totalScreens = forecastsElem.scrollHeight / this.pageHeight;
|
2020-09-13 04:49:51 +00:00
|
|
|
this.calcNavTiming();
|
2020-09-09 20:23:19 +00:00
|
|
|
this.setStatus(STATUS.loaded);
|
2020-09-04 18:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the unformatted data (also used by extended forecast)
|
|
|
|
async getRawData(weatherParameters) {
|
|
|
|
// request us or si units
|
|
|
|
let units = 'us';
|
|
|
|
if (navigation.units() === UNITS.metric) units = 'si';
|
|
|
|
try {
|
2022-11-22 22:19:10 +00:00
|
|
|
return await json(weatherParameters.forecast, {
|
2020-09-04 18:02:20 +00:00
|
|
|
data: {
|
|
|
|
units,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`GetWeatherForecast failed: ${weatherParameters.forecast}`);
|
2020-09-23 16:49:15 +00:00
|
|
|
console.error(e.status, e.responseJSON);
|
2020-09-09 19:29:03 +00:00
|
|
|
this.setStatus(STATUS.failed);
|
2020-09-04 18:02:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async drawCanvas() {
|
|
|
|
super.drawCanvas();
|
|
|
|
|
2022-08-04 16:07:35 +00:00
|
|
|
const top = -this.screenIndex * this.pageHeight;
|
|
|
|
this.elem.querySelector('.forecasts').style.top = `${top}px`;
|
2020-09-04 18:02:20 +00:00
|
|
|
|
|
|
|
this.finishDraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the forecast
|
2020-10-29 21:44:28 +00:00
|
|
|
static parse(forecast) {
|
2020-09-04 18:02:20 +00:00
|
|
|
// only use the first 6 lines
|
2020-10-29 21:44:28 +00:00
|
|
|
return forecast.properties.periods.slice(0, 6).map((text) => ({
|
2020-09-04 18:02:20 +00:00
|
|
|
// format day and text
|
|
|
|
DayName: text.name.toUpperCase(),
|
|
|
|
Text: text.detailedForecast,
|
|
|
|
}));
|
|
|
|
}
|
2020-10-29 21:44:28 +00:00
|
|
|
}
|