pyAirwaves is an ADS-B and AIS processing, storage, and display application with Leaflets integration.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB

defmodule Mix.Tasks.Pyairwaves.UpdateAircrafts do
use Mix.Task
import Mix.Tasks.Pyairwaves
require Logger
@moduledoc """
Update the list of known aircrafts models
Cron recommanded: run one time per month
# FIXME TODO; The initial list contains multiple ModelFullName per Designator
# We need to rebuild a new list, with unique Designator and a ModelFullName concatennating all the available ones
defp parse_aircraft(aircraft) do
aircraft_shadow =
"generic_#{String.slice(aircraft["EngineType"], 0, 1)}#{aircraft["EngineCount"]}#{
engine_count =
case aircraft["Enginecount"] do
"C" -> 1
nil -> 1
_ -> String.to_integer(aircraft["Enginecount"])
icao: aircraft["Designator"],
type: aircraft["ModelFullName"],
manufacturer: aircraft["ManufacturerCode"],
aircraft_description: aircraft["AircraftDescription"],
aircraft_shadow: aircraft_shadow,
engine_type: aircraft["EngineType"],
engine_count: engine_count,
wake_category: aircraft["WTC"]
# The initial dataset contains one entry per ModelFullName, we want only one per ICAO ID
# We should dedupe the list and concat the ModelFullName per ICAO ID
defp dedupe_list(from, to) when length(from) > 0 do
[item | tail] = from
type =
if Map.has_key?(to, item["Designator"]) do
Map.get(to, item["Designator"])["ModelFullName"] <> ", " <> item["ModelFullName"]
new_entry = item |> Map.put("ModelFullName", type)
new_to = Map.put(to, item["Designator"], new_entry)
dedupe_list(tail, new_to)
defp dedupe_list(from, to) when length(from) == 0 do
@shortdoc "Update the list of known aircrafts models"
def run(_) do
# start the required apps & repos
start_apps()"Starting aircrafts update. (Online)")
url = ""
headers = [
"X-Requested-With": "XMLHttpRequest",
Accept: "application/json, text/javascript, */*; q=0.01",
Host: "",
Origin: "",
"Content-Length": "0",
Referer: ""
options = []
Pyairwaves.Repo.delete_all(Pyairwaves.Aircraft)"Table cleaned.")
{:ok, aircrafts} =!(url, "", headers, options).body
|> Jason.decode()
aircrafts = dedupe_list(aircrafts, %{}), fn {_icao, ac} -> parse_aircraft(ac) end)
# Enum.each(aircrafts, fn {})
|> Enum.chunk_every(1000)
|> chunk ->
Pyairwaves.Repo.insert_all(Pyairwaves.Aircraft, chunk, on_conflict: :nothing, log: false)
end)"Update finished.")