| 1 |
// -*- mode: javascript; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
| 2 |
|
| 3 |
// Part of dump1090, a Mode S message decoder for RTLSDR devices. |
| 4 |
// |
| 5 |
// dbloader.js: load aircraft metadata from static json files |
| 6 |
// |
| 7 |
// Copyright (c) 2014,2015 Oliver Jowett <[email protected]> |
| 8 |
// |
| 9 |
// This file is free software: you may copy, redistribute and/or modify it |
| 10 |
// under the terms of the GNU General Public License as published by the |
| 11 |
// Free Software Foundation, either version 2 of the License, or (at your |
| 12 |
// option) any later version. |
| 13 |
// |
| 14 |
// This file is distributed in the hope that it will be useful, but |
| 15 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
// General Public License for more details. |
| 18 |
// |
| 19 |
// You should have received a copy of the GNU General Public License |
| 20 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
"use strict"; |
| 23 |
|
| 24 |
var _aircraft_cache = {}; |
| 25 |
|
| 26 |
function getAircraftData(icao) { |
| 27 |
var defer; |
| 28 |
|
| 29 |
icao = icao.toUpperCase(); |
| 30 |
|
| 31 |
if (icao in _aircraft_cache) { |
| 32 |
defer = _aircraft_cache[icao]; |
| 33 |
} else { |
| 34 |
// load from blocks: |
| 35 |
defer = _aircraft_cache[icao] = $.Deferred(); |
| 36 |
request_from_db(icao, 1, defer); |
| 37 |
} |
| 38 |
|
| 39 |
return defer; |
| 40 |
} |
| 41 |
|
| 42 |
function request_from_db(icao, level, defer) { |
| 43 |
var bkey = icao.substring(0, level); |
| 44 |
var dkey = icao.substring(level); |
| 45 |
var req = db_ajax(bkey); |
| 46 |
|
| 47 |
req.done(function(data) { |
| 48 |
var subkey; |
| 49 |
|
| 50 |
if (dkey in data) { |
| 51 |
defer.resolve(data[dkey]); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ("children" in data) { |
| 56 |
subkey = bkey + dkey.substring(0,1); |
| 57 |
if (data.children.indexOf(subkey) != -1) { |
| 58 |
request_from_db(icao, level+1, defer); |
| 59 |
return; |
| 60 |
} |
| 61 |
} |
| 62 |
defer.reject(); |
| 63 |
}); |
| 64 |
|
| 65 |
req.fail(function(jqXHR,textStatus,errorThrown) { |
| 66 |
defer.reject(); |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
var _request_count = 0; |
| 71 |
var _request_queue = []; |
| 72 |
var _request_cache = {}; |
| 73 |
|
| 74 |
var MAX_REQUESTS = 2; |
| 75 |
|
| 76 |
function db_ajax(bkey) { |
| 77 |
var defer; |
| 78 |
|
| 79 |
if (bkey in _request_cache) { |
| 80 |
return _request_cache[bkey]; |
| 81 |
} |
| 82 |
|
| 83 |
if (_request_count < MAX_REQUESTS) { |
| 84 |
// just do ajax directly |
| 85 |
++_request_count; |
| 86 |
defer = _request_cache[bkey] = $.ajax({ url: 'db/' + bkey + '.json', |
| 87 |
cache: true, |
| 88 |
timeout: 5000, |
| 89 |
dataType : 'json' }); |
| 90 |
defer.always(db_ajax_request_complete); |
| 91 |
} else { |
| 92 |
// put it in the queue |
| 93 |
defer = _request_cache[bkey] = $.Deferred(); |
| 94 |
defer.bkey = bkey; |
| 95 |
_request_queue.push(defer); |
| 96 |
} |
| 97 |
|
| 98 |
return defer; |
| 99 |
} |
| 100 |
|
| 101 |
function db_ajax_request_complete() { |
| 102 |
var req; |
| 103 |
var ajaxreq; |
| 104 |
|
| 105 |
if (_request_queue.length == 0) { |
| 106 |
--_request_count; |
| 107 |
} else { |
| 108 |
req = _request_queue.shift(); |
| 109 |
ajaxreq = $.ajax({ url: 'db/' + req.bkey + '.json', |
| 110 |
cache: true, |
| 111 |
timeout: 5000, |
| 112 |
dataType : 'json' }); |
| 113 |
ajaxreq.done(function(data) { req.resolve(data); }); |
| 114 |
ajaxreq.fail(function(jqxhr, status, error) { req.reject(jqxhr, status, error); }); |
| 115 |
ajaxreq.always(db_ajax_request_complete); |
| 116 |
} |
| 117 |
} |