ws4kp/server/scripts/modules/localforecast.js

140 lines
4 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
// display text based local forecast
2020-10-29 21:44:28 +00:00
/* globals WeatherDisplay, utils, STATUS, UNITS, draw, navigation */
2020-09-04 18:02:20 +00:00
// eslint-disable-next-line no-unused-vars
class LocalForecast extends WeatherDisplay {
2020-10-29 21:44:28 +00:00
constructor(navId, elemId) {
super(navId, elemId, 'Local Forecast');
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
// pre-load background image (returns promise)
this.backgroundImage = utils.image.load('images/BackGround1_1.png');
}
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);
// 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
// split this forecast into the correct number of screens
2020-09-25 00:38:53 +00:00
const maxRows = 7;
const maxCols = 32;
2020-09-04 18:02:20 +00:00
this.screenTexts = [];
// read each text
2020-10-29 21:44:28 +00:00
conditions.forEach((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('...', ' ');
2020-09-25 00:38:53 +00:00
text = utils.string.wordWrap(text, maxCols, '\n');
const lines = text.split('\n');
const lineCount = lines.length;
2020-09-04 18:02:20 +00:00
let ScreenText = '';
2020-09-25 00:38:53 +00:00
const maxRowCount = maxRows;
let rowCount = 0;
2020-09-04 18:02:20 +00:00
// if (PrependAlert) {
// ScreenText = LocalForecastScreenTexts[LocalForecastScreenTexts.length - 1];
2020-09-25 00:38:53 +00:00
// rowCount = ScreenText.split('\n').length - 1;
2020-09-04 18:02:20 +00:00
// }
2020-10-29 21:44:28 +00:00
for (let i = 0; i <= lineCount - 1; i += 1) {
if (lines[i] !== '') {
if (rowCount > maxRowCount - 1) {
2020-09-04 18:02:20 +00:00
// if (PrependAlert) {
// LocalForecastScreenTexts[LocalForecastScreenTexts.length - 1] = ScreenText;
// PrependAlert = false;
// } else {
2020-10-29 21:44:28 +00:00
this.screenTexts.push(ScreenText);
// }
ScreenText = '';
rowCount = 0;
}
ScreenText += `${lines[i]}\n`;
rowCount += 1;
2020-09-04 18:02:20 +00:00
}
}
// if (PrependAlert) {
// this.screenTexts[this.screenTexts.length - 1] = ScreenText;
// PrependAlert = false;
// } else {
this.screenTexts.push(ScreenText);
// }
});
this.timing.totalScreens = this.screenTexts.length;
this.calcNavTiming();
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 {
2020-10-02 02:35:49 +00:00
return await utils.fetch.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;
}
}
// TODO: alerts needs a cleanup
// TODO: second page of screenTexts when needed
async drawCanvas() {
super.drawCanvas();
this.context.drawImage(await this.backgroundImage, 0, 0);
draw.horizontalGradientSingle(this.context, 0, 30, 500, 90, draw.topColor1, draw.topColor2);
draw.triangle(this.context, 'rgb(28, 10, 87)', 500, 30, 450, 90, 500, 90);
draw.horizontalGradientSingle(this.context, 0, 90, 52, 399, draw.sideColor1, draw.sideColor2);
draw.horizontalGradientSingle(this.context, 584, 90, 640, 399, draw.sideColor1, draw.sideColor2);
draw.titleText(this.context, 'Local ', 'Forecast');
// clear existing text
draw.box(this.context, 'rgb(33, 40, 90)', 65, 105, 505, 280);
// Draw the text.
this.screenTexts[this.screenIndex].split('\n').forEach((text, index) => {
2020-10-29 21:44:28 +00:00
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 75, 140 + 40 * index, text, 2);
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
}