ws4kp/server/scripts/twc3.js

557 lines
14 KiB
JavaScript
Raw Normal View History

2020-09-04 18:02:20 +00:00
/* globals _StationInfo, luxon, _RegionalCities, utils, icons, _TravelCities, SunCalc */
const _DayShortNames = { 'Sunday': 'Sun', 'Monday': 'Mon', 'Tuesday': 'Tue', 'Wednesday': 'Wed', 'Thursday': 'Thu', 'Friday': 'Fri', 'Saturday': 'Sat' };
var canvasRegionalObservations;
var canvasRegionalForecast1;
var canvasRegionalForecast2;
var canvasLocalRadar;
var canvasCurrentWeather;
var canvasExtendedForecast1;
var canvasExtendedForecast2;
var canvasLocalForecast;
var divHazards;
var divHazardsScroll;
var canvasHazards;
var canvasAlmanac;
var canvasAlmanacTides;
var canvasOutlook;
var canvasMarineForecast;
var canvasAirQuality;
var canvasTravelForecast;
var divOutlookTemp;
var cnvOutlookTemp;
var divOutlookPrcp;
var cnvOutlookPrcp;
var canvasLatestObservations;
var _WeatherParameters = null;
var _UpdateWeatherUpdateMs = 50;
var canvasBackGroundDateTime = null;
var canvasBackGroundCurrentConditions = null;
var _UpdateWeatherCurrentConditionType = CurrentConditionTypes.Title;
var _UpdateWeatherCurrentConditionCounterMs = 0;
var _UpdateCustomScrollTextMs = 0;
var _UpdateHazardsY = 0;
const GetMonthPrecipitation = async (WeatherParameters) => {
const DateTime = luxon.DateTime;
const today = DateTime.local().startOf('day').toISO().replace('.000','');
try {
const cliProducts = await $.ajaxCORS({
type: 'GET',
url: 'https://api.weather.gov/products',
data: {
location: WeatherParameters.WeatherOffice,
type: 'CLI',
start: today,
},
dataType: 'json',
crossDomain: true,
});
// get the first url from the list
const cli = await $.ajaxCORS({
type: 'GET',
url: cliProducts['@graph'][0]['@id'],
dataType: 'json',
crossDomain: true,
});
WeatherParameters.WeatherMonthlyTotals = WeatherMonthlyTotalsParser(cli.productText);
console.log(WeatherParameters.WeatherMonthlyTotals);
} catch (e) {
console.error('GetMonthPrecipitation failed');
2020-09-23 16:49:15 +00:00
console.error(e.status, e.responseJSON);
2020-09-04 18:02:20 +00:00
return false;
}
};
2020-09-08 03:06:44 +00:00
Date.prototype.stdTimezoneOffset = function () {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
};
Date.prototype.dst = function () {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
};
var GetWeatherHazards3 = function (WeatherParameters) {
var ZoneId = WeatherParameters.ZoneId;
var HazardUrls = [];
var HazardCounter = 0;
WeatherParameters.WeatherHazardConditions =
{
ZoneId: WeatherParameters.ZoneId,
Hazards: [],
};
var Url = 'https://alerts.weather.gov/cap/wwaatmget.php?x=' + ZoneId + '&y=0';
2020-09-04 18:02:20 +00:00
// Load the xml file using ajax
$.ajaxCORS({
type: 'GET',
url: Url,
2020-09-08 03:06:44 +00:00
dataType: 'text',
2020-09-04 18:02:20 +00:00
crossDomain: true,
cache: false,
2020-09-08 03:06:44 +00:00
success: function (text) {
// IE doesn't support XML tags with colons.
text = text.replaceAll('<cap:', '<cap_');
text = text.replaceAll('</cap:', '</cap_');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var $xml = $(text);
//console.log(xml);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
$xml.find('entry').each(function () {
var entry = $(this);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
// Skip non-alerts.
var cap_msgType = entry.find('cap_msgType');
if (cap_msgType.text() !== 'Alert') {
return true;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var link = entry.find('link');
var Url = link.attr('href');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
HazardUrls.push(Url);
});
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (HazardUrls.length === 0) {
PopulateHazardConditions(WeatherParameters);
console.log(WeatherParameters.WeatherHazardConditions);
return;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
$(HazardUrls).each(function () {
var Url = this.toString();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
$.ajaxCORS({
type: 'GET',
url: Url,
dataType: 'xml',
crossDomain: true,
cache: true,
success: function (xml) {
var $xml = $(xml);
console.log(xml);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var description = $xml.find('description');
WeatherParameters.WeatherHazardConditions.Hazards.push(description.text());
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
HazardCounter++;
if (HazardCounter === HazardUrls.length) {
PopulateHazardConditions(WeatherParameters);
console.log(WeatherParameters.WeatherHazardConditions);
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
},
error: function () {
console.error('GetWeatherHazards3 failed for Url: ' + Url);
WeatherParameters.Progress.Hazards = LoadStatuses.Failed;
},
});
2020-09-04 18:02:20 +00:00
});
},
error: function (xhr, error, errorThrown) {
2020-09-08 03:06:44 +00:00
console.error('GetWeatherHazards3 failed: ' + errorThrown);
WeatherParameters.Progress.Hazards = LoadStatuses.Failed;
2020-09-04 18:02:20 +00:00
},
});
};
2020-09-08 03:06:44 +00:00
$(() => {
canvasBackGroundDateTime = $('#canvasBackGroundDateTime');
canvasBackGroundCurrentConditions = $('#canvasBackGroundCurrentConditions');
canvasProgress = $('#canvasProgress');
divProgress = $('#divProgress');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasLocalRadar = $('#canvasLocalRadar');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasRegionalForecast1 = $('#canvasRegionalForecast1');
canvasRegionalForecast2 = $('#canvasRegionalForecast2');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasRegionalObservations = $('#canvasRegionalObservations');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasCurrentWeather = $('#canvasCurrentWeather');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasExtendedForecast1 = $('#canvasExtendedForecast1');
canvasExtendedForecast2 = $('#canvasExtendedForecast2');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasLocalForecast = $('#canvasLocalForecast');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
divHazards = $('#divHazards');
divHazardsScroll = $('#divHazardsScroll');
canvasHazards = $('#canvasHazards');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasAlmanac = $('#canvasAlmanac');
canvasAlmanacTides = $('#canvasAlmanacTides');
canvasOutlook = $('#canvasOutlook');
canvasMarineForecast = $('#canvasMarineForecast');
canvasAirQuality = $('#canvasAirQuality');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
divOutlookTemp = $('#divOutlookTemp');
divOutlookPrcp = $('#divOutlookPrcp');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasTravelForecast = $('#canvasTravelForecast');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasLatestObservations = $('#canvasLatestObservations');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
canvasProgress.mousemove(canvasProgress_mousemove);
canvasProgress.click(canvasProgress_click);
2020-09-04 18:02:20 +00:00
_WeatherParameters = {};
_WeatherParameters.WeatherHazardConditions = {};
const WeatherCanvases = [];
WeatherCanvases.push(canvasProgress);
WeatherCanvases.push(canvasCurrentWeather);
WeatherCanvases.push(canvasLatestObservations);
WeatherCanvases.push(canvasTravelForecast);
WeatherCanvases.push(canvasRegionalForecast1);
WeatherCanvases.push(canvasRegionalForecast2);
WeatherCanvases.push(canvasRegionalObservations);
WeatherCanvases.push(canvasAlmanac);
WeatherCanvases.push(canvasAlmanacTides);
WeatherCanvases.push(canvasOutlook);
WeatherCanvases.push(canvasMarineForecast);
WeatherCanvases.push(canvasAirQuality);
WeatherCanvases.push(canvasLocalForecast);
WeatherCanvases.push(canvasExtendedForecast1);
WeatherCanvases.push(canvasExtendedForecast2);
WeatherCanvases.push(canvasHazards);
WeatherCanvases.push(canvasLocalRadar);
_WeatherParameters.WeatherCanvases = WeatherCanvases;
$(WeatherCanvases).each(function () {
var WeatherCanvas = $(this);
WeatherCanvas.css('position', 'absolute');
WeatherCanvas.css('top', '0px');
WeatherCanvas.css('left', '0px');
WeatherCanvas.hide();
});
canvasProgress.show();
_WeatherParameters.TravelCities = _TravelCities;
_WeatherParameters.Progress = new Progress({
WeatherParameters: _WeatherParameters,
});
});
var NavigateMenu = function () {
if (_IsPlaying) {
NavigatePlayToggle();
}
Navigate(0);
_PlayMs = 0;
};
2020-09-08 03:06:44 +00:00
$.fn.scrollIntoView = function () {
const $self = this;
const OkToFadeOut = true;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
_WeatherParameters.WeatherCanvases.forEach($WeatherCanvas => {
2020-09-04 18:02:20 +00:00
if (!$WeatherCanvas.is($self)) {
if ($WeatherCanvas.is(':visible')) {
$WeatherCanvas.css('z-index', '9999');
if (!OkToFadeOut) {
$WeatherCanvas.hide();
} else {
$WeatherCanvas.fadeOut({
progress: () => {
UpdateWeatherCanvas(_WeatherParameters, $WeatherCanvas);
UpdateWeatherCanvas(_WeatherParameters, $self);
},
});
}
}
} else {
if (!$WeatherCanvas.is(':visible')) {
$WeatherCanvas.css('z-index', '');
$WeatherCanvas.show();
}
$WeatherCanvas.stop();
$WeatherCanvas.css('opacity', '');
UpdateWeatherCanvas(_WeatherParameters, $WeatherCanvas);
}
});
_RefreshGifs = true;
window.setTimeout(function () { _RefreshGifs = false; }, 200);
};
var canvasProgress_mousemove = function (e) {
canvasProgress.css('cursor', '');
var RatioX = canvasProgress.width() / 640;
var RatioY = canvasProgress.height() / 480;
if (e.offsetX >= (70 * RatioX) && e.offsetX <= (565 * RatioX)) {
//if (e.offsetY >= (105 * RatioY) && e.offsetY <= (350 * RatioY))
if (e.offsetY >= (100 * RatioY) && e.offsetY <= (385 * RatioY)) {
// Show hand cursor.
canvasProgress.css('cursor', 'pointer');
}
}
};
2020-09-08 03:06:44 +00:00
var PopulateHazardConditions = function (WeatherParameters) {
if (WeatherParameters === null || (_DontLoadGifs && WeatherParameters.Progress.Hazards !== LoadStatuses.Loaded)) {
return;
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
var WeatherHazardConditions = WeatherParameters.WeatherHazardConditions;
var ZoneId = WeatherHazardConditions.ZoneId;
var Text;
var Line;
var SkipLine;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var DontLoadGifs = _DontLoadGifs;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
divHazards.empty();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
$(WeatherHazardConditions.Hazards).each(function () {
//Text = this.replaceAll("\n", "<br/>");
//divHazards.html(divHazards.html() + "<br/><br/>" + Text);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
Text = this.toString();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
SkipLine = false;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
Text = Text.replaceAll('\n', ' ');
//Text = Text.replaceAll("*** ", "");
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
//$(Text.split("\n")).each(function ()
$(Text.split(' ')).each(function () {
Line = this.toString();
Line = Line.toUpperCase();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (Line.startsWith('&&')) {
return false;
} else if (Line.startsWith('$$')) {
return false;
}
if (SkipLine) {
if (Line === '') {
SkipLine = false;
return true;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
return true;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (Line.startsWith(ZoneId)) {
SkipLine = true;
return true;
} else if (Line.indexOf('>') !== -1) {
SkipLine = true;
return true;
} else if (Line.indexOf('LAT...LON ') !== -1) {
SkipLine = true;
return true;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
//divHazards.html(divHazards.html() + "<br/>" + Line);
if (Line.indexOf('.') === 0 || Line.indexOf('*') === 0) {
divHazards.html(divHazards.html() + '<br/><br/>');
if (Line.indexOf('.') === 0 && Line.indexOf('...') !== 0) {
Line = Line.substr(1);
}
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
divHazards.html(divHazards.html() + Line + ' ');
2020-09-04 18:02:20 +00:00
});
2020-09-08 03:06:44 +00:00
divHazards.html(divHazards.html() + '<br/><br/>');
});
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var DrawHazards = function () {
// Draw canvas
var canvas = canvasHazards[0];
var context = canvas.getContext('2d');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var BackGroundImage = new Image();
BackGroundImage.onload = function () {
context.drawImage(BackGroundImage, 0, 0);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (DontLoadGifs) {
UpdateHazards();
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (WeatherHazardConditions.Hazards.length > 0) {
WeatherParameters.Progress.Hazards = LoadStatuses.Loaded;
} else {
WeatherParameters.Progress.Hazards = LoadStatuses.NoData;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
UpdateWeatherCanvas(WeatherParameters, canvasHazards);
2020-09-04 18:02:20 +00:00
};
2020-09-08 03:06:44 +00:00
BackGroundImage.src = 'images/BackGround7.png';
};
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var HazardsText = divHazards.html();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
HazardsText = HazardsText.replaceAll('<br>', '\n');
HazardsText = HazardsText.replaceAll('<br/>', '\n');
HazardsText = HazardsText.replaceAll('<br />', '\n');
HazardsText = HazardsText.replaceAll('<br></br>', '\n');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
WeatherHazardConditions.HazardsText = HazardsText;
WeatherHazardConditions.HazardsTextC = ConvertConditionsToMetric(HazardsText);
if (_Units === Units.Metric) {
HazardsText = WeatherHazardConditions.HazardsTextC;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var HazardsWrapped = HazardsText.wordWrap(32);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var cnvHazardsScroll;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var ShowHazardsScroll = function () {
var cnvHazardsScrollId;
var context;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
cnvHazardsScrollId = 'cnvHazardsScroll';
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
var HazardsWrappedLines = HazardsWrapped.split('\n');
var MaxHazardsWrappedLines = 365;
if (_OperatingSystem === OperatingSystems.Andriod) {
MaxHazardsWrappedLines = 92;
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
if (HazardsWrappedLines.length > MaxHazardsWrappedLines) {
HazardsWrappedLines = HazardsWrappedLines.splice(0, MaxHazardsWrappedLines - 1);
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
var height = 0 + (HazardsWrappedLines.length * 45);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (DontLoadGifs === false) {
// Clear the current image.
divHazardsScroll.empty();
divHazardsScroll.html('<canvas id=\'' + cnvHazardsScrollId + '\' />');
cnvHazardsScroll = $('#' + cnvHazardsScrollId);
cnvHazardsScroll.attr('width', '640'); // For Chrome.
cnvHazardsScroll.attr('height', height); // For Chrome.
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
cnvHazardsScroll = $('#' + cnvHazardsScrollId);
context = cnvHazardsScroll[0].getContext('2d');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
DrawBox(context, 'rgb(112, 35, 35)', 0, 0, 640, height);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
//var y = 0;
var y = 45;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
$(HazardsWrappedLines).each(function () {
var HazardLine = this.toString();
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
DrawText(context, 'Star4000', '24pt', '#FFFFFF', 80, y, HazardLine, 1);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
y += 45;
});
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
DrawHazards();
2020-09-04 18:02:20 +00:00
};
2020-09-08 03:06:44 +00:00
ShowHazardsScroll();
2020-09-04 18:02:20 +00:00
};
2020-09-08 03:06:44 +00:00
var UpdateHazards = function (Offset) {
var canvas = canvasHazards[0];
var context = canvas.getContext('2d');
var cnvHazardsScroll = $('#cnvHazardsScroll');
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
switch (Offset) {
case undefined:
2020-09-04 18:02:20 +00:00
break;
2020-09-08 03:06:44 +00:00
case 0:
_UpdateHazardsY = 0;
2020-09-04 18:02:20 +00:00
break;
2020-09-08 03:06:44 +00:00
case Infinity:
_UpdateHazardsY = cnvHazardsScroll.height();
break;
default:
_UpdateHazardsY += (385 * Offset);
if (_UpdateHazardsY > cnvHazardsScroll.height()) {
_UpdateHazardsY = cnvHazardsScroll.height();
} else if (_UpdateHazardsY < 0) {
_UpdateHazardsY = 0;
}
break;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
DrawBox(context, 'rgb(112, 35,35)', 0, 0, 640, 385);
context.drawImage(cnvHazardsScroll[0], 0, _UpdateHazardsY, 640, 385, 0, 0, 640, 385);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
};
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
const WeatherMonthlyTotalsParser = (text) => {
return +text.match(/MONTH TO DATE\s*(\d{1,2}\.\d\d)/)[1];
};
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
const Progress = function (e) {
const DrawCustomScrollText = (WeatherParameters, context) => {
const font = 'Star4000';
const size = '24pt';
const color = '#ffffff';
const shadow = 2;
let x = 640;
const y = 430;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
if (WeatherParameters.Progress.GetTotalPercentage() !== 100) {
return;
2020-09-04 18:02:20 +00:00
}
2020-09-08 03:06:44 +00:00
// Clear the date and time area.
context.drawImage(canvasBackGroundCurrentConditions[0], 0, 0, 640, 75, 0, 405, 640, 75);
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
const text = _ScrollText;
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
x = 640 - ((_UpdateCustomScrollTextMs / _UpdateWeatherUpdateMs) * 5);
// Wait an extra 5 characters.
if (x < ((text.length + 10) * 15 * -1)) {
_UpdateCustomScrollTextMs = 0;
x = 640;
}
2020-09-04 18:02:20 +00:00
2020-09-08 03:06:44 +00:00
// Draw the current condition.
DrawText(context, font, size, color, x, y, text, shadow);
2020-09-04 18:02:20 +00:00
};
const AssignScrollText = (e) => {
_ScrollText = e.ScrollText;
_UpdateCustomScrollTextMs = 0;
_UpdateWeatherCurrentConditionType = CurrentConditionTypes.Title;
_UpdateWeatherCurrentConditionCounterMs = 0;
};