2022-11-22 22:19:10 +00:00
|
|
|
import { locationCleanup } from './utils/string.mjs';
|
|
|
|
import { elemForEach } from './utils/elem.mjs';
|
2022-12-06 22:14:56 +00:00
|
|
|
import getCurrentWeather from './currentweather.mjs';
|
|
|
|
import { currentDisplay } from './navigation.mjs';
|
2022-11-22 22:19:10 +00:00
|
|
|
|
|
|
|
// constants
|
|
|
|
const degree = String.fromCharCode(176);
|
|
|
|
|
|
|
|
// local variables
|
|
|
|
let interval;
|
|
|
|
let screenIndex = 0;
|
|
|
|
|
|
|
|
// start drawing conditions
|
|
|
|
// reset starts from the first item in the text scroll list
|
|
|
|
const start = () => {
|
|
|
|
// store see if the context is new
|
|
|
|
|
|
|
|
// set up the interval if needed
|
|
|
|
if (!interval) {
|
|
|
|
interval = setInterval(incrementInterval, 4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw the data
|
|
|
|
drawScreen();
|
|
|
|
};
|
|
|
|
|
|
|
|
const stop = (reset) => {
|
|
|
|
if (reset) screenIndex = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// increment interval, roll over
|
|
|
|
const incrementInterval = () => {
|
2022-12-06 22:14:56 +00:00
|
|
|
// test current screen
|
|
|
|
const display = currentDisplay();
|
|
|
|
if (!display?.okToDrawCurrentConditions) {
|
|
|
|
stop(display?.elemId === 'progress');
|
|
|
|
return;
|
|
|
|
}
|
2022-11-22 22:19:10 +00:00
|
|
|
screenIndex = (screenIndex + 1) % (screens.length);
|
|
|
|
// draw new text
|
|
|
|
drawScreen();
|
|
|
|
};
|
|
|
|
|
|
|
|
const drawScreen = async () => {
|
|
|
|
// get the conditions
|
2022-12-12 19:53:33 +00:00
|
|
|
const data = await getCurrentWeather(() => this.stillWaiting());
|
2022-11-22 22:19:10 +00:00
|
|
|
|
|
|
|
// nothing to do if there's no data yet
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
drawCondition(screens[screenIndex](data));
|
|
|
|
};
|
|
|
|
|
|
|
|
// the "screens" are stored in an array for easy addition and removal
|
|
|
|
const screens = [
|
|
|
|
// station name
|
|
|
|
(data) => `Conditions at ${locationCleanup(data.station.properties.name).substr(0, 20)}`,
|
|
|
|
|
|
|
|
// temperature
|
|
|
|
(data) => {
|
2022-12-14 16:44:00 +00:00
|
|
|
let text = `Temp: ${data.Temperature}${degree}${data.TemperatureUnit}`;
|
2022-11-22 22:19:10 +00:00
|
|
|
if (data.observations.heatIndex.value) {
|
2022-12-14 16:44:00 +00:00
|
|
|
text += ` Heat Index: ${data.HeatIndex}${degree}${data.TemperatureUnit}`;
|
2022-11-22 22:19:10 +00:00
|
|
|
} else if (data.observations.windChill.value) {
|
2022-12-14 16:44:00 +00:00
|
|
|
text += ` Wind Chill: ${data.WindChill}${degree}${data.TemperatureUnit}`;
|
2022-11-22 22:19:10 +00:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
},
|
|
|
|
|
|
|
|
// humidity
|
2022-12-14 16:44:00 +00:00
|
|
|
(data) => `Humidity: ${data.Humidity}% Dewpoint: ${data.DewPoint}${degree}${data.TemperatureUnit}`,
|
2022-11-22 22:19:10 +00:00
|
|
|
|
|
|
|
// 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}`}`,
|
|
|
|
];
|
|
|
|
|
|
|
|
// internal draw function with preset parameters
|
|
|
|
const drawCondition = (text) => {
|
|
|
|
// update all html scroll elements
|
|
|
|
elemForEach('.weather-display .scroll .fixed', (elem) => {
|
|
|
|
elem.innerHTML = text;
|
|
|
|
});
|
|
|
|
};
|
2022-12-07 16:53:18 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
start();
|
|
|
|
});
|