ws4kp/server/scripts/modules/weatherdisplay.js

490 lines
15 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
// base weather display class
2022-07-29 21:12:42 +00:00
/* globals navigation, utils, luxon, currentWeatherScroll */
2020-09-04 18:02:20 +00:00
const STATUS = {
loading: Symbol('loading'),
loaded: Symbol('loaded'),
failed: Symbol('failed'),
noData: Symbol('noData'),
2020-09-18 16:24:45 +00:00
disabled: Symbol('disabled'),
2020-09-04 18:02:20 +00:00
};
// eslint-disable-next-line no-unused-vars
class WeatherDisplay {
2022-07-29 21:12:42 +00:00
constructor(navId, elemId, name, defaultEnabled, isHtml) {
2020-09-04 18:02:20 +00:00
// navId is used in messaging
this.navId = navId;
this.elemId = undefined;
this.gifs = [];
this.data = undefined;
this.loadingStatus = STATUS.loading;
2020-10-29 21:44:28 +00:00
this.name = name ?? elemId;
2020-10-21 01:04:51 +00:00
this.getDataCallbacks = [];
2022-07-29 21:12:42 +00:00
this.isHtml = isHtml;
2020-09-04 18:02:20 +00:00
// default navigation timing
this.timing = {
totalScreens: 1,
2020-09-25 14:55:29 +00:00
baseDelay: 9000, // 5 seconds
2020-09-04 18:02:20 +00:00
delay: 1, // 1*1second = 1 second total display time
};
this.navBaseCount = 0;
2020-09-09 19:29:03 +00:00
this.screenIndex = -1; // special starting condition
2020-09-04 18:02:20 +00:00
2020-09-18 19:59:58 +00:00
// create the canvas, also stores this.elemId
this.createCanvas(elemId);
if (elemId !== 'progress') this.addCheckbox(defaultEnabled);
2020-09-18 16:24:45 +00:00
if (this.enabled) {
this.setStatus(STATUS.loading);
} else {
this.setStatus(STATUS.disabled);
}
this.startNavCount();
2022-07-29 21:12:42 +00:00
// get any templates
this.loadTemplates();
2020-09-04 18:02:20 +00:00
}
2020-09-18 19:59:58 +00:00
addCheckbox(defaultEnabled = true) {
2020-09-18 16:24:45 +00:00
// get the saved status of the checkbox
2020-09-18 19:59:58 +00:00
let savedStatus = window.localStorage.getItem(`${this.elemId}Enabled`);
2020-09-18 16:24:45 +00:00
if (savedStatus === null) savedStatus = defaultEnabled;
if (savedStatus === 'true' || savedStatus === true) {
this.enabled = true;
} else {
this.enabled = false;
}
2020-09-18 19:59:58 +00:00
// refresh (or initially store the state of the checkbox)
window.localStorage.setItem(`${this.elemId}Enabled`, this.enabled);
2020-09-18 16:24:45 +00:00
// create a checkbox in the selected displays area
2020-09-18 19:59:58 +00:00
const checkbox = document.createElement('template');
checkbox.innerHTML = `<label for="${this.elemId}Enabled">
2020-10-29 21:44:28 +00:00
<input type="checkbox" value="true" id="${this.elemId}Enabled" name="${this.elemId}Enabled"${this.enabled ? ' checked' : ''}/>
2020-09-18 16:24:45 +00:00
${this.name}</label>`;
2020-09-18 19:59:58 +00:00
checkbox.content.firstChild.addEventListener('change', (e) => this.checkboxChange(e));
2020-09-18 16:24:45 +00:00
const availableDisplays = document.getElementById('enabledDisplays');
2020-09-18 19:59:58 +00:00
availableDisplays.appendChild(checkbox.content.firstChild);
2020-09-18 16:24:45 +00:00
}
checkboxChange(e) {
2020-09-18 19:59:58 +00:00
// update the state
this.enabled = e.target.checked;
// store the value for the next load
window.localStorage.setItem(`${this.elemId}Enabled`, this.enabled);
// calling get data will update the status and actually get the data if we're set to enabled
this.getData();
2020-09-18 16:24:45 +00:00
}
2020-09-04 18:02:20 +00:00
// set data status and send update to navigation module
setStatus(value) {
this.status = value;
navigation.updateStatus({
id: this.navId,
status: this.status,
});
}
get status() {
return this.loadingStatus;
}
set status(state) {
this.loadingStatus = state;
}
createCanvas(elemId, width = 640, height = 480) {
// only create it once
if (this.elemId) return;
this.elemId = elemId;
2020-09-23 15:39:03 +00:00
2022-07-29 21:12:42 +00:00
// no additional work if this is HTML
if (this.isHtml) return;
2020-09-23 15:39:03 +00:00
// create a canvas
const canvas = document.createElement('template');
2020-10-29 21:44:28 +00:00
canvas.innerHTML = `<canvas id='${`${elemId}Canvas`}' width='${width}' height='${height}' style='display: none;' />`;
2020-09-23 15:39:03 +00:00
// add to the page
2020-09-04 18:02:20 +00:00
const container = document.getElementById('container');
2020-09-23 15:39:03 +00:00
container.appendChild(canvas.content.firstChild);
2020-09-04 18:02:20 +00:00
}
// get necessary data for this display
2020-09-25 14:55:29 +00:00
getData(weatherParameters) {
2020-09-04 18:02:20 +00:00
// clear current data
this.data = undefined;
2020-09-18 16:24:45 +00:00
2020-09-25 14:55:29 +00:00
// store weatherParameters locally in case we need them later
if (weatherParameters) this.weatherParameters = weatherParameters;
// set status
2020-09-18 16:24:45 +00:00
if (this.enabled) {
this.setStatus(STATUS.loading);
} else {
this.setStatus(STATUS.disabled);
return false;
}
2020-09-09 19:29:03 +00:00
// recalculate navigation timing (in case it was modified in the constructor)
this.calcNavTiming();
2020-09-18 16:24:45 +00:00
return true;
2020-09-04 18:02:20 +00:00
}
2020-10-21 01:04:51 +00:00
// return any data requested before it was available
getDataCallback() {
// call each callback
2020-10-29 21:44:28 +00:00
this.getDataCallbacks.forEach((fxn) => fxn(this.data));
2020-10-21 01:04:51 +00:00
// clear the callbacks
this.getDataCallbacks = [];
}
2020-09-04 18:02:20 +00:00
drawCanvas() {
2022-07-29 21:12:42 +00:00
if (!this.isHtml) {
2020-09-04 18:02:20 +00:00
// stop all gifs
2022-07-29 21:12:42 +00:00
this.gifs.forEach((gif) => gif.pause());
// delete the gifs
this.gifs.length = 0;
// refresh the canvas
this.canvas = document.getElementById(`${this.elemId}Canvas`);
this.context = this.canvas.getContext('2d');
}
2020-09-18 19:59:58 +00:00
// clean up the first-run flag in screen index
2020-10-29 21:44:28 +00:00
if (this.screenIndex < 0) this.screenIndex = 0;
2020-09-04 18:02:20 +00:00
}
finishDraw() {
let OkToDrawCurrentConditions = true;
let OkToDrawNoaaImage = true;
let OkToDrawCurrentDateTime = true;
let OkToDrawLogoImage = true;
// let OkToDrawCustomScrollText = false;
2020-10-29 21:44:28 +00:00
let bottom;
2020-09-04 18:02:20 +00:00
// visibility tests
// if (_ScrollText !== '') OkToDrawCustomScrollText = true;
if (this.elemId === 'almanac') OkToDrawNoaaImage = false;
if (this.elemId === 'travelForecast') OkToDrawNoaaImage = false;
2020-09-09 19:29:03 +00:00
if (this.elemId === 'regionalForecast') OkToDrawNoaaImage = false;
2020-09-25 03:44:51 +00:00
if (this.elemId === 'progress') {
OkToDrawCurrentConditions = false;
OkToDrawNoaaImage = false;
}
2020-09-06 01:01:13 +00:00
if (this.elemId === 'radar') {
2020-09-04 18:02:20 +00:00
OkToDrawCurrentConditions = false;
OkToDrawCurrentDateTime = false;
OkToDrawNoaaImage = false;
// OkToDrawCustomScrollText = false;
}
if (this.elemId === 'hazards') {
OkToDrawNoaaImage = false;
bottom = true;
OkToDrawLogoImage = false;
}
// draw functions
if (OkToDrawCurrentDateTime) {
this.drawCurrentDateTime(bottom);
// auto clock refresh
if (!this.dateTimeInterval) {
setInterval(() => this.drawCurrentDateTime(bottom), 100);
}
}
if (OkToDrawLogoImage) this.drawLogoImage();
if (OkToDrawNoaaImage) this.drawNoaaImage();
2020-09-25 03:44:51 +00:00
if (OkToDrawCurrentConditions) {
currentWeatherScroll.start(this.context);
} else {
// cause a reset if the progress screen is displayed
currentWeatherScroll.stop(this.elemId === 'progress');
}
2020-09-04 18:02:20 +00:00
// TODO: add custom scroll text
// if (OkToDrawCustomScrollText) DrawCustomScrollText(WeatherParameters, context);
}
2022-07-29 21:12:42 +00:00
drawCurrentDateTime() {
2020-09-04 18:02:20 +00:00
// only draw if canvas is active to conserve battery
if (!this.isActive()) return;
2020-10-29 21:44:28 +00:00
const { DateTime } = luxon;
2020-09-04 18:02:20 +00:00
// Get the current date and time.
const now = DateTime.local();
2020-10-29 21:44:28 +00:00
// time = "11:35:08 PM";
const time = now.toLocaleString(DateTime.TIME_WITH_SECONDS).padStart(11, ' ');
2020-09-04 18:02:20 +00:00
2022-07-29 21:12:42 +00:00
if (this.lastTime !== time) {
utils.elem.forEach('.date-time.time', (elem) => { elem.innerHTML = time.toUpperCase(); });
2020-09-04 18:02:20 +00:00
}
2022-07-29 21:12:42 +00:00
this.lastTime = time;
2020-09-04 18:02:20 +00:00
2020-10-29 21:44:28 +00:00
const date = now.toFormat(' ccc LLL ') + now.day.toString().padStart(2, ' ');
2020-09-04 18:02:20 +00:00
2022-07-29 21:12:42 +00:00
if (this.lastDate !== date) {
utils.elem.forEach('.date-time.date', (elem) => { elem.innerHTML = date.toUpperCase(); });
2020-09-04 18:02:20 +00:00
}
2022-07-29 21:12:42 +00:00
this.lastDate = date;
2020-09-04 18:02:20 +00:00
}
2020-10-29 21:44:28 +00:00
async drawNoaaImage() {
2022-07-29 21:12:42 +00:00
if (this.isHtml) return;
2020-09-04 18:02:20 +00:00
// load the image and store locally
if (!this.drawNoaaImage.image) {
this.drawNoaaImage.image = utils.image.load('images/noaa5.gif');
}
// wait for the image to load completely
const img = await this.drawNoaaImage.image;
this.context.drawImage(img, 356, 39);
}
2020-10-29 21:44:28 +00:00
async drawLogoImage() {
2022-07-29 21:12:42 +00:00
if (this.isHtml) return;
2020-09-04 18:02:20 +00:00
// load the image and store locally
if (!this.drawLogoImage.image) {
this.drawLogoImage.image = utils.image.load('images/Logo3.png');
}
// wait for the image load completely
const img = await this.drawLogoImage.image;
this.context.drawImage(img, 50, 30, 85, 67);
}
// show/hide the canvas and start/stop the navigation timer
showCanvas(navCmd) {
// reset timing if enabled
2020-09-04 18:02:20 +00:00
// if a nav command is present call it to set the screen index
if (navCmd === navigation.msg.command.firstFrame) this.navNext(navCmd);
if (navCmd === navigation.msg.command.lastFrame) this.navPrev(navCmd);
2022-07-29 21:12:42 +00:00
this.startNavCount();
if (!this.isHtml) {
2020-09-04 18:02:20 +00:00
// see if the canvas is already showing
2022-07-29 21:12:42 +00:00
if (this.canvas.style.display === 'block') return;
2020-09-04 18:02:20 +00:00
2022-07-29 21:12:42 +00:00
// show the canvas
this.canvas.style.display = 'block';
} else {
this.elem.classList.add('show');
}
2020-09-04 18:02:20 +00:00
}
2020-10-29 21:44:28 +00:00
2020-09-04 18:02:20 +00:00
hideCanvas() {
this.resetNavBaseCount();
2020-09-04 18:02:20 +00:00
2022-07-29 21:12:42 +00:00
if (this.canvas) {
this.canvas.style.display = 'none';
}
if (this.isHtml) {
this.elem.classList.remove('show');
}
2020-09-04 18:02:20 +00:00
}
isActive() {
2022-07-29 21:12:42 +00:00
if (!this.isHtml) return document.getElementById(`${this.elemId}Canvas`).offsetParent !== null;
2022-08-04 16:07:35 +00:00
return this.elem.offsetHeight !== 0;
2020-09-04 18:02:20 +00:00
}
2020-09-17 21:34:38 +00:00
isEnabled() {
2020-09-18 16:24:45 +00:00
return this.enabled;
2020-09-17 21:34:38 +00:00
}
2020-09-04 18:02:20 +00:00
// navigation timings
// totalScreens = total number of screens that are available
// baseDelay = ms to delay before re-evaluating screenIndex
// delay: three options
// integer = each screen will display for this number of baseDelays
// [integer, integer, ...] = screenIndex 0 displays for integer[0]*baseDelay, etc.
// [{time, si}, ...] = time as above, si is specific screen index to display during this interval
// if the array forms are used totalScreens is overwritten by the size of the array
navBaseTime() {
// see if play is active and screen is active
if (!navigation.isPlaying() || !this.isActive()) return;
// increment the base count
2020-10-29 21:44:28 +00:00
this.navBaseCount += 1;
2020-09-04 18:02:20 +00:00
// call base count change if available for this function
if (this.baseCountChange) this.baseCountChange(this.navBaseCount);
2020-09-04 18:02:20 +00:00
2020-09-09 19:29:03 +00:00
// handle base count/screen index changes
this.updateScreenFromBaseCount();
}
2020-09-17 21:34:38 +00:00
async updateScreenFromBaseCount() {
2020-09-09 19:29:03 +00:00
// get the next screen index
2020-10-29 21:44:28 +00:00
const nextScreenIndex = this.screenIndexFromBaseCount();
2020-09-09 19:29:03 +00:00
// special cases for first and last frame
// must compare with false as nextScreenIndex could be 0 which is valid
if (nextScreenIndex === false) {
this.sendNavDisplayMessage(navigation.msg.response.next);
2020-09-04 18:02:20 +00:00
return;
}
2020-09-09 19:29:03 +00:00
// test for no change and exit early
if (nextScreenIndex === this.screenIndex) return;
2020-09-17 21:34:38 +00:00
// test for -1 (no screen displayed yet)
if (nextScreenIndex === -1) {
this.screenIndex = 0;
} else {
this.screenIndex = nextScreenIndex;
}
2020-09-09 19:29:03 +00:00
// call the appropriate screen index change method
if (!this.screenIndexChange) {
2020-09-17 21:34:38 +00:00
await this.drawCanvas();
this.showCanvas();
2020-09-09 19:29:03 +00:00
} else {
this.screenIndexChange(this.screenIndex);
}
}
// take the three timing formats shown above and break them into arrays for consistent usage in navigation functions
// this.timing.fullDelay = [end of screen index 0 in base counts, end of screen index 1...]
// this.timing.screenIndexes = [screen index to use during this.timing.fullDelay[0], screen index to use during this.timing.fullDelay[1], ...]
calcNavTiming() {
if (this.timing === false) return;
2020-09-09 19:29:03 +00:00
// update total screens
if (Array.isArray(this.timing.delay)) this.timing.totalScreens = this.timing.delay.length;
// if the delay is provided as a single value, expand it to a series of the same value
let intermediateDelay = [];
if (typeof this.timing.delay === 'number') {
2020-10-29 21:44:28 +00:00
for (let i = 0; i < this.timing.totalScreens; i += 1) intermediateDelay.push(this.timing.delay);
2020-09-09 19:29:03 +00:00
} else {
// map just the delays to the intermediate block
2020-10-29 21:44:28 +00:00
intermediateDelay = this.timing.delay.map((delay) => {
2020-09-09 19:29:03 +00:00
if (typeof delay === 'object') return delay.time;
return delay;
});
}
// calculate the cumulative end point of each delay
let sum = 0;
2020-10-29 21:44:28 +00:00
this.timing.fullDelay = intermediateDelay.map((val) => {
2020-09-09 19:29:03 +00:00
const calc = sum + val;
sum += val;
return calc;
});
// generate a list of screen either sequentially if not provided in an object or from the object
if (Array.isArray(this.timing.delay) && typeof this.timing.delay[0] === 'object') {
// extract screen indexes from objects
2020-10-29 21:44:28 +00:00
this.timing.screenIndexes = this.timing.delay.map((delay) => delay.si);
2020-09-09 19:29:03 +00:00
} else {
// generate sequential screen indexes
this.timing.screenIndexes = [];
2020-10-29 21:44:28 +00:00
for (let i = 0; i < this.timing.totalScreens; i += 1) this.timing.screenIndexes.push(i);
}
2020-09-04 18:02:20 +00:00
}
// navigate to next screen
navNext(command) {
// check for special 'first frame' command
if (command === navigation.msg.command.firstFrame) {
this.resetNavBaseCount();
} else {
2020-09-09 19:29:03 +00:00
// set the base count to the next available frame
2020-10-29 21:44:28 +00:00
const newBaseCount = this.timing.fullDelay.find((delay) => delay > this.navBaseCount);
2020-09-09 19:29:03 +00:00
this.navBaseCount = newBaseCount;
}
2020-09-09 19:29:03 +00:00
this.updateScreenFromBaseCount();
2020-09-04 18:02:20 +00:00
}
// navigate to previous screen
navPrev(command) {
// check for special 'last frame' command
if (command === navigation.msg.command.lastFrame) {
2020-10-29 21:44:28 +00:00
this.navBaseCount = this.timing.fullDelay[this.timing.totalScreens - 1] - 1;
} else {
2020-09-09 19:29:03 +00:00
// find the highest fullDelay that is less than the current base count
const newBaseCount = this.timing.fullDelay.reduce((acc, delay) => {
if (delay < this.navBaseCount) return delay;
return acc;
2020-10-29 21:44:28 +00:00
}, 0);
2020-09-09 19:29:03 +00:00
// if the new base count is zero then we're already at the first screen
if (newBaseCount === 0 && this.navBaseCount === 0) {
this.sendNavDisplayMessage(navigation.msg.response.previous);
return;
}
this.navBaseCount = newBaseCount;
}
2020-09-09 19:29:03 +00:00
this.updateScreenFromBaseCount();
}
2020-09-09 19:29:03 +00:00
// get the screen index for the current base count, returns false if past end of timing array (go to next screen, stop timing)
screenIndexFromBaseCount() {
2020-09-25 18:25:12 +00:00
// test for timing enabled
if (!this.timing) return 0;
2020-09-09 19:29:03 +00:00
// find the first timing in the timing array that is greater than the base count
2020-09-25 18:25:12 +00:00
if (this.timing && !this.timing.fullDelay) this.calcNavTiming();
2020-10-29 21:44:28 +00:00
const timingIndex = this.timing.fullDelay.findIndex((delay) => delay > this.navBaseCount);
2020-09-09 19:29:03 +00:00
if (timingIndex === -1) return false;
return this.timing.screenIndexes[timingIndex];
2020-09-04 18:02:20 +00:00
}
// start and stop base counter
startNavCount() {
2020-10-29 21:44:28 +00:00
if (!this.navInterval) this.navInterval = setInterval(() => this.navBaseTime(), this.timing.baseDelay);
2020-09-04 18:02:20 +00:00
}
2020-09-04 18:02:20 +00:00
resetNavBaseCount() {
this.navBaseCount = 0;
2020-09-09 19:29:03 +00:00
this.screenIndex = -1;
// reset the timing so we don't short-change the first screen
if (this.navInterval) {
clearInterval(this.navInterval);
this.navInterval = undefined;
}
2020-09-04 18:02:20 +00:00
}
sendNavDisplayMessage(message) {
navigation.displayNavMessage({
id: this.navId,
type: message,
});
}
2022-07-29 21:12:42 +00:00
loadTemplates() {
this.templates = {};
2022-08-05 17:05:14 +00:00
this.elem = document.getElementById(`${this.elemId}-html`);
if (!this.elem) return;
const templates = this.elem.querySelectorAll('.template');
templates.forEach((template) => {
const className = template.classList[0];
const node = template.cloneNode(true);
node.classList.remove('template');
this.templates[className] = node;
template.remove();
});
2022-07-29 21:12:42 +00:00
}
2022-08-03 02:39:27 +00:00
fillTemplate(name, fillValues) {
// get the template
const templateNode = this.templates[name];
if (!templateNode) return false;
// clone it
const template = templateNode.cloneNode(true);
Object.entries(fillValues).forEach(([key, value]) => {
// get the specified element
const elem = template.querySelector(`.${key}`);
if (!elem) return;
// fill based on type provided
if (typeof value === 'string' || typeof value === 'number') {
// string and number fill the first found selector
elem.innerHTML = value;
} else if (value?.type === 'img') {
// fill the image source
elem.querySelector('img').src = value.src;
}
});
return template;
}
2020-10-29 21:44:28 +00:00
}