ws4kp/server/scripts/modules/latestobservations.js

136 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
// current weather conditions display
2020-10-29 21:44:28 +00:00
/* globals WeatherDisplay, utils, STATUS, UNITS, draw, navigation, StationInfo */
2020-09-04 18:02:20 +00:00
// eslint-disable-next-line no-unused-vars
class LatestObservations extends WeatherDisplay {
2020-10-29 21:44:28 +00:00
constructor(navId, elemId) {
super(navId, elemId, 'Latest Observations');
2020-09-04 18:02:20 +00:00
// pre-load background image (returns promise)
this.backgroundImage = utils.image.load('images/BackGround1_1.png');
// constants
this.MaximumRegionalStations = 7;
}
2020-10-29 21:44:28 +00:00
async getData(_weatherParameters) {
super.getData(_weatherParameters);
const weatherParameters = _weatherParameters ?? this.weatherParameters;
2020-09-25 14:55:29 +00:00
2020-09-04 18:02:20 +00:00
// calculate distance to each station
2020-10-29 21:44:28 +00:00
const stationsByDistance = Object.keys(StationInfo).map((key) => {
const station = StationInfo[key];
2020-09-25 20:11:19 +00:00
const distance = utils.calc.distance(station.lat, station.lon, weatherParameters.latitude, weatherParameters.longitude);
2020-10-29 21:44:28 +00:00
return { ...station, distance };
2020-09-04 18:02:20 +00:00
});
// sort the stations by distance
2020-10-29 21:44:28 +00:00
const sortedStations = stationsByDistance.sort((a, b) => a.distance - b.distance);
2020-09-04 18:02:20 +00:00
// try up to 30 regional stations
2020-10-29 21:44:28 +00:00
const regionalStations = sortedStations.slice(0, 30);
2020-09-04 18:02:20 +00:00
// get data for regional stations
2020-10-29 21:44:28 +00:00
const allConditions = await Promise.all(regionalStations.map(async (station) => {
2020-09-04 18:02:20 +00:00
try {
2020-10-02 02:35:49 +00:00
const data = await utils.fetch.json(`https://api.weather.gov/stations/${station.id}/observations/latest`);
// test for temperature, weather and wind values present
2020-10-29 21:44:28 +00:00
if (data.properties.temperature.value === null
|| data.properties.textDescription === ''
|| data.properties.windSpeed.value === null) return false;
2020-09-04 18:02:20 +00:00
// format the return values
2020-10-29 21:44:28 +00:00
return {
...data.properties,
2020-09-25 20:11:19 +00:00
StationId: station.id,
city: station.city,
2020-10-29 21:44:28 +00:00
};
2020-09-04 18:02:20 +00:00
} catch (e) {
2020-09-25 20:11:19 +00:00
console.log(`Unable to get latest observations for ${station.id}`);
2020-10-29 21:44:28 +00:00
return false;
2020-09-04 18:02:20 +00:00
}
}));
// remove and stations that did not return data
2020-10-29 21:44:28 +00:00
const actualConditions = allConditions.filter((condition) => condition);
2020-09-04 18:02:20 +00:00
// cut down to the maximum of 7
2020-10-29 21:44:28 +00:00
this.data = actualConditions.slice(0, this.MaximumRegionalStations);
2020-09-04 18:02:20 +00:00
// test for at least one station
if (this.data.length < 1) {
this.setStatus(STATUS.noData);
return;
}
2020-09-17 21:34:38 +00:00
this.setStatus(STATUS.loaded);
2020-09-04 18:02:20 +00:00
}
async drawCanvas() {
super.drawCanvas();
const conditions = this.data;
// sort array by station name
2020-10-29 21:44:28 +00:00
const sortedConditions = conditions.sort((a, b) => ((a.Name < b.Name) ? -1 : 1));
2020-09-04 18:02:20 +00:00
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, 'Latest', 'Observations');
if (navigation.units() === UNITS.english) {
2020-10-29 21:44:28 +00:00
draw.text(this.context, 'Star4000 Small', '24pt', '#FFFFFF', 295, 105, `${String.fromCharCode(176)}F`, 2);
2020-09-04 18:02:20 +00:00
} else {
2020-10-29 21:44:28 +00:00
draw.text(this.context, 'Star4000 Small', '24pt', '#FFFFFF', 295, 105, `${String.fromCharCode(176)}C`, 2);
2020-09-04 18:02:20 +00:00
}
draw.text(this.context, 'Star4000 Small', '24pt', '#FFFFFF', 345, 105, 'WEATHER', 2);
draw.text(this.context, 'Star4000 Small', '24pt', '#FFFFFF', 495, 105, 'WIND', 2);
let y = 140;
sortedConditions.forEach((condition) => {
let Temperature = condition.temperature.value;
let WindSpeed = condition.windSpeed.value;
const windDirection = utils.calc.directionToNSEW(condition.windDirection.value);
if (navigation.units() === UNITS.english) {
Temperature = utils.units.celsiusToFahrenheit(Temperature);
WindSpeed = utils.units.kphToMph(WindSpeed);
}
2020-09-25 20:11:19 +00:00
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 65, y, condition.city.substr(0, 14), 2);
2020-10-29 21:44:28 +00:00
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 345, y, LatestObservations.shortenCurrentConditions(condition.textDescription).substr(0, 9), 2);
2020-09-04 18:02:20 +00:00
if (WindSpeed > 0) {
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 495, y, windDirection + (Array(6 - windDirection.length - WindSpeed.toString().length).join(' ')) + WindSpeed.toString(), 2);
} else if (WindSpeed === 'NA') {
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 495, y, 'NA', 2);
} else {
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', 495, y, 'Calm', 2);
}
const x = (325 - (Temperature.toString().length * 15));
draw.text(this.context, 'Star4000', '24pt', '#FFFFFF', x, y, Temperature, 2);
y += 40;
});
this.finishDraw();
}
2020-10-29 21:44:28 +00:00
static shortenCurrentConditions(_condition) {
let condition = _condition;
2020-09-04 18:02:20 +00:00
condition = condition.replace(/Light/, 'L');
condition = condition.replace(/Heavy/, 'H');
condition = condition.replace(/Partly/, 'P');
condition = condition.replace(/Mostly/, 'M');
condition = condition.replace(/Few/, 'F');
condition = condition.replace(/Thunderstorm/, 'T\'storm');
condition = condition.replace(/ in /, '');
condition = condition.replace(/Vicinity/, '');
condition = condition.replace(/ and /, ' ');
condition = condition.replace(/Freezing Rain/, 'Frz Rn');
condition = condition.replace(/Freezing/, 'Frz');
condition = condition.replace(/Unknown Precip/, '');
condition = condition.replace(/L Snow Fog/, 'L Snw/Fog');
condition = condition.replace(/ with /, '/');
return condition;
}
2020-10-29 21:44:28 +00:00
}