From 6e945c7341baf7a47a35cb36643217e2989c1cba Mon Sep 17 00:00:00 2001 From: Rajan Patel Date: Sat, 24 Feb 2024 16:10:40 +0000 Subject: [PATCH] log to file, fix some errors --- README.md | 4 ++++ rootfs/scripts/acars2pos.py | 29 ++++++++++++++++++++++---- rootfs/scripts/acars_decode/Decoder.py | 15 ++++++++++++- rootfs/scripts/util.py | 7 +++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8ffdffe..29aa5a4 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Under active development, everything is subject to change without notice. |----------|-------------|---------| | `JSON_IN` | Semi-colon separated list of `host:port` entries to connect to for JSON ingest. | acars_router:15550 | | `SBS_OUT` | Semi-colon separated list of `host:port` entries to connect to for SBS/Basestation output. | ultrafeeder:12000 | +| `LOG_FILE` | Set to any value to message text, type, SBS output, and adsbexchange link to a file in `/log`. | Unset | ## Docker Compose @@ -27,4 +28,7 @@ services: environment: - JSON_IN=planeslxc:15550;planeslxc:15555;planeslxc:15556 - SBS_OUT=adsbpc:12002;adsbpc:12004 + - LOG_FILE=true + volumes: + - ./logs:/log ``` diff --git a/rootfs/scripts/acars2pos.py b/rootfs/scripts/acars2pos.py index de1a607..1fae814 100755 --- a/rootfs/scripts/acars2pos.py +++ b/rootfs/scripts/acars2pos.py @@ -1,4 +1,5 @@ import locale +import os import socket import traceback from datetime import datetime, timezone @@ -81,6 +82,11 @@ def thread_wrapper(func, *args): txqs.append(SimpleQueue()) Thread(name=f"tx {s[0]}:{s[1]}", target=thread_wrapper, args=(tx_thread, s, txqs[-1])).start() +if not os.path.exists("/log"): + os.makedirs("/log") +logfile = open(f"/log/{datetime.now(timezone.utc):%Y_%m_%d}.log", "a", 1) +logfileh1 = open(f"/log/{datetime.now(timezone.utc):%Y_%m_%d}.h1.log", "a", 1) + while True: try: raw = rxq.get() @@ -177,12 +183,15 @@ def thread_wrapper(func, *args): print(f'{sbs["type"]} {sbs.get("msgtype")}', file=stderr) # print(pos, file=stderr) + if not sbs.get("reg"): + sbs["reg"] = icao2reg(sbs.get("icao", "")) + sbs["reg"] = sub(r'[^a-zA-Z0-9-]', '', sbs["reg"]).upper() if not sbs.get("icao"): - sbs["icao"] = reg2icao(sbs["reg"]) + sbs["icao"] = reg2icao(sbs.get("reg", "")) if not sbs.get("icao"): - print(f'{Fore.GREEN}xxxxxxx {sbs["reg"]}\t{sbs["icao"]}{Fore.RESET}', file=stderr) + print(f'{Fore.GREEN}xxxxxxx {sbs["reg"]}{Fore.RESET}', file=stderr) continue if sbs["type"] == "acars": @@ -194,10 +203,23 @@ def thread_wrapper(func, *args): else: squawk = "0000" - out = f'MSG,3,1,1,{sbs["icao"].upper()},1,{datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y/%m/%d,%T},{datetime.now(timezone.utc):%Y/%m/%d,%T},{sbs["flight"]},,,,{lat},{lon},,{squawk},,,,' + out = f'MSG,3,1,1,{sbs["icao"].upper()},1,{datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y/%m/%d,%T},{datetime.now(timezone.utc):%Y/%m/%d,%T},{sbs.get("flight", "")},,,,{lat},{lon},,{squawk},,,,' print(f'https://globe.adsbexchange.com/?icao={sbs["icao"]}&showTrace={datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y-%m-%d}×tamp={sbs["time"]}') print(f'{Fore.BLUE}{out}{Fore.RESET}\n', file=stderr) + + if getenv("LOG_FILE"): + if sbs.get("msgtype") == "H1": + logfileh1.write(f'{sbs["txt"]}\n') + logfileh1.write(f'{sbs["type"]} {sbs.get("msgtype")}\n') + logfileh1.write(out+"\n") + logfileh1.write(f'https://globe.adsbexchange.com/?icao={sbs["icao"]}&showTrace={datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y-%m-%d}×tamp={sbs["time"]}\n\n') + else: + logfile.write(f'{sbs["txt"]}\n') + logfile.write(f'{sbs["type"]} {sbs.get("msgtype")}\n') + logfile.write(out+"\n") + logfile.write(f'https://globe.adsbexchange.com/?icao={sbs["icao"]}&showTrace={datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y-%m-%d}×tamp={sbs["time"]}\n\n') + for q in txqs: q.put(out+"\r\n") except BaseException: @@ -205,4 +227,3 @@ def thread_wrapper(func, *args): pprint(data, stream=stderr) print(traceback.format_exc(), file=stderr) pass - diff --git a/rootfs/scripts/acars_decode/Decoder.py b/rootfs/scripts/acars_decode/Decoder.py index d21f518..5c79db9 100644 --- a/rootfs/scripts/acars_decode/Decoder.py +++ b/rootfs/scripts/acars_decode/Decoder.py @@ -46,6 +46,19 @@ def decode(msg): print() dat["lat"] = (int(raw.group(2)) + float(raw.group(3))/60) * (-1 if raw.group(1) == "S" else 1) dat["lon"] = (int(raw.group(5)) + float(raw.group(6))/60) * (-1 if raw.group(4) == "W" else 1) + elif dat["msgtype"] == "16": + if raw := search(r"([NS]) (\d{2}\.\d{3})[ ,]([WE])\s{0,2}(\d{1,3}\.\d{3})", dat["txt"]): + print("matched type 16 frac deg") + print(dat["txt"]) + print() + dat["lat"] = float(raw.group(2)) * (-1 if raw.group(1) == "S" else 1) + dat["lon"] = float(raw.group(4)) * (-1 if raw.group(3) == "W" else 1) + elif raw := search(r"([NS])(\d{2})(\d{2}\.\d{2}) ([WE])\s{0,2}(\d{1,3}) ?(\d{1,2}\.\d{2})", dat["txt"]): + print("matched type 16 frac min") + print(dat["txt"]) + print() + dat["lat"] = (int(raw.group(2)) + float(raw.group(3))/60) * (-1 if raw.group(1) == "S" else 1) + dat["lon"] = (int(raw.group(5)) + float(raw.group(6))/60) * (-1 if raw.group(4) == "W" else 1) return dat def decodeACARS(msg): @@ -79,7 +92,7 @@ def decodeVDLM2(msg): if p["name"] == "ac_location": dat["lat"] = p["value"]["loc"]["lat"] dat["lon"] = p["value"]["loc"]["lon"] - print("got VDLM2 with XID pos {dat['lat']} {dat['lon']}") + print(f"got VDLM2 with XID pos {dat['lat']} {dat['lon']}") return dat acdb = {} diff --git a/rootfs/scripts/util.py b/rootfs/scripts/util.py index 2d66ff0..054524d 100644 --- a/rootfs/scripts/util.py +++ b/rootfs/scripts/util.py @@ -54,3 +54,10 @@ def reg2icao(reg): # page = requests.get(URL, headers={"User-Agent": UA}) # icao = BeautifulSoup(page.content, "html.parser").find(id="txt-mode-s").contents[0].strip() return icao + +def icao2reg(icao): + reg = "" + res = _cur.execute("SELECT Registration FROM Aircraft WHERE ModeS == ?", (icao,)).fetchall() + if len(res) > 0: + reg = res[0][0] + return reg