Skip to content

Commit

Permalink
log to file, fix some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatel3001 committed Feb 24, 2024
1 parent dc258b0 commit 6e945c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
29 changes: 25 additions & 4 deletions rootfs/scripts/acars2pos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import locale
import os
import socket
import traceback
from datetime import datetime, timezone
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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":
Expand All @@ -194,15 +203,27 @@ 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}&timestamp={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}&timestamp={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}&timestamp={sbs["time"]}\n\n')

for q in txqs:
q.put(out+"\r\n")
except BaseException:
print("Other exception:", file=stderr)
pprint(data, stream=stderr)
print(traceback.format_exc(), file=stderr)
pass

15 changes: 14 additions & 1 deletion rootfs/scripts/acars_decode/Decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {}
Expand Down
7 changes: 7 additions & 0 deletions rootfs/scripts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6e945c7

Please sign in to comment.