// How often to query the server? const REFRESH_INTERVAL_SEC = 60; // Storage for the spot data that the server gives us. var spots = [] // Storage for the options that the server gives us. This will define our filters. var options = {}; // Last time we updated the spots list on display. var lastUpdateTime; // Load spots and populate the table. function loadSpots() { $.getJSON('/api/spots', function(jsonData) { // Store last updated time lastUpdateTime = moment.utc(); updateRefreshDisplay(); // Store data spots = jsonData; // Update table updateTable(); }); } // Update the spots table function updateTable() { // Populate table with headers let headers = Object.keys(spots[0]); let table = $('').append(''); ["UTC", "DX", "Frequency", "Mode", "Comment", "Source", "Ref.", "DE"].forEach(header => table.find('thead tr').append(``)); spots.forEach(s => { // Create row let $tr = $(''); // Show in red if QRT if (s["qrt"] == true) { $tr.addClass("table-faded"); } // Format a UTC time for display var time = moment.utc(s["time"], moment.ISO_8601); var time_formatted = time.format("HH:mm") // Format the frequency var mhz = Math.floor(s["freq"] / 1000.0); var khz = Math.floor(s["freq"] - (mhz * 1000.0)); var hz = Math.floor((s["freq"] - Math.floor(s["freq"])) * 1000.0); var hz_string = (hz > 0) ? hz.toFixed(0) : ""; var freq_string = `${mhz.toFixed(0)}${khz.toFixed(0).padStart(3, '0')}${hz_string}` // Format sig_refs var sig_refs = "" if (s["sig_refs"]) { sig_refs = s["sig_refs"].join(", ") } // Format DE flag var de_flag = ""; if (s["de_flag"] && s["de_flag"] != "") { de_flag = s["de_flag"]; } // Populate the row $tr.append(``); $tr.append(``); $tr.append(``); $tr.append(``); $tr.append(''); $tr.append(``); $tr.append(``); $tr.append(``); table.find('tbody').append($tr); }); // Update DOM $('#table-container').html(table); } // Load server status function loadStatus() { $.getJSON('/api/status', function(jsonData) { $('#status-container').html(jsonData); // todo implement }); } // Load server options. Once a successful callback is made from this, we then query spots and set up the timer to query // spots repeatedly. function loadOptions() { $.getJSON('/api/options', function(jsonData) { options = jsonData; console.log(options); //todo remove loadSpots(); setInterval(loadSpots, REFRESH_INTERVAL_SEC * 1000) }); } // Update the refresh timing display function updateRefreshDisplay() { if (lastUpdateTime != null) { let count = REFRESH_INTERVAL_SEC; let secSinceUpdate = moment.duration(moment().diff(lastUpdateTime)).asSeconds(); updatingString = "Updating..." if (secSinceUpdate < REFRESH_INTERVAL_SEC) { count = REFRESH_INTERVAL_SEC - secSinceUpdate; updatingString = "Updating in " + count.toFixed(0) + " seconds..."; } $("#timing-container").text("Last updated at " + lastUpdateTime.format('HH:mm') + " UTC. " + updatingString); } } // Utility function to escape HTML characters from a string. function escapeHtml(str) { if (typeof str !== 'string') { return ''; } const escapeCharacter = (match) => { switch (match) { case '&': return '&'; case '<': return '<'; case '>': return '>'; case '"': return '"'; case '\'': return '''; case '`': return '`'; default: return match; } }; return str.replace(/[&<>"'`]/g, escapeCharacter); } // Startup. Call loadOptions(), this will then trigger loading spots and setting up timers. loadOptions(); // Update the refresh timing display every second setInterval(updateRefreshDisplay, 1000);
${header}
${time_formatted}${s["dx_flag"]}${s["dx_call"]}${freq_string}${s["mode"]}' + escapeHtml(`${s["comment"]}`) + ' ${s["source"]}${sig_refs}${s["de_flag"]}${s["de_call"]}