ws4kp/server/scripts/modules/currentweatherscroll.js

124 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-09-25 03:44:51 +00:00
'use strict';
/* globals draw, navigation */
// eslint-disable-next-line no-unused-vars
const currentWeatherScroll = (() => {
2020-09-25 14:55:29 +00:00
// constants
const degree = String.fromCharCode(176);
2020-09-25 03:44:51 +00:00
// local variables
let context; // currently active context
let blankDrawArea; // original state of context
let station;
let interval;
let screenIndex = 0;
// start drawing conditions
// reset starts from the first item in the text scroll list
const start = (_context) => {
// see if there is a context available
if (!_context) return;
// store see if the context is new
if (_context !== context) {
// clean the outgoing context
cleanLastContext();
// store the new blank context
blankDrawArea = _context.getImageData(0, 405, 640, 75);
}
// store the context locally
context = _context;
// set up the interval if needed
if (!interval) {
2020-09-25 14:55:29 +00:00
interval = setInterval(incrementInterval, 4000);
2020-09-25 03:44:51 +00:00
}
// draw the data
drawScreen();
};
const stop = (reset) => {
cleanLastContext();
if (interval) interval = clearInterval(interval);
if (reset) screenIndex = 0;
};
const cleanLastContext = () => {
if (blankDrawArea) context.putImageData(blankDrawArea, 0, 405);
blankDrawArea = undefined;
context = undefined;
};
// increment interval, roll over
const incrementInterval = () => {
2020-09-25 14:55:29 +00:00
screenIndex = (screenIndex+1)%(screens.length);
2020-09-25 03:44:51 +00:00
// draw new text
drawScreen();
};
const drawScreen = () => {
// get the conditions
const data = navigation.getCurrentWeather();
// nothing to do if there's no data yet
if (!data) return;
// clean up any old text
context.putImageData(blankDrawArea, 0, 405);
2020-09-25 14:55:29 +00:00
drawCondition(screens[screenIndex](data));
2020-09-25 03:44:51 +00:00
};
2020-09-25 14:55:29 +00:00
// the "screens" are stored in an array for easy addition and removal
const screens = [
// station name
(data) => `Conditions at ${data.station.properties.name.substr(0,20)}`,
2020-09-25 14:55:29 +00:00
// temperature
(data) => {
let text = `Temp: ${data.Temperature}${degree} ${data.TemperatureUnit}`;
2020-10-02 03:35:19 +00:00
if (data.observations.heatIndex.value) {
2020-09-25 14:55:29 +00:00
text += ` Heat Index: ${data.HeatIndex}${degree} ${data.TemperatureUnit}`;
2020-10-02 03:35:19 +00:00
} else if (data.observations.windChill.value) {
2020-09-25 14:55:29 +00:00
text += ` Wind Chill: ${data.WindChill}${degree} ${data.TemperatureUnit}`;
}
return text;
},
// humidity
(data) => `Humidity: ${data.Humidity}${degree} ${data.TemperatureUnit} Dewpoint: ${data.DewPoint}${degree} ${data.TemperatureUnit}`,
// barometric pressure
(data) => `Barometric Pressure: ${data.Pressure} ${data.PressureDirection}`,
// wind
(data) => {
let text = '';
if (data.WindSpeed > 0) {
text = `Wind: ${data.WindDirection} ${data.WindSpeed} ${data.WindUnit}`;
} else {
text = 'Wind: Calm';
}
if (data.WindGust > 0) {
text += ` Gusts to ${data.WindGust}`;
}
return text;
},
// visibility
(data) => `Visib: ${data.Visibility} ${data.VisibilityUnit} Ceiling: ${data.Ceiling===0?'Unlimited':`${data.Ceiling} ${data.CeilingUnit}`}`,
];
2020-09-25 03:44:51 +00:00
// internal draw function with preset parameters
const drawCondition = (text) => {
draw.text(context, 'Star4000', '24pt', '#ffffff', 70, 430, text, 2);
};
// return the api
return {
start,
stop,
};
})();