From 93aedbe15c40d794f0a36d3947bab4a729f32232 Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Wed, 7 Jan 2015 18:43:08 +0000 Subject: [PATCH] Tell the user about AJAX errors. Set AJAX options: 5s timeout, no caching. Don't spool up a second request while the previous one is in progress. If we repeatedly see the same receiver timestamp, warn the user that the JSON isn't being updated. --- public_html/script.js | 34 +++++++++++++++++++++++++++++++++- public_html/style.css | 5 +++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/public_html/script.js b/public_html/script.js index a1769fb..0acc1ea 100644 --- a/public_html/script.js +++ b/public_html/script.js @@ -29,8 +29,22 @@ var TrackedHistorySize = 0; var SitePosition = null; var ReceiverClock = null; + +var LastReceiverTimestamp = null; +var StaleReceiverCount = 0; +var FetchPending = null; + function fetchData() { - $.getJSON('data/aircraft.json', function(data) { + if (FetchPending !== null && FetchPending.state() == 'pending') { + // don't double up on fetches, let the last one resolve + return; + } + + FetchPending = $.ajax({ url: 'data/aircraft.json', + timeout: 5000, + cache: false, + dataType: 'json' }); + FetchPending.done(function(data) { // Loop through all the planes in the data packet var now = data.now; var acs = data.aircraft; @@ -71,7 +85,25 @@ function fetchData() { var rcv = new Date(now * 1000); ReceiverClock.render(rcv.getUTCHours(),rcv.getUTCMinutes(),rcv.getUTCSeconds()); } + + // Check for stale receiver data + if (LastReceiverTimestamp === now) { + StaleReceiverCount++; + if (StaleReceiverCount > 5) { + $("#update_error_detail").text("The data from dump1090 hasn't been updated in a while. Maybe dump1090 is no longer running?"); + $("#update_error").css('display','block'); + } + } else { + StaleReceiverCount = 0; + LastReceiverTimestamp = now; + $("#update_error").css('display','none'); + } }); + + FetchPending.fail(function(jqxhr, status, error) { + $("#update_error_detail").text("AJAX call failed (" + status + (error ? (": " + error) : "") + "). Maybe dump1090 is no longer running?"); + $("#update_error").css('display','block'); + }); } function initialize() { diff --git a/public_html/style.css b/public_html/style.css index 602a97a..d25688b 100644 --- a/public_html/style.css +++ b/public_html/style.css @@ -11,8 +11,9 @@ div#SpecialSquawkWarning { position: absolute; bottom: 25px; right: 430px; borde background-color: #FFFFA3; opacity: 0.75; filter:alpha(opacity=75); padding: 5px; text-align: center; } -table#optionsTabs { width: 100%; font-size: small; font-family: monospace; background-color: #ddd; - border: 1px; border-color: #000000;} +div#update_error { position: absolute; bottom: 25px; left: 25px; border: 2px solid red; + background-color: #FFFFA3; opacity: 0.75; filter:alpha(opacity=75); padding: 5px; + text-align: center; } #tableinfo, #sudo_buttons { font-size: x-small; font-family: monospace; }