Skip to content

Commit

Permalink
rollback message type check, add a bunch of regexes for all messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatel3001 committed Feb 26, 2024
1 parent 6e945c7 commit 685b894
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 64 deletions.
23 changes: 10 additions & 13 deletions rootfs/scripts/acars2pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import requests
from bs4 import BeautifulSoup
from colorama import Fore, init
from colorama import Fore

from acars_decode import Decoder as AD
from util import *
Expand Down Expand Up @@ -99,8 +99,6 @@ def thread_wrapper(func, *args):
if not sbs.get("txt") and not sbs.get("lat"):
continue

sbs["txt"] = sbs.get("txt", "").upper().replace("\r", "").replace("\n", "")

if sbs.get("lat"):
lat = sbs["lat"]
lon = sbs["lon"]
Expand All @@ -114,13 +112,14 @@ def thread_wrapper(func, *args):
pos2b = findall("LON", sbs["txt"])
if (len(pos1) == 1):
txt = sub(rgx1, Fore.RED + r'\1' + Fore.RESET, sbs["txt"])
print(f"old regex 1 matched message type {dat['msgtype']}")
print(txt)

pos = pos1[0]
pos = sub(r'/', '', pos)
pos = sub(r'\s', '', pos)
pos = sub(r',', '', pos)
pos = sub(r'\.', '', pos)
pos = sub(r'-', 'W', pos)

issouth = "S" in pos
iswest = "W" in pos
Expand All @@ -130,8 +129,6 @@ def thread_wrapper(func, *args):
if not(isnorth or issouth) and not(iswest or iseast):
continue

print(txt, file=stderr)

pos = split(r'[WE]', pos[1:])

lat = pos[0].lstrip("0")
Expand All @@ -144,13 +141,14 @@ def thread_wrapper(func, *args):
continue
elif len(pos1b) == 1:
txt = sub(rgx2, Fore.RED + r'\1' + Fore.RESET, sbs["txt"])
print(f"old regex 2 matched message type {dat['msgtype']}")
print(txt)

pos = pos1b[0]
pos = sub(r'/', '', pos)
pos = sub(r'\s', '', pos)
pos = sub(r',', '', pos)
pos = sub(r'\.', '', pos)
pos = sub(r'-', 'W', pos)

issouth = "S" in pos
iswest = "W" in pos
Expand All @@ -160,8 +158,6 @@ def thread_wrapper(func, *args):
if not(isnorth or issouth) and not(iswest or iseast):
continue

print(txt, file=stderr)

pos = split(r'[NS]', pos[1:])

