Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kx1t committed Dec 11, 2024
1 parent 3bce9f3 commit bf1af4e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 85 deletions.
47 changes: 23 additions & 24 deletions rootfs/usr/share/plane-alert/plane-alert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -394,38 +394,37 @@ then
unset msg_array
declare -A msg_array

PLANELINE="${ALERT_DICT[${pa_record[0]}]}"
IFS="," read -ra TAGLINE <<< "$PLANELINE"
# Parse this into a single line with syntax ICAO,TailNr,Owner,PlaneDescription,date,time,lat,lon,callsign,adsbx_url,squawk

PLANELINE="${ALERT_DICT[${pa_record[0]}]}"
IFS="," read -ra TAGLINE <<< "$PLANELINE"
# Parse this into a single line with syntax ICAO,TailNr,Owner,PlaneDescription,date,time,lat,lon,callsign,adsbx_url,squawk

ICAO="${pa_record[0]/ */}" # ICAO (stripped spaces)
TAIL="${TAGLINE[1]}"
#Get a tail number if we don't have one
if [[ -z "$TAIL" ]]; then
TAIL="$(grep -i -w "$ICAO" /run/planefence/icao2plane.txt 2>/dev/null | head -1 | awk -F "," '{print $2}')"
fi
ICAO="${pa_record[0]/ */}" # ICAO (stripped spaces)
TAIL="${TAGLINE[1]}"
#Get a tail number if we don't have one
if [[ -z "$TAIL" ]]; then
TAIL="$(grep -i -w "$ICAO" /run/planefence/icao2plane.txt 2>/dev/null | head -1 | awk -F "," '{print $2}')"
fi

#Get an owner if there's none, we have a tail number and we are in the US
OWNER="${TAGLINE[2]}"
if [[ -z $OWNER ]] && [[ -n $TAIL ]]; then
#if [[ "${TAIL:0:1}" == "N" ]]; then
if [[ $TAIL =~ ^N[0-9][0-9a-zA-Z]+$ ]]; then
OWNER="$(/usr/share/planefence/airlinename.sh "$TAIL")"
fi
fi
#Get an owner if there's none and there is a flight number
if [[ -z $OWNER ]] && [[ -n ${pa_record[8]/ */} ]]; then
OWNER="$(/usr/share/planefence/airlinename.sh "${pa_record[8]/ */}")"
fi
#Get an owner if there's none, we have a tail number and we are in the US
OWNER="${TAGLINE[2]}"
if [[ -z $OWNER ]] && [[ -n $TAIL ]]; then
#if [[ "${TAIL:0:1}" == "N" ]]; then
if [[ $TAIL =~ ^N[0-9][0-9a-zA-Z]+$ ]]; then
OWNER="$(/usr/share/planefence/airlinename.sh "$TAIL")"
fi
fi
#Get an owner if there's none and there is a flight number
if [[ -z $OWNER ]] && [[ -n ${pa_record[8]/ */} ]]; then
OWNER="$(/usr/share/planefence/airlinename.sh "${pa_record[8]/ */}")"
fi

# now put all relevant info into the associative array:
msg_array[icao]="${pa_record[0]//#/}"
msg_array[tail]="${pa_record[1]//#/}"
msg_array[squawk]="${pa_record[10]//#/}"
[[ "${pa_record[10]//#/}" == "7700 " ]] && msg_array[emergency]=true || msg_array[emergency]=false
[[ "${msg_array[squawk]}" == "7700 " ]] && msg_array[emergency]=true || msg_array[emergency]=false
msg_array[flight]="${pa_record[8]//#/}"
msg_array[operator]="${OWNER}"
if [[ -n "${OWNER}" ]]; then msg_array[operator]="${OWNER}"; fi
msg_array[type]="${pa_record[3]//#/}"
msg_array[datetime]="$(date -d "${pa_record[4]} ${pa_record[5]}" "+${MQTT_DATETIME_FORMAT:-%s}")"
msg_array[tracklink]="${pa_record[9]//globe.adsbexchange.com/"$TRACKSERVICE"}"
Expand Down
116 changes: 55 additions & 61 deletions rootfs/usr/share/planefence/airlinename.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/command/with-contenv bash
#shellcheck shell=bash disable=SC1091
#set -x
# airlinename.sh - a Bash shell script to return an airline or owner name based on the aircraft's flight or tail number
#
Expand Down Expand Up @@ -44,96 +45,94 @@ CACHEFILE="/usr/share/planefence/persist/.internal/planeownerscache.txt"
# so that any values redefined in planefence prevail over plane-alert
[[ -f "/usr/share/plane-alert/plane-alert.conf" ]] && source "/usr/share/plane-alert/plane-alert.conf"
# get the planefence.conf configuration:
if [ -f "/usr/share/planefence/planefence.conf" ]
then
source "/usr/share/planefence/planefence.conf"
if [[ -f "/usr/share/planefence/planefence.conf" ]]; then
source "/usr/share/planefence/planefence.conf"
else
echo $/usr/share/planefence/planefence.conf is missing. We need it to run $0!
echo $/usr/share/planefence/planefence.conf is missing. We need it to run "$0"!
exit 2
fi

