-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbloader.js
152 lines (129 loc) · 5.35 KB
/
dbloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// -*- mode: javascript; indent-tabs-mode: nil; c-basic-offset: 8 -*-
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
//
// dbloader.js: load aircraft metadata from static json files
//
// Copyright (c) 2014,2015 Oliver Jowett <oliver@mutability.co.uk>
//
// MODIFIED BY IAN RENTON FOR UMID 1090
//
// This file is free software: you may copy, redistribute and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 2 of the License, or (at your
// option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
"use strict";
var _aircraft_cache = {};
var _aircraft_type_cache = null;
function getAircraftData(icao, server) {
var defer;
icao = icao.toUpperCase();
if (icao in _aircraft_cache) {
defer = _aircraft_cache[icao];
} else {
// load from blocks:
defer = _aircraft_cache[icao] = $.Deferred();
request_from_db(icao, 1, defer, server);
}
return defer;
}
function request_from_db(icao, level, defer, server) {
var bkey = icao.substring(0, level);
var dkey = icao.substring(level);
var req = db_ajax(bkey, server);
req.done(function(data) {
var subkey;
if (dkey in data) {
getIcaoAircraftTypeData(data[dkey], defer, server);
return;
}
if ("children" in data) {
subkey = bkey + dkey.substring(0,1);
if (data.children.indexOf(subkey) != -1) {
request_from_db(icao, level+1, defer, server);
return;
}
}
defer.reject();
});
req.fail(function(jqXHR,textStatus,errorThrown) {
defer.reject();
});
}
function getIcaoAircraftTypeData(aircraftData, defer, server) {
if (_aircraft_type_cache === null) {
$.getJSON(server + "db/aircraft_types/icao_aircraft_types.json")
.done(function(typeLookupData) {
_aircraft_type_cache = typeLookupData;
})
.always(function() {
lookupIcaoAircraftType(aircraftData, defer);
});
}
else {
lookupIcaoAircraftType(aircraftData, defer);
}
}
function lookupIcaoAircraftType(aircraftData, defer) {
if (_aircraft_type_cache !== null && "t" in aircraftData) {
var typeDesignator = aircraftData.t.toUpperCase();
if (typeDesignator in _aircraft_type_cache) {
var typeData = _aircraft_type_cache[typeDesignator];
if (typeData.desc != undefined && aircraftData.desc === undefined && typeData.desc != null && typeData.desc.length == 3) {
aircraftData.desc = typeData.desc;
}
if (typeData.wtc != undefined && aircraftData.wtc === undefined) {
aircraftData.wtc = typeData.wtc;
}
}
}
defer.resolve(aircraftData);
}
var _request_count = 0;
var _request_queue = [];
var _request_cache = {};
var MAX_REQUESTS = 2;
function db_ajax(bkey, server) {
var defer;
if (bkey in _request_cache) {
return _request_cache[bkey];
}
if (_request_count < MAX_REQUESTS) {
// just do ajax directly
++_request_count;
defer = _request_cache[bkey] = $.ajax({ url: server + 'db/' + bkey + '.json',
cache: true,
timeout: 5000,
dataType : 'json' });
defer.always(function() {db_ajax_request_complete(server); });
} else {
// put it in the queue
defer = _request_cache[bkey] = $.Deferred();
defer.bkey = bkey;
_request_queue.push(defer);
}
return defer;
}
function db_ajax_request_complete(server) {
var req;
var ajaxreq;
if (_request_queue.length == 0) {
--_request_count;
} else {
req = _request_queue.shift();
ajaxreq = $.ajax({ url: server + 'db/' + req.bkey + '.json',
cache: true,
timeout: 5000,
dataType : 'json' });
ajaxreq.done(function(data) { req.resolve(data); });
ajaxreq.fail(function(jqxhr, status, error) { req.reject(jqxhr, status, error); });
ajaxreq.always(function() {db_ajax_request_complete(server); });
}
}