lat = pos[0].lstrip("0")
Expand All @@ -175,7 +171,8 @@ def thread_wrapper(func, *args):
elif len(pos2a) and len(pos2b):
txt = sub(r'(LAT)', Fore.MAGENTA + r'\1' + Fore.RESET, sbs["txt"])
txt = sub(r'(LON)', Fore.MAGENTA + r'\1' + Fore.RESET, txt)
print(txt, file=stderr)
print(f"old regex 3 matched message type {dat['msgtype']}")
print(txt)
continue
else:
continue
Expand Down Expand Up @@ -203,13 +200,13 @@ 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.get("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:.3f},{lon:.3f},,{squawk},,,,'

print(f'https://globe.adsbexchange.com/?icao={sbs["icao"]}&showTrace={datetime.fromtimestamp(sbs["time"], tz=timezone.utc):%Y-%m-%d}&timestamp={sbs["time"]}')
print(f'{Fore.BLUE}{out}{Fore.RESET}\n', file=stderr)

if getenv("LOG_FILE"):
if sbs.get("msgtype") == "H1":
if getenv("LOG_FILE") and sbs.get("msgtype") and sbs.get("type") != "hfdl":
if sbs["msgtype"] == "H1":
logfileh1.write(f'{sbs["txt"]}\n')
logfileh1.write(f'{sbs["type"]} {sbs.get("msgtype")}\n')
logfileh1.write(out+"\n")
Expand Down
92 changes: 41 additions & 51 deletions rootfs/scripts/acars_decode/Decoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from re import search
from re import compile, sub
from colorama import Fore

rgxs = [compile(r"([NS])[ 0]{0,2}(\d{1,2}\.\d{3}).?([WE])[0 ]{0,2}(\d{1,3}\.\d{3})"),
compile(r"([NS])[ 0]{0,1}(\d{1,2})[ 0]{0,1}(\d{1,2}\.\d{1}).?([WE])[ 0]{0,2}(\d{1,3}?)[ 0]{0,1}(\d{1,2}\.\d{1})"),
compile(r"([NS])[ 0]{0,1}(\d{1,2})[ 0]{0,3}(\d{0,2}\.\d{2}).?([WE])[ 0]{0,2}(\d{1,3})[ 0]{0,1}(\d{1,2}\.\d{2})"),
compile(r"([NS])\s?(\d{2})(\d{2})(\d{2}).?([WE])\s?(\d{2,3})(\d{2})(\d{2})"),
compile(r"([NS])[ 0]{0,1}(\d{1,2})(\d{3}).?([WE])[ 0]{0,2}(\d{1,3})(\d{3})"),
compile(r"[0 ]{0,1}(\d{1,2})[0 ]{0,1}(\d{1,2}\.\d{1})([NS])[ 0]{0,2}(\d{1,3})[0 ]{0,1}(\d{1,2}\.\d{1})([WE])"),
compile(r"[ 0]{0,1}(\d{1,2})[ 0]{0,1}(\d{1,2})([NS])[ 0]{0,2}(\d{1,3})[ 0]{0,1}(\d{1,2})([WE])"),
compile(r"LAT ([NS]) [0 ]{0,1}(\d{1,2}):(\d{2}\.\d{1}) LONG [WE] [0 ]{0,1}(\d{1,2}):(\d{2}\.\d{1})")]

def decode(msg):
if msg.get("vdl2"):
Expand All @@ -8,57 +18,37 @@ def decode(msg):
else:
dat = decodeACARS(msg)

if dat is None or dat.get("msgtype") is None:
return dat
if dat:
dat["txt"] = dat.get("txt", "").upper().replace("\r", "").replace("\n", "")
for i,rgx in enumerate(rgxs):
raw = rgx.findall(dat["txt"])
if len(raw) == 1:
pos = dat["txt"]
for pat in raw:
pos = sub(f"({pat})", Fore.RED + r"\1" + Fore.RESET, pos)
print(f"regex {i} matched message type {dat['msgtype']}")
print(pos)

raw = raw[0] #search(rgx, dat["txt"])
if i == 0:
dat["lat"] = float(raw[1]) * (-1 if raw[0] == "S" else 1)
dat["lon"] = float(raw[3]) * (-1 if raw[2] == "W" else 1)
elif i == 1 or i == 2 or i == 7:
dat["lat"] = (int(raw[1]) + float(raw[2])/60) * (-1 if raw[0] == "S" else 1)
dat["lon"] = (int(raw[4]) + float(raw[5])/60) * (-1 if raw[3] == "W" else 1)
elif i == 3:
dat["lat"] = (int(raw[1]) + int(raw[2])/60 + int(raw[3])/3600) * (-1 if raw[0] == "S" else 1)
dat["lon"] = (int(raw[5]) + int(raw[6])/60 + int(raw[7])/3600) * (-1 if raw[4] == "W" else 1)
elif i == 4:
dat["lat"] = (int(raw[1]) + int(raw[2])/600) * (-1 if raw[0] == "S" else 1)
dat["lon"] = (int(raw[4]) + int(raw[5])/600) * (-1 if raw[3] == "W" else 1)
elif i == 5 or i == 6:
dat["lat"] = (int(raw[0]) + float(raw[1])/60) * (-1 if raw[2] == "S" else 1)
dat["lon"] = (int(raw[3]) + float(raw[4])/60) * (-1 if raw[5] == "W" else 1)

if dat.get("lat"):
break

if dat["msgtype"] == "10":
if raw := search(r"([NS]) ?(\d{2}\.\d{3})[,\/]([WE]) ?(\d{2,3}\.\d{3})", dat["txt"]):
print("matched type 10")
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(1) == "W" else 1)
elif dat["msgtype"] == "4N":
if raw := search(r"([NS])(\d{3})(\d{3}) ([WE])(\d{3})(\d{3})", dat["txt"]):
print("matched type 4N")
print(dat["txt"])
print()
dat["lat"] = (int(raw.group(2)) + int(raw.group(3))/600) * (-1 if raw.group(1) == "S" else 1)
dat["lon"] = (int(raw.group(5)) + int(raw.group(6))/600) * (-1 if raw.group(4) == "W" else 1)
elif dat["msgtype"] == "4T":
if raw := search(r"(\d{2})(\d{2}\.\d{1})([NS])[ 0](\d{2})(\d{2}\.\d{1})([WE])", dat["txt"]):
print("matched type 4T")
print(dat["txt"])
print()
dat["lat"] = (int(raw.group(1)) + float(raw.group(2))/60) * (-1 if raw.group(3) == "S" else 1)
dat["lon"] = (int(raw.group(4)) + float(raw.group(5))/60) * (-1 if raw.group(6) == "W" else 1)
elif dat["msgtype"] == "21":
if raw := search(r"([NS]) (\d{2}\.\d{3})([WE]) (\d{2}\.\d{3})", dat["txt"]):
print("matched type 21")
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 dat["msgtype"] == "80" or dat["msgtype"] == "83":
if raw := search(r"[NS](\d{2})(\d{2}\.\d{1})[WE](\d{3)(\d{2}}\.\d{1})", dat["txt"]):
print("matched type 80/83")
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)
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):
Expand Down

0 comments on commit 685b894

Please sign in to comment.