if [[ ! -f "$AIRLINECODES" ]]
then
if [[ ! -f "$AIRLINECODES" ]]; then
echo "Cannot stat $AIRLINECODES. $0 exiting."
exit 2
fi

if [[ "$1" == "" ]]
then
if [[ -z "$1" ]]; then
echo Missing argument
echo "Usage: $0 <flight_or_tail_number> [<ICAO>]"
exit 2
fi

[[ "$OWNERDBCACHE" == "" ]] && OWNERDBCACHE=7 # time in days
[[ "$REMOTEMISSCACHE" == "" ]] && REMOTEMISSCACHE=3600 # time in seconds
if [[ -z "$OWNERDBCACHE" ]]; then OWNERDBCACHE=7;fi # time in days
if [[ -z "$REMOTEMISSCACHE" ]]; then REMOTEMISSCACHE=3600; fi # time in seconds
MUSTCACHE=0

# Cache Cleanup Script
# Syntax: CLEANUP_CACHE <cachefile> <max age in days>
CLEANUP_CACHE ()
{
[[ "$2" -gt "0" ]] && CACHETIME="$2" || CACHETIME=7
if [[ -f "$1" ]]
then
if [[ "$2" -gt "0" ]]; then CACHETIME="$2"; else CACHETIME=7; fi
if [[ -f "$1" ]]; then
# we could probably combine these, but... first remove the items that have expired in the cache
awk -F ',' -v a="$(date -d "-$CACHETIME days" +%s)" -v b="$(date -d "-$REMOTEMISSCACHE seconds" +%s)" '{if ( ( $3 >= a && $2 != "#NOTFOUND") || ( $3 >= b && $2 == "#NOTFOUND")){print $1 "," $2 "," $3}}' $1 >/tmp/namecache 2>/dev/null
mv -f /tmp/namecache $1 2>/dev/null
awk -F ',' -v a="$(date -d "-$CACHETIME days" +%s)" -v b="$(date -d "-$REMOTEMISSCACHE seconds" +%s)" '{if ( ( $3 >= a && $2 != "#NOTFOUND") || ( $3 >= b && $2 == "#NOTFOUND")){print $1 "," $2 "," $3}}' "$1" >/tmp/namecache 2>/dev/null
mv -f /tmp/namecache "$1" 2>/dev/null
fi
}

# First, let's try to see if it's a regular airline by looking up the argument in our own database:
a="$1" # get the flight number or tail number from the command line argument
a="${a#@}" # strip off any leading "@" signs - this is a Planefence feature

[[ "$2" != "" ]] && c="$2" || c="" # C is optional ICAO
if [[ -n "$2" ]]; then c="$2"; else c=""; fi # C is optional ICAO

#echo "debug: called $0 $1=$a $2=$c"
# add a few exceptions:
[[ "${a:0:4}" == "HMED" ]] && b=" Medevac Bristol"
[[ "${a:0:4}" == "NATO" ]] && b=" NATO"
if [[ "${a:0:4}" == "HMED" ]]; then b=" Medevac Bristol"
elif [[ "${a:0:4}" == "NATO" ]]; then b=" NATO"; fi

# Airlinecodes has the 3-character code in field 1 and the full name in field 2
# to prevent false hits when the tall number starts with 3 letters
# (common outside the US), only call this if the input looks like a flight number
[[ "$b" == "" ]] && echo $a | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null && b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 == a){print $2;exit;}}' $AIRLINECODES)" # Print flight number if we can find it
[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="aln"
if [[ -z "$b" ]]; then
echo "$a" | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null && b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 == a){print $2;exit;}}' "$AIRLINECODES")" # Print flight number if we can find it
elif [[ "$q" == "" ]]; then
q="aln"
fi

