forked from wiedehopf/tar1090
-
Notifications
You must be signed in to change notification settings - Fork 2
handle IATA callsigns from ADS-C #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+169
−11
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e8186d3
added airlines.js to be updated from opentravelmap.
jaluebbe 37001a0
replace IATA callsigns by ICAO callsigns in map, list and for route l…
jaluebbe d13782f
Update airlines.js
jaluebbe 6282d84
Update airlines.js
jaluebbe 7a460e6
Update airlines.js
jaluebbe b6a34c2
Update planeObject.js
jaluebbe 50ed0d9
Keep cargo-only airline mappings, guard against missing airlines.js
jaluebbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // airlines.js | ||
| // Loads the IATA -> ICAO callsign prefix table from OpenTravelData at | ||
| // runtime and caches it in localStorage, so it's fetched at most once | ||
| // per AIRLINE_CACHE_MAX_AGE_MS instead of on every page load. | ||
| "use strict"; | ||
|
|
||
| const AIRLINE_CSV_URL = "https://raw.githubusercontent.com/opentraveldata/opentraveldata/master/opentraveldata/optd_airline_best_known_so_far.csv"; | ||
| const AIRLINE_CACHE_KEY = "tar1090_iata_to_icao_v1"; | ||
| const AIRLINE_CACHE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 1 week | ||
|
|
||
| let iata_to_icao = {}; | ||
|
|
||
| function parseAirlineCsv(text) { | ||
| // Columns: pk^env_id^validity_from^validity_to^3char_code^2char_code^...^type^... | ||
| // Only currently valid (validity_to empty), non-cargo-only (type != "C") | ||
| // entries are kept, to avoid ambiguity between passenger and cargo | ||
| // airlines sharing an IATA code (see the Lufthansa special case in | ||
| // iataToIcao() in planeObject.js). | ||
| const map = {}; | ||
| const lines = text.split('\n'); | ||
| for (let i = 1; i < lines.length; i++) { | ||
| const f = lines[i].split('^'); | ||
| if (f.length < 12) { | ||
| continue; | ||
| } | ||
| const validityTo = (f[3] || '').trim(); | ||
| const icao3 = (f[4] || '').trim(); | ||
| const iata2 = (f[5] || '').trim(); | ||
| const type = (f[11] || '').trim(); | ||
| if (!/^[A-Z0-9]{2}$/.test(iata2) || !/^[A-Z]{3}$/.test(icao3) || validityTo || type === 'C') { | ||
| continue; | ||
| } | ||
| map[iata2] = icao3; | ||
| } | ||
|
jaluebbe marked this conversation as resolved.
|
||
| return map; | ||
| } | ||
|
|
||
| // Re-run setFlight() on already-tracked aircraft so their callsigns pick | ||
| // up the table once it's loaded (planes seen before that point were left | ||
| // with their raw, unconverted callsign). | ||
| function refreshTrackedCallsigns() { | ||
| if (typeof g === 'undefined' || !g.planes) { | ||
| return; | ||
| } | ||
| for (const hex in g.planes) { | ||
| const plane = g.planes[hex]; | ||
| if (plane.flight) { | ||
| const oldTs = plane.flightTs; | ||
| plane.setFlight(plane.flight); | ||
| plane.flightTs = oldTs; | ||
| } | ||
| } | ||
|
jaluebbe marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function loadAirlineTable() { | ||
| try { | ||
| const cached = JSON.parse(localStorage.getItem(AIRLINE_CACHE_KEY)); | ||
| if (cached && typeof cached.ts === 'number' && cached.data && typeof cached.data === 'object' | ||
| && Date.now() - cached.ts < AIRLINE_CACHE_MAX_AGE_MS) { | ||
|
jaluebbe marked this conversation as resolved.
|
||
| iata_to_icao = cached.data; | ||
| refreshTrackedCallsigns(); | ||
| return; | ||
| } | ||
|
Copilot marked this conversation as resolved.
Copilot marked this conversation as resolved.
|
||
| } catch (e) { | ||
| // ignore missing/corrupt cache, fall through to fetch | ||
| } | ||
|
|
||
| fetch(AIRLINE_CSV_URL) | ||
| .then(res => { | ||
| if (!res.ok) { | ||
| throw new Error(`HTTP ${res.status} ${res.statusText}`); | ||
| } | ||
| return res.text(); | ||
| }) | ||
| .then(text => { | ||
|
Copilot marked this conversation as resolved.
|
||
| iata_to_icao = parseAirlineCsv(text); | ||
| refreshTrackedCallsigns(); | ||
| try { | ||
| localStorage.setItem(AIRLINE_CACHE_KEY, JSON.stringify({ ts: Date.now(), data: iata_to_icao })); | ||
| } catch (e) { | ||
| // localStorage full or unavailable, not fatal | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| console.error("airlines.js: failed to load IATA->ICAO table", err); | ||
| }); | ||
| } | ||
|
|
||
| loadAirlineTable(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.