From a01f09839f80390a1aba18e868ad3408a5763c4e Mon Sep 17 00:00:00 2001
From: kx1t
Date: Mon, 8 Apr 2024 16:34:43 +0200
Subject: [PATCH] cleanup
---
rootfs/usr/share/planefence/noise2fence.sh | 31 ++--
rootfs/usr/share/planefence/planefence.sh | 196 +++++++--------------
2 files changed, 79 insertions(+), 148 deletions(-)
diff --git a/rootfs/usr/share/planefence/noise2fence.sh b/rootfs/usr/share/planefence/noise2fence.sh
index 0cc220b9..792e655d 100755
--- a/rootfs/usr/share/planefence/noise2fence.sh
+++ b/rootfs/usr/share/planefence/noise2fence.sh
@@ -2,7 +2,7 @@
# NOISE2FENCE -- a script for extracting recorded noise values from NOISECAPT
# and adding them to CSV files that have been created by PlaneFence
#
-# Copyright 2020 Ramon F. Kolb - licensed under the terms and conditions
+# Copyright 2020-2024 Ramon F. Kolb - licensed under the terms and conditions
# of GPLv3. The terms and conditions of this license are included with the Github
# distribution of this package, and are also available here:
# https://github.com/kx1t/planefence/
@@ -15,6 +15,7 @@
# Feel free to make changes to the variables between these two lines. However, it is
# STRONGLY RECOMMENDED to RTFM! See README.md for explanation of what these do.
#
+# shellcheck disable=SC1091
[[ -f "/usr/share/planefence/planefence.conf" ]] && source /usr/share/planefence/planefence.conf
CSVDIR=/usr/share/planefence/html
@@ -26,14 +27,14 @@ CSVTMP=/usr/share/planefence/persist/.internal/pf-noise-csv.tmp
NOISETMP=/usr/share/planefence/persist/.internal/pf-noise-data.tmp
LOGFILE=/tmp/noise2fence.log
VERBOSE=
-VERSION=0.2-docker
+VERSION=0.3-docker
# -----------------------------------------------------------------------------------
# Figure out if NOISECAPT is active or not. REMOTENOISE contains the URL of the NoiseCapt container/server
# and is configured via the $PF_NOISECAPT variable in the .env file.
# Only if REMOTENOISE contains a URL and this URL is reachable, we collect noise data
# Note that this doesn't check for the validity of the actual URL, just that we can reach it.
#replace wget with curl to save disk space --was [[ "x$REMOTENOISE" != "x" ]] && [[ "$(wget -q -O /dev/null $REMOTENOISE ; echo $?)" == "0" ]] && NOISECAPT=1 || NOISECAPT=0
-[[ "x$REMOTENOISE" != "x" ]] && [[ "$(curl --fail -s -o /dev/null $REMOTENOISE ; echo $?)" == "0" ]] && NOISECAPT=1 || NOISECAPT=0
+if [[ -n "$REMOTENOISE" ]] && curl --fail -s -o /dev/null "$REMOTENOISE"; then NOISECAPT=1; else NOISECAPT=0; fi
if [ "$NOISECAPT" != "1" ]
then
@@ -63,20 +64,19 @@ CSVFILE=$CSVNAMEBASE$NOISEDATE$CSVNAMEEXT
# replace wget by curl to save disk space. was: if [ "$(wget -q -O - $REMOTENOISE/${LOGNAMEBASE##*/}$NOISEDATE$LOGNAMEEXT > $LOGNAMEBASE$NOISEDATE$LOGNAMEEXT.tmp ; echo $?)" != "0" ]
-if [ "$(curl --fail -s $REMOTENOISE/${LOGNAMEBASE##*/}$NOISEDATE$LOGNAMEEXT > $LOGNAMEBASE$NOISEDATE$LOGNAMEEXT.tmp ; echo $?)" != "0" ]
+if ! curl --fail -s "$REMOTENOISE/${LOGNAMEBASE##*/}$NOISEDATE$LOGNAMEEXT" > "$LOGNAMEBASE$NOISEDATE$LOGNAMEEXT.tmp"
then
echo "Can't reach $REMOTENOISE/${LOGNAMEBASE##*/}$NOISEDATE$LOGNAMEEXT ... exiting"
exit 1
fi
-mv -f $LOGNAMEBASE$NOISEDATE$LOGNAMEEXT.tmp $LOGNAMEBASE$NOISEDATE$LOGNAMEEXT
+mv -f "$LOGNAMEBASE$NOISEDATE$LOGNAMEEXT.tmp" "$LOGNAMEBASE$NOISEDATE$LOGNAMEEXT"
LOG "Got $LOGNAMEBASE$NOISEDATE$LOGNAMEEXT from $REMOTELOG"
-NOISEFILE=$LOGNAMEBASE$NOISEDATE$LOGNAMEEXT
+NOISEFILE="$LOGNAMEBASE$NOISEDATE$LOGNAMEEXT"
# make sure there's no stray TMP file around, so we can directly append
-[ -f "$CSVTMP" ] && rm "$CSVTMP"
-[ -f "$NOISETMP" ] && rm "$NOISETMP"
+rm -f "$CSVTMP" "$NOISETMP"
#Now iterate through the CSVFILE:
LOG "------------------------------"
@@ -88,13 +88,13 @@ then
# Clean the $CSVFILE first
# cat "$CSVFILE" | tr -d '\r' >/tmp/noisetmp.tmp
# mv /tmp/noisetmp.tmp "$CSVFILE"
- while read CSVLINE
+ while read -r CSVLINE
do
- XX=$(echo -n $CSVLINE | tr -d '[:cntrl:]')
+ XX=$(echo -n "$CSVLINE" | tr -d '[:cntrl:]')
CSVLINE=$XX
unset RECORD
# Read the line, but first clean it up as it appears to have a newline in it
- IFS="," read -aRECORD <<< "$CSVLINE"
+ IFS="," read -ra RECORD <<< "$CSVLINE"
LOG "${#RECORD[*]} records in the current line: (${RECORD[*]})"
# if there's no audio stored in the record
if [ "${#RECORD[*]}" -le "7" ]
@@ -115,7 +115,7 @@ then
(( NUMPOS=NUMPOS+1 ))
LOG "Start Position: $STARTPOS, Number of samples: $NUMPOS"
# Then put the corresponding noisecapt records into $NOISETMP.
- tail --lines=+"$STARTPOS" $NOISEFILE | head --lines="$NUMPOS" > $NOISETMP
+ tail --lines=+"$STARTPOS" "$NOISEFILE" | head --lines="$NUMPOS" > $NOISETMP
#RECORD[6]="${RECORD[6]//[$'\t\r\n']}"
# Next is to figure out the data that we want to add to the PLANEFENCE record.
# $NOISEFILE and $NOISECAPT have the following format, with all audio values in dBFS:
@@ -133,12 +133,7 @@ then
fi
# Now write everything back to $CSVTMP, which we will then copy back over the old CSV file
( IFS=','; echo "${RECORD[*]}" >> "$CSVTMP" )
- LOG "The record now contains $(IFS=','; echo ${RECORD[*]})"
- #for i in {0..10}
- #do
- # printf "%s," "${RECORD[i]}" >> "$CSVTMP"
- #done
- # printf "%s\n" "${RECORD[11]}" >> "$CSVTMP"
+ LOG "The record now contains ${RECORD[*]}"
done < "$CSVFILE"
# Now, if there is a $CSVTMP file, we will overwrite $CSVFILE with it.
diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh
index cf10e9d1..c8904e81 100755
--- a/rootfs/usr/share/planefence/planefence.sh
+++ b/rootfs/usr/share/planefence/planefence.sh
@@ -7,7 +7,7 @@
#
# Usage: ./planefence.sh
#
-# Copyright 2020-2023 Ramon F. Kolb - licensed under the terms and conditions
+# Copyright 2020-2024 Ramon F. Kolb - licensed under the terms and conditions
# of GPLv3. The terms and conditions of this license are included with the Github
# distribution of this package, and are also available here:
# https://github.com/kx1t/planefence/
@@ -31,7 +31,7 @@
# Only change the variables below if you know what you are doing.
# all errors will show a line number and the command used to produce the error
-trap 'echo -e "[ERROR] $(basename $0) in line $LINENO when executing: $BASH_COMMAND"' ERR
+source /scripts/common
# We need to define the directory where the config file is located:
@@ -74,9 +74,8 @@ fi
# first get DISTANCE unit:
DISTUNIT="mi"
DISTCONV=1
-if [ "$SOCKETCONFIG" != "" ]
-then
- case "$(grep "^distanceunit=" $SOCKETCONFIG |sed "s/distanceunit=//g")" in
+if [[ -f "$SOCKETCONFIG" ]]; then
+ case "$(grep "^distanceunit=" "$SOCKETCONFIG" |sed "s/distanceunit=//g")" in
nauticalmile)
DISTUNIT="nm"
;;
@@ -93,9 +92,8 @@ fi
# get ALTITUDE unit:
ALTUNIT="ft"
-if [ "$SOCKETCONFIG" != "" ]
-then
- case "$(grep "^altitudeunit=" $SOCKETCONFIG |sed "s/altitudeunit=//g")" in
+if [[ -f "$SOCKETCONFIG" ]]; then
+ case "$(grep "^altitudeunit=" "$SOCKETCONFIG" |sed "s/altitudeunit=//g")" in
feet)
ALTUNIT="ft"
;;
@@ -110,8 +108,7 @@ fi
# replace wget by curl to save memory space. Was: [[ "x$REMOTENOISE" != "x" ]] && [[ "$(wget -q -O /tmp/noisecapt-$FENCEDATE.log $REMOTENOISE/noisecapt-$FENCEDATE.log ; echo $?)" == "0" ]] && NOISECAPT=1 || NOISECAPT=0
if [[ "x$REMOTENOISE" != "x" ]]
then
- if [[ "$(curl --fail -s $REMOTENOISE/noisecapt-$FENCEDATE.log > /tmp/noisecapt-$FENCEDATE.log; echo $?)" == "0" ]]
- then
+ if curl --fail -s "$REMOTENOISE/noisecapt-$FENCEDATE.log" > "/tmp/noisecapt-$FENCEDATE.log"; then
NOISECAPT=1
else
NOISECAPT=0
@@ -124,30 +121,32 @@ if [[ "$FUDGELOC" != "" ]]
then
if [[ "$FUDGELOC" == "0" ]]
then
- printf -v LON_VIS "%.0f" $LON
- printf -v LAT_VIS "%.0f" $LAT
+ printf -v LON_VIS "%.0f" "$LON"
+ printf -v LAT_VIS "%.0f" "$LAT"
elif [[ "$FUDGELOC" == "1" ]]
then
- printf -v LON_VIS "%.1f" $LON
- printf -v LAT_VIS "%.1f" $LAT
+ printf -v LON_VIS "%.1f" "$LON"
+ printf -v LAT_VIS "%.1f" "$LAT"
elif [[ "$FUDGELOC" == "2" ]]
then
- printf -v LON_VIS "%.2f" $LON
- printf -v LAT_VIS "%.2f" $LAT
+ printf -v LON_VIS "%.2f" "$LON"
+ printf -v LAT_VIS "%.2f" "$LAT"
else
# If $FUDGELOC != "" but also != "2", then assume it is "3"
- printf -v LON_VIS "%.3f" $LON
- printf -v LAT_VIS "%.3f" $LAT
+ printf -v LON_VIS "%.3f" "$LON"
+ printf -v LAT_VIS "%.3f" "$LAT"
fi
# clean up the strings:
else
# let's not print more than 5 digits
- printf -v LON_VIS "%.5f" $LON
- printf -v LAT_VIS "%.5f" $LAT
+ printf -v LON_VIS "%.5f" "$LON"
+ printf -v LAT_VIS "%.5f" "$LAT"
fi
-LON_VIS="$(sed 's/^00*\|00*$//g' <<< $LON_VIS)" # strip any trailing zeros - "41.10" -> "41.1", or "41.00" -> "41."
+# shellcheck disable=SC2001
+LON_VIS="$(sed 's/^00*\|00*$//g' <<< "$LON_VIS")" # strip any trailing zeros - "41.10" -> "41.1", or "41.00" -> "41."
LON_VIS="${LON_VIS%.}" # If the last character is a ".", strip it - "41.1" -> "41.1" but "41." -> "41"
-LAT_VIS="$(sed 's/^00*\|00*$//g' <<< $LAT_VIS)" # strip any trailing zeros - "41.10" -> "41.1", or "41.00" -> "41."
+# shellcheck disable=SC2001
+LAT_VIS="$(sed 's/^00*\|00*$//g' <<< "$LAT_VIS")" # strip any trailing zeros - "41.10" -> "41.1", or "41.00" -> "41."
LAT_VIS="${LAT_VIS%.}" # If the last character is a ".", strip it - "41.1" -> "41.1" but "41." -> "41"
#
@@ -158,7 +157,7 @@ LAT_VIS="${LAT_VIS%.}" # If the last character is a ".", strip it - "41.1" ->
LOG ()
{
# This reads a string from stdin and stores it in a variable called IN. This enables things like 'echo hello world > LOG'
- while [ -n "$1" ] || read IN; do
+ while [ -n "$1" ] || read -r IN; do
if [ -n "$1" ]; then
IN="$1"
fi
@@ -168,7 +167,7 @@ LOG ()
then
printf "%s-%s[%s]v%s: %s\n" "$(date +"%Y%m%d-%H%M%S")" "$PROCESS_NAME" "$CURRENT_PID" "$VERSION" "$IN" | logger
else
- printf "%s-%s[%s]v%s: %s\n" "$(date +"%Y%m%d-%H%M%S")" "$PROCESS_NAME" "$CURRENT_PID" "$VERSION" "$IN" >> $LOGFILE
+ printf "%s-%s[%s]v%s: %s\n" "$(date +"%Y%m%d-%H%M%S")" "$PROCESS_NAME" "$CURRENT_PID" "$VERSION" "$IN" >> "$LOGFILE"
fi
fi
if [ -n "$1" ]; then
@@ -243,8 +242,7 @@ EOF
1 hr avg |
EOF
# If there are spectrograms for today, then also make a column for these:
- if (( $(ls -1 "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" 2>/dev/null |wc -l) > 0 ))
- then
+ if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then
printf "Spectrogram | \n" >> "$2"
SPECTROPRINT="true"
else
@@ -279,7 +277,8 @@ EOF
# do this for the whole INPUT at once, doing it for every line is slow (subshell, sed initialization)
# Step 1/5. Replace the map zoom by whatever $HEATMAPZOOM contains
- [[ -n "$HEATMAPZOOM" ]] && INPUT=$(sed 's|\(^.*&zoom=\)[0-9]*\(.*\)|\1'"$HEATMAPZOOM"'\2|' <<< "$INPUT")
+ # shellcheck disable=SC2001
+ [[ -n "$HEATMAPZOOM" ]] && INPUT=$(sed 's|\(^.*&zoom=\)[0-9]*\(.*\)|\1'"$HEATMAPZOOM"'\2|' <<< "$INPUT") || true
# Now write the table
COUNTER=1
@@ -346,11 +345,10 @@ EOF
STARTTIME=$(date +%s -d "${NEWVALUES[2]}")
ENDTIME=$(date +%s -d "${NEWVALUES[3]}")
(( ENDTIME - STARTTIME < 30 )) && ENDTIME=$(( STARTTIME + 30 ))
- [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && SPECTROFILE=noisecapt-spectro-$(date -d "@$(awk -F, -v a=$STARTTIME -v b=$ENDTIME 'BEGIN{c=-999; d=0}{if ($1>=0+a && $1<=1+b && $2>0+c) {c=$2; d=$1}} END{print d}' /usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log)" +%y%m%d-%H%M%S).png || SPECTROFILE=""
- # if it has a weird date, discard it because it wont exist.
- # otherwise, go get it from the remote server:
+ [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && SPECTROFILE=noisecapt-spectro-$(date -d "@$(awk -F, -v a="$STARTTIME" -v b="$ENDTIME" 'BEGIN{c=-999; d=0}{if ($1>=0+a && $1<=1+b && $2>0+c) {c=$2; d=$1}} END{print d}' /usr/share/planefence/persist/.internal/noisecapt-"$FENCEDATE".log)" +%y%m%d-%H%M%S).png || SPECTROFILE=""
+ # go get it from the remote server:
# debug code: echo $REMOTENOISE/$SPECTROFILE to $OUTFILEDIR/$SPECTROFILE
- [[ "$SPECTROFILE" == "noisecapt-spectro-691231-190000.png" ]] && SPECTROFILE="" || curl --fail -s "$REMOTENOISE/$SPECTROFILE" > "$OUTFILEDIR/$SPECTROFILE"
+ if ! curl --fail -s "$REMOTENOISE/$SPECTROFILE" > "$OUTFILEDIR/$SPECTROFILE"; then SPECTROFILE=""; fi
else
SPECTROFILE=""
fi
@@ -375,7 +373,7 @@ EOF
# if it's not in the cache, look it up with the appropriate shell script
if [[ -z $CACHEDNAME ]]; then
- AIRLINENAME=$(/usr/share/planefence/airlinename.sh ${CALLSIGN} ${NEWVALUES[0]})
+ AIRLINENAME=$(/usr/share/planefence/airlinename.sh "${CALLSIGN}" "${NEWVALUES[0]}")
#echo ${CALLSIGN} ${AIRLINENAME}
elif [[ $CACHEDNAME == "UNKNOWN" ]]; then
AIRLINENAME=""
@@ -501,19 +499,21 @@ EOF
# right below. Right now, it lists all files that have the planefence-20*.html format (planefence-200504.html, etc.), and then
# picks the newest 7 (or whatever HISTTIME is set to), reverses the strings to capture the characters 6-11 from the right, which contain the date (200504)
# and reverses the results back so we get only a list of dates in the format yymmdd.
- for d in $(ls -1 "$1"/planefence-??????.html | tail --lines=$((HISTTIME+1)) | head --lines=$HISTTIME | rev | cut -c6-11 | rev | sort -r)
+ # shellcheck disable=SC2012
+ for d in $(ls -1 "$1"/planefence-??????.html | tail --lines=$((HISTTIME+1)) | head --lines="$HISTTIME" | rev | cut -c6-11 | rev | sort -r)
do
- printf " | %s" "$(date -d "$d" +%d-%b-%Y): " >> "$2"
- printf "html - " "planefence-$(date -d "$d" +"%y%m%d").html" >> "$2"
- printf "csv" "planefence-$(date -d "$d" +"%y%m%d").csv" >> "$2"
+ { printf " | %s" "$(date -d "$d" +%d-%b-%Y): "
+ printf "html - " "planefence-$(date -d "$d" +"%y%m%d").html"
+ printf "csv" "planefence-$(date -d "$d" +"%y%m%d").csv"
+ } >> "$2"
done
- printf "
\n" >> "$2"
- printf "Additional dates may be available by browsing to planefence-yymmdd.html in this directory.
" >> "$2"
- printf "\n\n" >> "$2"
+ { printf "\n"
+ printf "Additional dates may be available by browsing to planefence-yymmdd.html in this directory.
"
+ printf "\n\n"
+ } >> "$2"
# and print the footer:
- if [ "$3" == "standalone" ]
- then
+ if [[ "$3" == "standalone" ]]; then
printf "