# Now, if we got nothing, then let's try the Plane-Alert database.
# The Plane-Alert db has the tail number in field 2 and the full name in field 3:
[[ "$b" == "" ]] && [[ -f "$PLANEFILE" ]] && b="$(awk -F ',' -v a="$a" '{IGNORECASE=1; if ($2 == a){print $3;exit;}}' $PLANEFILE)"
[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="pa-file"
if [[ -z "$b" ]] && [[ -f "$PLANEFILE" ]]; then
b="$(awk -F ',' -v a="$a" '{IGNORECASE=1; if ($2 == a){print $3;exit;}}' "$PLANEFILE")"
fi
if [[ -n "$b" ]] && [[ "$q" == "" ]]; then q="pa-file"; fi

# Still nothing? Let's see if there is a cache, and if so, if there's a match in our cache
# The cache has the search item (probably tail number) field 1 and the full name in field 2. (Field 3 contains the time added to cache):
[[ "$b" == "" ]] && [[ -f "$CACHEFILE" ]] && echo $a | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null && b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 ~ "^"a){print $2;exit;}}' $CACHEFILE)"

[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="ca-a"

if [[ -z "$b" ]] && [[ -f "$CACHEFILE" ]]; then
echo "$a" | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null && b="$(awk -F ',' -v a="${a:0:3}" '{IGNORECASE=1; if ($1 ~ "^"a){print $2;exit;}}' $CACHEFILE)"
fi
if [[ -n "$b" ]] && [[ -z "$q" ]]; then q="ca-a"; fi

[[ "$b" == "" ]] && [[ -f "$CACHEFILE" ]] && b="$(awk -F ',' -v a="$a" '{IGNORECASE=1; if ($1 == a){print $2;exit;}}' $CACHEFILE)"
[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="ca-n"

if [[ -z "$b" ]] && [[ -f "$CACHEFILE" ]]; then
b="$(awk -F ',' -v a="$a" '{IGNORECASE=1; if ($1 == a){print $2;exit;}}' $CACHEFILE)"
fi
if [[ -n "$b" ]] && [[ -z "$q" ]]; then q="ca-n"; fi

# Nothing? Then do an FAA DB lookup
if [[ "$b" == "" ]] && [[ "${a:0:1}" == "N" ]]
then
b="$(timeout 3 curl --compressed -A "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0" -s https://registry.faa.gov/AircraftInquiry/Search/NNumberResult?nNumberTxt=$a | grep 'data-label=\"Name\"'|head -1 | sed 's|.*>\(.*\)<.*|\1|g')"
if [[ -z "$b" ]] && [[ "${a:0:1}" == "N" ]]; then
b="$(timeout 5 curl --compressed -A "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0" -s "https://registry.faa.gov/AircraftInquiry/Search/NNumberResult?nNumberTxt=$a" | grep 'data-label=\"Name\"'|head -1 | sed 's|.*>\(.*\)<.*|\1|g')"
# If we got something, make sure it will get added to the cache:
[[ "$b" != "" ]] && MUSTCACHE=1
[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="faa"


if [[ -n "$b" ]]; then MUSTCACHE=1; fi
if [[ -n "$b" ]] && [[ -z "$q" ]]; then q="faa"; fi
fi

if [[ "$b" == "" ]]
then
# check OpenSky DB -- this is a bit of a Last Resort as the OS database isn't too accurate
if [[ -f /run/OpenSkyDB.csv ]]
then
b="$(awk -F "," -v p="${a,,}" '{IGNORECASE=1; gsub("-",""); gsub("\"",""); if(tolower($2)==p) {print $14;exit}}' /run/OpenSkyDB.csv)"
[[ "$b" != "" ]] && MUSTCACHE=1
[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="OpenSky"
if [[ -z "$b" ]]; then
# check OpenSky DB -- this is a bit of a Last Resort as the OS database isn't too accurate
if [[ -f /run/OpenSkyDB.csv ]]; then
b="$(awk -F "," -v p="${a,,}" '{IGNORECASE=1; gsub("-",""); gsub("\"",""); if(tolower($2)==p) {print $14;exit}}' /run/OpenSkyDB.csv)"
if [[ -n "$b" ]]; then MUSTCACHE=1; fi
if [[ -n "$b" ]] && [[ -z "$q" ]]; then q="OpenSky"; fi
fi
fi

