current conditions ready for data

This commit is contained in:
Matt Walsh
2020-09-24 22:44:51 -05:00
parent 965cb29699
commit 0a6a31217b
12 changed files with 314 additions and 122 deletions

View File

@@ -38,7 +38,6 @@ class Almanac extends WeatherDisplay {
// process images for outlook
const [outlookTemp, outlookPrecip] = await Promise.all(imagePromises);
console.log(outlookTemp,outlookPrecip);
const outlook = this.parseOutlooks(weatherParameters.latitude, weatherParameters.longitude, outlookTemp, outlookPrecip);
// store the data
@@ -47,8 +46,8 @@ class Almanac extends WeatherDisplay {
moon,
outlook,
};
// draw the canvas
this.drawCanvas();
// update status
this.setStatus(STATUS.loaded);
}
@@ -295,7 +294,6 @@ class Almanac extends WeatherDisplay {
});
this.finishDraw();
this.setStatus(STATUS.loaded);
break;
case 1:

View File

@@ -50,45 +50,55 @@ class CurrentWeather extends WeatherDisplay {
this.setStatus(STATUS.loaded);
}
async drawCanvas () {
super.drawCanvas();
// format the data for use outside this function
parseData() {
if (!this.data) return false;
const data = {};
const observations = this.data.features[0].properties;
// values from api are provided in metric
let Temperature = Math.round(observations.temperature.value);
let DewPoint = Math.round(observations.dewpoint.value);
let Ceiling = Math.round(observations.cloudLayers[0].base.value);
let CeilingUnit = 'm.';
let Visibility = Math.round(observations.visibility.value/1000);
let VisibilityUnit = ' km.';
let WindSpeed = Math.round(observations.windSpeed.value);
const WindDirection = utils.calc.directionToNSEW(observations.windDirection.value);
let Pressure = Math.round(observations.barometricPressure.value);
let HeatIndex = Math.round(observations.heatIndex.value);
let WindChill = Math.round(observations.windChill.value);
let WindGust = Math.round(observations.windGust.value);
let Humidity = Math.round(observations.relativeHumidity.value);
const Icon = icons.getWeatherIconFromIconLink(observations.icon);
let PressureDirection = '';
const TextConditions = observations.textDescription;
data.observations = observations;
data.Temperature = Math.round(observations.temperature.value);
data.DewPoint = Math.round(observations.dewpoint.value);
data.Ceiling = Math.round(observations.cloudLayers[0].base.value);
data.CeilingUnit = 'm.';
data.Visibility = Math.round(observations.visibility.value/1000);
data.VisibilityUnit = ' km.';
data.WindSpeed = Math.round(observations.windSpeed.value);
data.WindDirection = utils.calc.directionToNSEW(observations.windDirection.value);
data.Pressure = Math.round(observations.barometricPressure.value);
data.HeatIndex = Math.round(observations.heatIndex.value);
data.WindChill = Math.round(observations.windChill.value);
data.WindGust = Math.round(observations.windGust.value);
data.Humidity = Math.round(observations.relativeHumidity.value);
data.Icon = icons.getWeatherIconFromIconLink(observations.icon);
data.PressureDirection = '';
data.TextConditions = observations.textDescription;
// difference since last measurement (pascals, looking for difference of more than 150)
const pressureDiff = (observations.barometricPressure.value - this.data.features[1].properties.barometricPressure.value);
if (pressureDiff > 150) PressureDirection = 'R';
if (pressureDiff < -150) PressureDirection = 'F';
if (pressureDiff > 150) data.PressureDirection = 'R';
if (pressureDiff < -150) data.PressureDirection = 'F';
if (navigation.units() === UNITS.english) {
Temperature = utils.units.celsiusToFahrenheit(Temperature);
DewPoint = utils.units.celsiusToFahrenheit(DewPoint);
Ceiling = Math.round(utils.units.metersToFeet(Ceiling)/100)*100;
CeilingUnit = 'ft.';
Visibility = utils.units.kilometersToMiles(observations.visibility.value/1000);
VisibilityUnit = ' mi.';
WindSpeed = utils.units.kphToMph(WindSpeed);
Pressure = utils.units.pascalToInHg(Pressure);
HeatIndex = utils.units.celsiusToFahrenheit(HeatIndex);
WindChill = utils.units.celsiusToFahrenheit(WindChill);
WindGust = utils.units.kphToMph(WindGust);
data.Temperature = utils.units.celsiusToFahrenheit(data.Temperature);
data.DewPoint = utils.units.celsiusToFahrenheit(data.DewPoint);
data.Ceiling = Math.round(utils.units.metersToFeet(data.Ceiling)/100)*100;
data.CeilingUnit = 'ft.';
data.Visibility = utils.units.kilometersToMiles(observations.visibility.value/1000);
data.VisibilityUnit = ' mi.';
data.WindSpeed = utils.units.kphToMph(data.WindSpeed);
data.Pressure = utils.units.pascalToInHg(data.Pressure);
data.HeatIndex = utils.units.celsiusToFahrenheit(data.HeatIndex);
data.WindChill = utils.units.celsiusToFahrenheit(data.WindChill);
data.WindGust = utils.units.kphToMph(data.WindGust);
}
return data;
}
async drawCanvas () {
super.drawCanvas();
// parse each time to deal with a change in units if necessary
const data = this.parseData();
this.context.drawImage(await this.backgroundImage, 0, 0);
draw.horizontalGradientSingle(this.context, 0, 30, 500, 90, draw.topColor1, draw.topColor2);
@@ -98,37 +108,37 @@ class CurrentWeather extends WeatherDisplay {
draw.titleText(this.context, 'Current', 'Conditions');
draw.text(this.context, 'Star4000 Large', '24pt', '#FFFFFF', 170, 135, Temperature + String.fromCharCode(176), 2);
draw.text(this.context, 'Star4000 Large', '24pt', '#FFFFFF', 170, 135, data.Temperature + String.fromCharCode(176), 2);
let Conditions = observations.textDescription;
if (TextConditions.length > 15) {
let Conditions = data.observations.textDescription;
if (Conditions.length > 15) {
Conditions = this.shortConditions(Conditions);
}
draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 195, 170, Conditions, 2, 'center');
draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 80, 330, 'Wind:', 2);
draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 300, 330, WindDirection + ' ' + WindSpeed, 2, 'right');
draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 300, 330, data.WindDirection + ' ' + data.WindSpeed, 2, 'right');
if (WindGust) draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 80, 375, 'Gusts to ' + WindGust, 2);
if (data.WindGust) draw.text(this.context, 'Star4000 Extended', '24pt', '#FFFFFF', 80, 375, 'Gusts to ' + data.WindGust, 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFF00', 315, 120, this.data.station.properties.name.substr(0, 20), 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 165, 'Humidity:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 165, Humidity + '%', 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 165, data.Humidity + '%', 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 205, 'Dewpoint:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 205, DewPoint + String.fromCharCode(176), 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 205, data.DewPoint + String.fromCharCode(176), 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 245, 'Ceiling:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 245, (Ceiling === '' ? 'Unlimited' : Ceiling + CeilingUnit), 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 245, (data.Ceiling === '' ? 'Unlimited' : data.Ceiling + data.CeilingUnit), 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 285, 'Visibility:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 285, Visibility + VisibilityUnit, 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 285, data.Visibility + data.VisibilityUnit, 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 325, 'Pressure:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 535, 325, Pressure, 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 535, 325, data.Pressure, 2, 'right');
switch (PressureDirection) {
switch (data.PressureDirection) {
case 'R':
// Shadow
draw.triangle(this.context, '#000000', 552, 302, 542, 312, 562, 312);
@@ -158,17 +168,17 @@ class CurrentWeather extends WeatherDisplay {
default:
}
if (observations.heatIndex.value && HeatIndex !== Temperature) {
if (data.observations.heatIndex.value && data.HeatIndex !== data.Temperature) {
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 365, 'Heat Index:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 365, HeatIndex + String.fromCharCode(176), 2, 'right');
} else if (observations.windChill.value && WindChill !== '' && WindChill < Temperature) {
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 365, data.HeatIndex + String.fromCharCode(176), 2, 'right');
} else if (data.observations.windChill.value && data.WindChill !== '' && data.WindChill < data.Temperature) {
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 340, 365, 'Wind Chill:', 2);
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 365, WindChill + String.fromCharCode(176), 2, 'right');
draw.text(this.context, 'Star4000 Large', 'bold 16pt', '#FFFFFF', 560, 365, data.WindChill + String.fromCharCode(176), 2, 'right');
}
// get main icon
this.gifs.push(await utils.image.superGifAsync({
src: Icon,
src: data.Icon,
auto_play: true,
canvas: this.canvas,
x: 140,
@@ -179,6 +189,11 @@ class CurrentWeather extends WeatherDisplay {
this.finishDraw();
}
// return the latest gathered information if available
getCurrentWeather() {
return this.parseData();
}
shortConditions(condition) {
condition = condition.replace(/Light/g, 'L');
condition = condition.replace(/Heavy/g, 'H');

View File

@@ -0,0 +1,96 @@
'use strict';
/* globals draw, navigation */
// eslint-disable-next-line no-unused-vars
const currentWeatherScroll = (() => {
// 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) {
interval = setInterval(incrementInterval, 700);
}
// 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 = () => {
screenIndex = (screenIndex+1)%2;
// 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;
if (!station) return;
// clean up any old text
context.putImageData(blankDrawArea, 0, 405);
switch (screenIndex) {
case 0:
default:
drawCondition(`Conditions at ${station.name.substr(0,20)}`);
break;
case 1:
drawCondition(`Page 2`);
break;
}
};
// internal draw function with preset parameters
const drawCondition = (text) => {
draw.text(context, 'Star4000', '24pt', '#ffffff', 70, 430, text, 2);
};
// store the latest station data
const setStation = (weatherParameters) => {
station = weatherParameters.stations[0].properties;
};
// return the api
return {
start,
stop,
setStation,
};
})();

View File

@@ -1,7 +1,7 @@
'use strict';
// navigation handles progress, next/previous and initial load messages from the parent frame
/* globals index, utils, _StationInfo, STATUS */
/* globals CurrentWeather, LatestObservations, TravelForecast, RegionalForecast, LocalForecast, ExtendedForecast, Almanac, Radar, Progress */
/* globals CurrentWeather, LatestObservations, TravelForecast, RegionalForecast, LocalForecast, ExtendedForecast, Almanac, Radar, Progress, currentWeatherScroll */
document.addEventListener('DOMContentLoaded', () => {
navigation.init();
@@ -19,6 +19,9 @@ const navigation = (() => {
let playing = false;
let progress;
// current conditions are made available from the display below
let currentWeather;
const init = async () => {
// nothing to do
};
@@ -94,8 +97,9 @@ const navigation = (() => {
// start loading canvases if necessary
if (displays.length === 0) {
currentWeather = new CurrentWeather(0,'currentWeather');
displays = [
new CurrentWeather(0,'currentWeather'),
currentWeather,
new LatestObservations(1, 'latestObservations'),
new TravelForecast(2, 'travelForecast', false), // not active by default
new RegionalForecast(3, 'regionalForecast'),
@@ -107,6 +111,8 @@ const navigation = (() => {
}
// call for new data on each display
displays.forEach(display => display.getData(weatherParameters));
// pass the information to the bottom scroll
currentWeatherScroll.setStation(weatherParameters);
// GetMonthPrecipitation(this.weatherParameters);
// GetAirQuality3(this.weatherParameters);
@@ -256,6 +262,12 @@ const navigation = (() => {
// return the specificed display
const getDisplay = (index) => displays[index];
// get current conditions
const getCurrentWeather = () => {
if (!currentWeather) return false;
return currentWeather.getCurrentWeather();
};
return {
init,
message,
@@ -265,5 +277,6 @@ const navigation = (() => {
displayNavMessage,
msg,
getDisplay,
getCurrentWeather,
};
})();

View File

@@ -386,7 +386,6 @@ class RegionalForecast extends WeatherDisplay {
}));
this.finishDraw();
this.setStatus(STATUS.loaded);
}
}

View File

@@ -56,7 +56,6 @@ const utils = (() => {
const img = new Image();
img.scr = src;
cachedImages.push(src);
console.log(cachedImages);
return true;
};

View File

@@ -1,6 +1,6 @@
// base weather display class
/* globals navigation, utils, draw, UNITS, luxon */
/* globals navigation, utils, draw, UNITS, luxon, currentWeatherScroll */
const STATUS = {
loading: Symbol('loading'),
@@ -150,6 +150,10 @@ class WeatherDisplay {
if (this.elemId === 'almanac') OkToDrawNoaaImage = false;
if (this.elemId === 'travelForecast') OkToDrawNoaaImage = false;
if (this.elemId === 'regionalForecast') OkToDrawNoaaImage = false;
if (this.elemId === 'progress') {
OkToDrawCurrentConditions = false;
OkToDrawNoaaImage = false;
}
if (this.elemId === 'radar') {
OkToDrawCurrentConditions = false;
OkToDrawCurrentDateTime = false;
@@ -171,8 +175,12 @@ class WeatherDisplay {
}
if (OkToDrawLogoImage) this.drawLogoImage();
if (OkToDrawNoaaImage) this.drawNoaaImage();
// TODO: fix current conditions scroll
// if (OkToDrawCurrentConditions) DrawCurrentConditions(WeatherParameters, this.context);
if (OkToDrawCurrentConditions) {
currentWeatherScroll.start(this.context);
} else {
// cause a reset if the progress screen is displayed
currentWeatherScroll.stop(this.elemId === 'progress');
}
// TODO: add custom scroll text
// if (OkToDrawCustomScrollText) DrawCustomScrollText(WeatherParameters, context);
}