diff --git a/rootfs/usr/share/plane-alert/plane-alert.sh b/rootfs/usr/share/plane-alert/plane-alert.sh index 282f932..ced62aa 100755 --- a/rootfs/usr/share/plane-alert/plane-alert.sh +++ b/rootfs/usr/share/plane-alert/plane-alert.sh @@ -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"}" diff --git a/rootfs/usr/share/planefence/airlinename.sh b/rootfs/usr/share/planefence/airlinename.sh index 00610eb..8edb487 100755 --- a/rootfs/usr/share/planefence/airlinename.sh +++ b/rootfs/usr/share/planefence/airlinename.sh @@ -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 # @@ -44,41 +45,37 @@ 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 []" 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 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 } @@ -86,54 +83,56 @@ CLEANUP_CACHE () 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 @@ -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}" @@ -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"