Expand All @@ -143,27 +142,23 @@ fi
# ---------------------------------------------------

# Still nothing - if it looks like an flight number, then try the Planefence server as a last resort
if [[ "$CHECKREMOTEDB" == "ON" ]] && [[ "$b" == "" ]] && [[ "$(echo $a | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null ; echo $?)" == "0" ]]
then
if chk_enabled "$CHECKREMOTEDB" && [[ -z "$b" ]] && [[ "$(echo "$a" | grep -e '^[A-Za-z]\{3\}[0-9][A-Za-z0-9]*' >/dev/null ; echo $?)" == "0" ]]; then
b="$(curl -L -s "$REMOTEURL/?flight=$a&icao=$c")"
[[ "${b:0:1}" == "#" ]] && b="#NOTFOUND" # results starting with # are errors or not-founds
if [[ "${b:0:1}" == "#" ]]; then b="#NOTFOUND"; fi # results starting with # are errors or not-founds
MUSTCACHE=2 # 2 means only cache the airline prefix
elif [[ "$CHECKREMOTEDB" == "ON" ]] && [[ "$b" == "" ]] && [[ "${a:0:4}" == "HMED" ]]
elif chk_enabled "$CHECKREMOTEDB" && [[ -z "$b" ]] && [[ "${a:0:4}" == "HMED" ]]
then
b="$(curl -L -s "$REMOTEURL/?flight=$a&icao=$c")"
[[ "${b:0:1}" == "#" ]] && b="#NOTFOUND" # results starting with # are errors or not-founds
if [[ "${b:0:1}" == "#" ]]; then b="#NOTFOUND"; fi # results starting with # are errors or not-founds
MUSTCACHE=2 # 2 means only cache the airline prefix
fi

[[ "$b" != "" ]] && [[ "$q" == "" ]] && q="rdb"


if [[ -n "$b" ]] && [[ "$q" == "" ]]; then q="rdb"; fi

# Clean up the results
if [[ "$b" != "" ]]
then
if [[ -n "$b" ]]; then
b="${b^^}"
b="$(echo $b|xargs -0)" #clean up extra spaces
b="$(echo "$b"|xargs -0)" #clean up extra spaces
b="${b% [A-Z0-9]}" #clean up single letters/numbers at the end, so "KENNEDY JOHN F" becomes "KENNEDY JOHN"
b="${b% DBA}" #clean up some undesired suffices, mostly corporate entity names
b="${b% TRUSTEE}"
Expand All @@ -187,19 +182,18 @@ then
fi

# Write back to cache if needed
[[ "$MUSTCACHE" == "1" ]] && printf "%s,%s,%s\n" "$a" "$b" "$(date +%s)" >> "$CACHEFILE"
[[ "$MUSTCACHE" == "2" ]] && printf "%s,%s,%s\n" "${a:0:4}" "$b" "$(date +%s)" >> "$CACHEFILE"
[[ "$MUSTCACHE" != "0" ]] && CLEANUP_CACHE $CACHEFILE $OWNERDBCACHE
if [[ "$MUSTCACHE" == "1" ]]; then printf "%s,%s,%s\n" "$a" "$b" "$(date +%s)" >> "$CACHEFILE"; fi
if [[ "$MUSTCACHE" == "2" ]]; then printf "%s,%s,%s\n" "${a:0:4}" "$b" "$(date +%s)" >> "$CACHEFILE"; fi
if [[ "$MUSTCACHE" != "0" ]]; then CLEANUP_CACHE "$CACHEFILE" "$OWNERDBCACHE"; fi

# prune dupes from cache
if [[ "$MUSTCACHE" != "0" ]] && [[ "$(awk -F',' 'seen[$1]++' $CACHEFILE 2>/dev/null |wc -l)" != "0" ]]
then
if [[ "$MUSTCACHE" != "0" ]] && [[ "$(awk -F',' 'seen[$1]++' $CACHEFILE 2>/dev/null |wc -l)" != "0" ]]; then
awk -F',' '!seen[$1]++' "$CACHEFILE" >/tmp/airlinecache
mv -f /tmp/airlinecache "$CACHEFILE"
fi

# so.... if we got no reponse from the remote server, then remove it now:
[[ "$b" == "#NOTFOUND" ]] && b=""
if [[ "$b" == "#NOTFOUND" ]]; then b=""; fi

# Lookup is done - return the result
echo "$b"

0 comments on commit bf1af4e

Please sign in to comment.