From 1d37e1ad949dc0b42f4939c55308582a27e9bb05 Mon Sep 17 00:00:00 2001 From: kx1t Date: Fri, 15 Mar 2024 17:00:07 -0400 Subject: [PATCH 01/15] create `.internal` directory with 0777 instead of 0700 (to allow alertlist to be read by `lighttpd`) --- rootfs/usr/share/planefence/prep-planefence.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rootfs/usr/share/planefence/prep-planefence.sh b/rootfs/usr/share/planefence/prep-planefence.sh index a239398..4e82b7a 100755 --- a/rootfs/usr/share/planefence/prep-planefence.sh +++ b/rootfs/usr/share/planefence/prep-planefence.sh @@ -44,9 +44,11 @@ function configure_both() { # However, if there is a planefence.config file in the ..../persist directory # (by default exposed to ~/.planefence) then export all of those variables as well # note that the grep strips off any spaces at the beginning of a line, and any commented line -mkdir -p -m 0700 /usr/share/planefence/persist/.internal +mkdir -p -m 0777 /usr/share/planefence/persist/.internal mkdir -p /usr/share/planefence/persist/planepix -chmod -fR a+rw /usr/share/planefence/persist /usr/share/planefence/persist/{.[!.]*,*} +chmod -f a=rwx /usr/share/planefence/persist +chmod -fR a+rw /usr/share/planefence/persist/{.[!.]*,*} + chmod a=rwx /usr/share/planefence/persist/planepix if [[ -f /usr/share/planefence/persist/planefence.config ]] then From ae6b852a8701978e12207f663c05d81d4aaa736e Mon Sep 17 00:00:00 2001 From: kx1t Date: Sun, 7 Apr 2024 23:28:30 +0200 Subject: [PATCH 02/15] fix gnuplot for noisecapt when used in +XXXX timezonez --- rootfs/usr/share/planefence/planefence.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index b28d9b6..cf10e9d 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -329,10 +329,10 @@ EOF (( ENDTIME - STARTTIME < 30 )) && ENDTIME=$(( STARTTIME + 15 )) && STARTTIME=$(( STARTTIME - 15)) NOWTIME=$(date +%s) # check if there are any noise samples - if (( (NOWTIME - ENDTIME) > (ENDTIME - STARTTIME) )) && [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && [[ "$(awk -v s=$STARTTIME -v e=$$ENDTIME '$1>=s && $1<=e' /usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log | wc -l)" -gt "0" ]] + if (( (NOWTIME - ENDTIME) > (ENDTIME - STARTTIME) )) && [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && [[ "$(awk -v s="$STARTTIME" -v e="$ENDTIME" '$1>=s && $1<=e' /usr/share/planefence/persist/.internal/noisecapt-"$FENCEDATE".log | wc -l)" -gt "0" ]] then #echo debug gnuplot start=$STARTTIME end=$ENDTIME infile=/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log outfile=$NOISEGRAPHFILE - gnuplot -e "offset=$(echo "`date +%z` * 36" | bc); start="$STARTTIME"; end="$ENDTIME"; infile='/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log'; outfile='"$NOISEGRAPHFILE"'; plottitle='$TITLE'; margin=60" $PLANEFENCEDIR/noiseplot.gnuplot + gnuplot -e "offset=$(echo "$(date +%z) * 36" | sed 's/+[0]\?//g' | bc); start=$STARTTIME; end=$ENDTIME; infile='/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log'; outfile='$NOISEGRAPHFILE'; plottitle='$TITLE'; margin=60" $PLANEFENCEDIR/noiseplot.gnuplot else NOISEGRAPHLINK="" fi @@ -346,11 +346,11 @@ 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="" + [[ -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: # debug code: echo $REMOTENOISE/$SPECTROFILE to $OUTFILEDIR/$SPECTROFILE - [[ "$SPECTROFILE" == "noisecapt-spectro-691231-190000.png" ]] && SPECTROFILE="" || curl --fail -s $REMOTENOISE/$SPECTROFILE > $OUTFILEDIR/$SPECTROFILE + [[ "$SPECTROFILE" == "noisecapt-spectro-691231-190000.png" ]] && SPECTROFILE="" || curl --fail -s "$REMOTENOISE/$SPECTROFILE" > "$OUTFILEDIR/$SPECTROFILE" else SPECTROFILE="" fi @@ -661,7 +661,7 @@ then while read -r newline do IFS="," read -ra newrec <<< "$newline" - if grep "^${newrec[0]}," "$OUTFILECSV" 2>&1 >/dev/null + if grep -q "^${newrec[0]}," "$OUTFILECSV" then #debug echo -n "There is a matching ICAO... ${newrec[1]} " # there's a ICAO match between the new record and the existing file @@ -853,14 +853,14 @@ then rm -f /tmp/noiselog 2>/dev/null [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$(date -d "yesterday" +%y%m%d).log" ]] && cp -f "/usr/share/planefence/persist/.internal/noisecapt-$(date -d "yesterday" +%y%m%d).log" /tmp/noiselog [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$(date -d "today" +%y%m%d).log" ]] && cat "/usr/share/planefence/persist/.internal/noisecapt-$(date -d "today" +%y%m%d).log" >> /tmp/noiselog - gnuplot -e "offset=$(echo "`date +%z` * 36" | bc); start="$(date -d "yesterday" +%s)"; end="$(date +%s)"; infile='/tmp/noiselog'; outfile='/usr/share/planefence/html/noiseplot-latest.jpg'; plottitle='Noise Plot over Last 24 Hours (End date = "$(date +%Y-%m-%d)")'; margin=60" $PLANEFENCEDIR/noiseplot.gnuplot + gnuplot -e "offset=$(echo "$(date +%z) * 36" | sed 's/+[0]\?//g' | bc); start=$(date -d "yesterday" +%s); end=$(date +%s); infile='/tmp/noiselog'; outfile='/usr/share/planefence/html/noiseplot-latest.jpg'; plottitle='Noise Plot over Last 24 Hours (End date = $(date +%Y-%m-%d))'; margin=60" $PLANEFENCEDIR/noiseplot.gnuplot rm -f /tmp/noiselog 2>/dev/null -elif (( $(find $TMPDIR/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | wc -l ) > 0 )) +elif (( $(find "$TMPDIR"/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | wc -l ) > 0 )) then - ln -sf $(find $TMPDIR/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | tail -1) $OUTFILEDIR/noisecapt-spectro-latest.png + ln -sf "$(find "$TMPDIR"/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | tail -1)" "$OUTFILEDIR"/noisecapt-spectro-latest.png else - rm -f $OUTFILEDIR/noisecapt-spectro-latest.png 2>/dev/null + rm -f "$OUTFILEDIR"/noisecapt-spectro-latest.png 2>/dev/null fi [[ "$BASETIME" != "" ]] && echo "10. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- done getting NoiseCapt stuff, invoking plane-alert.sh" || true From a01f09839f80390a1aba18e868ad3408a5763c4e Mon Sep 17 00:00:00 2001 From: kx1t Date: Mon, 8 Apr 2024 16:34:43 +0200 Subject: [PATCH 03/15] 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 0cc220b..792e655 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 cf10e9d..c8904e8 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 "\n\n" >>"$2" fi } @@ -534,9 +534,9 @@ then rm "$TMPLINES" 2>/dev/null rm "$OUTFILEHTML" 2>/dev/null rm "$OUTFILECSV" 2>/dev/null - rm $OUTFILEBASE-"$FENCEDATE"-table.html 2>/dev/null - rm $OUTFILETMP 2>/dev/null - rm $TMPDIR/dump1090-pf* 2>/dev/null + rm "$OUTFILEBASE-$FENCEDATE"-table.html 2>/dev/null + rm "$OUTFILETMP" 2>/dev/null + rm "$TMPDIR"/dump1090-pf* 2>/dev/null LOG "File cache reset- doing full run for $FENCEDATE" fi @@ -549,6 +549,7 @@ then else READLINES=0 fi +# shellcheck disable=SC2153 if [ -f "$TOTLINES" ] then read -r TOTALLINES < "$TOTLINES" @@ -588,12 +589,12 @@ SOCKETFILEYESTERDAY="$LOGFILEBASE$(date -d yesterday +%y%m%d).txt" if [[ -f $SOCKETFILEYESTERDAY ]] && (( $(date -d "1970-01-01 $(date +%T) +0:00" +%s) > PRUNEMINS * 60 )) then # If we're longer than PRUNEMINS into today, remove yesterday's file - rm -v -f $SOCKETFILEYESTERDAY + rm -v -f "$SOCKETFILEYESTERDAY" fi # if the PRUNESTARTFILE file doesn't exist # note down that we started up, write down 0 for the next prune as nothing will be older than PRUNEMINS -if ! [ -f "$PRUNESTARTFILE" ] || [[ "$LASTFENCEDATE" != "$FENCEDATE" ]]; then +if [[ ! -f "$PRUNESTARTFILE" ]] || [[ "$LASTFENCEDATE" != "$FENCEDATE" ]]; then echo 0 > $PRUNESTARTFILE # if PRUNESTARTFILE is older than PRUNEMINS, do the pruning elif [[ $(find $PRUNESTARTFILE -mmin +$PRUNEMINS | wc -l) == 1 ]]; then @@ -603,15 +604,15 @@ elif [[ $(find $PRUNESTARTFILE -mmin +$PRUNEMINS | wc -l) == 1 ]]; then CUTLINES=0 fi tmpfile=$(mktemp) - tail --lines=+$((CUTLINES + 1)) "$SOCKETFILE" > $tmpfile + tail --lines=+$((CUTLINES + 1)) "$SOCKETFILE" > "$tmpfile" # restart Socket30003 to ensure that things run smoothly: touch /tmp/socket-cleanup # this flags the socket30003 runfile not to complain about the exit and restart immediately killall /usr/bin/perl sleep .1 # give the script a moment to exit, then move the files - mv -f $tmpfile "$SOCKETFILE" - rm -f $tmpfile + mv -f "$tmpfile" "$SOCKETFILE" + rm -f "$tmpfile" # update line numbers (( READLINES -= CUTLINES )) @@ -639,13 +640,13 @@ echo "$TOTALLINES" > "$TOTLINES" LOG "Current run starts at line $READLINES of $CURRCOUNT, with $TOTALLINES lines for today" # Now create a temp file with the latest logs -tail --lines=+$READLINES "$SOCKETFILE" > $INFILETMP +tail --lines=+"$READLINES" "$SOCKETFILE" > "$INFILETMP" [[ "$BASETIME" != "" ]] && echo "2. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- invoking planefence.py" || true # First, run planefence.py to create the CSV file: LOG "Invoking planefence.py..." -$PLANEFENCEDIR/planefence.py --logfile=$INFILETMP --outfile=$OUTFILETMP --maxalt=$MAXALT --altcorr=$ALTCORR --dist=$DIST --distunit=$DISTUNIT --lat=$LAT --lon=$LON $VERBOSE $CALCDIST --trackservice=$TRACKSERVICE | LOG +$PLANEFENCEDIR/planefence.py --logfile="$INFILETMP" --outfile="$OUTFILETMP" --maxalt="$MAXALT" --altcorr="$ALTCORR" --dist="$DIST" --distunit="$DISTUNIT" --lat="$LAT" --lon="$LON" "$VERBOSE" "$CALCDIST" --trackservice="$TRACKSERVICE" | LOG LOG "Returned from planefence.py..." # Now we need to combine any double entries. This happens when a plane was in range during two consecutive Planefence runs @@ -721,6 +722,7 @@ $PLANEFENCEDIR/pf-fix.sh "$OUTFILECSV" # Ignore list -- first clean up the list to ensure there are no empty lines sed -i '/^$/d' "$IGNORELIST" 2>/dev/null # now apply the filter +# shellcheck disable=SC2126 LINESFILTERED=$(grep -i -f "$IGNORELIST" "$OUTFILECSV" 2>/dev/null | wc -l) if (( LINESFILTERED > 0 )) then @@ -743,80 +745,12 @@ then mv -f /tmp/pf-out.tmp "$OUTFILECSV" fi # rewrite LINESFILTERED to file - [[ -f /run/planefence/filtered-$FENCEDATE ]] && read -ra i < "/run/planefence/filtered-$FENCEDATE" || i=0 + if [[ -f /run/planefence/filtered-$FENCEDATE ]]; then read -r i < "/run/planefence/filtered-$FENCEDATE"; else i=0; fi echo $((LINESFILTERED + i)) > "/run/planefence/filtered-$FENCEDATE" fi -# Now see is IGNORETIME is set. If so, we need to filter duplicates -# We will do it all in memory - load OUTFILECSV into an array, process the array, and write back to disk: -#if [[ -f "$OUTFILECSV" ]] && [[ "$IGNORETIME" -gt 0 ]] -#then -# -# # read the entire OUTFILECSV into memory: line by line into 'l[]' -# unset l -# i=0 -# while IFS= read -r l[i] -# do -# (( i++ )) -# done < "$OUTFILECSV" -# -# # if the file was empty, stop processing -# -# # $l[] contains all the OUTFILECSV lines. $i contains the total line count -# # Loop through them in reverse order - skip the top one as the 1st entry is always unique -# # Note - if the file is empty or has only 1 element, then the initial value of j (=i-1) = -1 or 0 and the -# # loop will be skipped. This is intentional behavior. -# -# for (( j=i-1; j>0; j-- )) -# do -# unset r -# IFS=, read -ra r <<< "${l[j]}" -# # $l now contains the entire line, $r contains the line in records. Start time is in r[2]. End time is in r[3] -# # We now need to filter out any that are too close in time -# echo r: ${r[@]} -# echo rst: date -d "${r[2]}" +%s -# rst=$(date -d "${r[2]}" +%s) # get the record's start time in seconds (rst= r start time) -# icao="${r[0]}" # get the record's icao address -# for (( k=j-1; k>=0; k-- )) -# do -# # if the line is empty, continue, else read in the line -# [[ -z "${l[k]}" ]] && continue -# unset s -# IFS=, read -ra s <<< "${l[k]}" -# -# # skip/continue if ICAO don't match -# [[ "${s[0]}" != "$icao" ]] && continue -# -# # stop processing this loop if the time diff is larger -# tet=$(date -d "${s[3]}" +%s) # (tet= test's end time. Didn't want to use 'set') -# echo tet: date -d "${s[3]}" +%s -# (( rst - tet > IGNORETIME )) && break -# -# # If we're still here, then the ICAO's match and the time is within the IGNORETIME boundaries. -# # So we take action and empty out the entire string -# l[k]="" -# done -# done -# -# # Now, the array in memory contains the records, with empty lines for the dupes -# # Write back all lines except for the empty ones: -# rm -f /tmp/pf-out.tmp -# for ((a=0; a> /tmp/pf-out.tmp -# done -# # mv /tmp/pf-out.tmp "$OUTFILECSV" -# mv -f /tmp/pf-out.tmp /usr/share/planefence/persist -# -# # clean up some memory -# unset l r s i j k a rst tet icao -# -#fi - -#----end implementation of ignore list---# -# And see if we need to invoke PlaneTweet: - +# see if we need to invoke PlaneTweet: [[ "$BASETIME" != "" ]] && echo "7. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- done applying filters, invoking PlaneTweet" || true if [[ -n "$PLANETWEET" || "${PF_DISCORD,,}" == "true" || "${PF_DISCORD,,}" == "on" || -n "$MASTODON_SERVER" ]] && [[ -z "$1" ]] @@ -865,7 +799,7 @@ fi [[ "$BASETIME" != "" ]] && echo "10. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- done getting NoiseCapt stuff, invoking plane-alert.sh" || true # If $PLANEALERT=on then lets call plane-alert to see if the new lines contain any planes of special interest: -[ "$PLANEALERT" == "ON" ] && ( LOG "Calling Plane-Alert as $PLALERTFILE $INFILETMP"; $PLALERTFILE $INFILETMP; ) +[ "$PLANEALERT" == "ON" ] && ( LOG "Calling Plane-Alert as $PLALERTFILE $INFILETMP"; $PLALERTFILE "$INFILETMP"; ) # Next, we are going to print today's HTML file: # Note - all text between 'cat' and 'EOF' is HTML code: @@ -972,15 +906,16 @@ ${PF_MOTD}
  • Last update: $(date +"%b %d, %Y %R:%S %Z")
  • Maximum distance from ${LAT_VIS}°N, ${LON_VIS}°E: $DIST $DISTUNIT -
  • Only aircraft below $(printf "%'.0d" $MAXALT) $ALTUNIT are reported -
  • Data extracted from $(printf "%'.0d" $TOTALLINES) ADS-B messages received since midnight today +
  • Only aircraft below $(printf "%'.0d" "$MAXALT") $ALTUNIT are reported +
  • Data extracted from $(printf "%'.0d" "$TOTALLINES") ADS-B messages received since midnight today EOF [[ "$FUDGELOC" != "" ]] && printf "
  • Please note that the reported station coordinates and the center of the circle on the heatmap are rounded for privacy protection. They do not reflect the exact location of the station.\n" >> "$OUTFILEHTMTMP" -[[ -f "/run/planefence/filtered-$FENCEDATE" ]] && [[ -f "$IGNORELIST" ]] && (( $(grep -c "^[^#;]" $IGNORELIST) > 0 )) && printf "
  • %d entries were filtered out today because of an ignore list\n" "$(> "$OUTFILEHTMTMP" +[[ -f "/run/planefence/filtered-$FENCEDATE" ]] && [[ -f "$IGNORELIST" ]] && (( $(grep -c "^[^#;]" "$IGNORELIST") > 0 )) && printf "
  • %d entries were filtered out today because of an ignore list\n" "$(<"/run/planefence/filtered-$FENCEDATE")" >> "$OUTFILEHTMTMP" [[ "$PA_LINK" != "" ]] && printf "
  • Additionally, click here to visit Plane Alert: a watchlist of aircraft in general range of the station.\n" "$PA_LINK" >> "$OUTFILEHTMTMP" +# shellcheck disable=SC2129 cat <>"$OUTFILEHTMTMP" @@ -1002,14 +937,14 @@ cat <>"$OUTFILEHTMTMP"
      EOF -printf "
    • Click on the Transponder ID to see the full flight information/history (from AdsbExchange)" >> "$OUTFILEHTMTMP" +printf "
    • Click on the Transponder ID to see the full flight information/history (from AdsbExchange)" "$LAT_VIS" "$LON_VIS" >> "$OUTFILEHTMTMP" printf "
    • Click on the Flight Number to see the full flight information/history (from FlightAware)" >> "$OUTFILEHTMTMP" printf "
    • Click on the Owner Information to see the FAA record for this plane (private, US registered planes only)" >> "$OUTFILEHTMTMP" (( ALTCORR > 0 )) && printf "
    • Minimum altitude is the altitude above local ground level, which is %s %s MSL." "$ALTCORR" "$ALTUNIT" >> "$OUTFILEHTMTMP" || printf "
    • Minimum altitude is the altitude above sea level." >> "$OUTFILEHTMTMP" [[ "$PLANETWEET" != "" ]] && printf "
    • Click on the word "yes" in the Tweeted column to see the Tweet.\n
    • Note that tweets are issued after a slight delay\n" >> "$OUTFILEHTMTMP" [[ "$PLANETWEET" != "" ]] && printf "
    • Get notified instantaneously of aircraft in range by following @%s on Twitter!\n" "$PLANETWEET" "$PLANETWEET" >> "$OUTFILEHTMTMP" -(( $(find $TMPDIR/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | wc -l ) > 0 )) && printf "
    • Click on the word "Spectrogram" to see the audio spectrogram of the noisiest period while the aircraft was in range\n" >> "$OUTFILEHTMTMP" +(( $(find "$TMPDIR"/noisecapt-spectro*.png -daystart -maxdepth 1 -mmin -1440 -print 2>/dev/null | wc -l ) > 0 )) && printf "
    • Click on the word "Spectrogram" to see the audio spectrogram of the noisiest period while the aircraft was in range\n" >> "$OUTFILEHTMTMP" [[ "$PLANEALERT" == "ON" ]] && printf "
    • See a list of aircraft matching the station's Alert List here\n" >> "$OUTFILEHTMTMP" printf "
    • Press the header of any of the columns to sort by that column.\n" >> "$OUTFILEHTMTMP" @@ -1055,6 +990,7 @@ fi # if $PLANEHEATHTML exists, then add the heatmap if [ -f "$PLANEHEATHTML" ] then + # shellcheck disable=SC2129 cat <>"$OUTFILEHTMTMP"
      @@ -1117,13 +1053,13 @@ EOF [[ "$BASETIME" != "" ]] && echo "16. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- starting final cleanup" || true -pushd "$OUTFILEDIR" > /dev/null +pushd "$OUTFILEDIR" > /dev/null || true mv -f "$OUTFILEHTMTMP" "$OUTFILEHTML" ln -sf "${OUTFILEHTML##*/}" index.html -popd > /dev/null +popd > /dev/null || true # VERY last thing... ensure that the log doesn't overflow: -if [ "$VERBOSE" != "" ] && [ "$LOGFILE" != "" ] && [ "$LOGFILE" != "logger" ] && [[ -f $LOGFILE ]] && (( $(wc -l < $LOGFILE) > 8000 )) +if [ "$VERBOSE" != "" ] && [ "$LOGFILE" != "" ] && [ "$LOGFILE" != "logger" ] && [[ -f $LOGFILE ]] && (( $(wc -l < "$LOGFILE") > 8000 )) then #sed -i -e :a -e '$q;N;8000,$D;ba' tail -n 4000 "$LOGFILE" > "$LOGFILE.tmp" From 559400997f270ad8d3e5e87a21bc0e00aea394c6 Mon Sep 17 00:00:00 2001 From: kx1t Date: Mon, 8 Apr 2024 21:16:03 +0200 Subject: [PATCH 04/15] fix spectrogram generation/display --- rootfs/usr/share/planefence/planefence.sh | 88 ++++++++++------------- 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index c8904e8..ac204bd 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -243,7 +243,7 @@ EOF EOF # If there are spectrograms for today, then also make a column for these: if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then - printf "Spectrogram\n" >> "$2" + printf " Spectrogram\n" >&3 SPECTROPRINT="true" else SPECTROPRINT="false" @@ -253,7 +253,7 @@ EOF if [[ "$HASTWEET" == "true" ]] then # print a header for the Tweeted column - printf " Notified\n" >> "$2" + printf " Notified\n" >&3 fi printf "\n" >&3 @@ -282,8 +282,7 @@ EOF # Now write the table COUNTER=1 - while read -r NEWLINE - do + while read -r NEWLINE; do [[ "$NEWLINE" == "" ]] && continue # skip empty lines [[ "${NEWLINE::1}" == "#" ]] && continue #skip lines that start with a "#" @@ -300,8 +299,7 @@ EOF # Step 3/5. If there's noise data, get a background color: # (only when we are printing noise data, and there's actual data in this record) LOUDNESS="" - if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[9]}" != "" ]] - then + if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[9]}" != "" ]]; then (( LOUDNESS = NEWVALUES[7] - NEWVALUES[11] )) BGCOLOR="$RED" (( LOUDNESS <= YELLOWLIMIT )) && BGCOLOR="$YELLOW" @@ -310,16 +308,14 @@ EOF # Step 4/5. Get a noise graph # (only when we are printing noise data, and there's actual data in this record) - if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]] - then + if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]]; then # First, the noise graph: # $NOISEGRAPHFILE is the full file path, NOISEGRAPHLINK is the subset with the filename only NOISEGRAPHFILE="$OUTFILEDIR"/"noisegraph-$(date -d "${NEWVALUES[2]}" +"%y%m%d-%H%M%S")-${NEWVALUES[0]}.png" NOISEGRAPHLINK=${NOISEGRAPHFILE##*/} # If no graph already exists, create one: - if [[ ! -f "$NOISEGRAPHFILE" ]] - then + if [[ ! -f "$NOISEGRAPHFILE" ]]; then # set some parameters for the graph: TITLE="Noise plot for ${NEWVALUES[1]#@} at ${NEWVALUES[3]}" STARTTIME=$(date -d "${NEWVALUES[2]}" +%s) @@ -328,8 +324,7 @@ EOF (( ENDTIME - STARTTIME < 30 )) && ENDTIME=$(( STARTTIME + 15 )) && STARTTIME=$(( STARTTIME - 15)) NOWTIME=$(date +%s) # check if there are any noise samples - if (( (NOWTIME - ENDTIME) > (ENDTIME - STARTTIME) )) && [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && [[ "$(awk -v s="$STARTTIME" -v e="$ENDTIME" '$1>=s && $1<=e' /usr/share/planefence/persist/.internal/noisecapt-"$FENCEDATE".log | wc -l)" -gt "0" ]] - then + if (( (NOWTIME - ENDTIME) > (ENDTIME - STARTTIME) )) && [[ -f "/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log" ]] && [[ "$(awk -v s="$STARTTIME" -v e="$ENDTIME" '$1>=s && $1<=e' /usr/share/planefence/persist/.internal/noisecapt-"$FENCEDATE".log | wc -l)" -gt "0" ]]; then #echo debug gnuplot start=$STARTTIME end=$ENDTIME infile=/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log outfile=$NOISEGRAPHFILE gnuplot -e "offset=$(echo "$(date +%z) * 36" | sed 's/+[0]\?//g' | bc); start=$STARTTIME; end=$ENDTIME; infile='/usr/share/planefence/persist/.internal/noisecapt-$FENCEDATE.log'; outfile='$NOISEGRAPHFILE'; plottitle='$TITLE'; margin=60" $PLANEFENCEDIR/noiseplot.gnuplot else @@ -340,17 +335,26 @@ EOF # Step 5/5. Get a spectrogram # (only when we are printing noise data, and there's actual data in this record) - if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]] - then + if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]]; then 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="" - # go get it from the remote server: - # debug code: echo $REMOTENOISE/$SPECTROFILE to $OUTFILEDIR/$SPECTROFILE - if ! curl --fail -s "$REMOTENOISE/$SPECTROFILE" > "$OUTFILEDIR/$SPECTROFILE"; then SPECTROFILE=""; fi - else - SPECTROFILE="" + + # get the measurement from noisecapt-"$FENCEDATE".log that contains the peak value + # limited by $STARTTIME and $ENDTIME, and then get the corresponding spectrogram file name + spectrotime="$(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)" + sf="noisecapt-spectro-$(date -d "@${spectrotime}" +"%y%m%d-%H%M%S").png" + + if [[ ! -s "$OUTFILEDIR/$sf" ]]; then + # we don't have $sf locally, or if it's an empty file, we get it: + curl -sL "$REMOTENOISE/$sf" > "$OUTFILEDIR/$sf" + fi + # shellcheck disable=SC2012 + if [[ ! -s "$OUTFILEDIR/$sf" ]] || (( $(ls -s1 "$OUTFILEDIR/$sf" | awk '{print $1}') < 10 )); then + # we don't have $sf (or it's an empty file) and we can't get it; so let's erase it in case it's an empty file: + rm -f "$OUTFILEDIR/$sf" + sf="" + fi fi # -------------------------------------------------------------- @@ -364,8 +368,7 @@ EOF # why check for non-printable characters, the file we process is trusted, if there are non-printable chars, fix the input file generation instead of this band-aid printf " %s\n" "${NEWVALUES[6]}" "${NEWVALUES[0]}" >&3 # ICAO printf " %s\n" "https://flightaware.com/live/modes/${NEWVALUES[0]}/ident/${CALLSIGN}/redirect" "${CALLSIGN}" >&3 # Flight number; strip "@" if there is any at the beginning of the record - if [[ "$AIRLINECODES" != "" ]] - then + if [[ "$AIRLINECODES" != "" ]]; then if [[ "${CALLSIGN}" != "" ]] && [[ "${CALLSIGN}" != "link" ]]; then # look up callsign in associative array to get the airline name @@ -382,15 +385,13 @@ EOF fi # update associative array to be written to disk - if [[ -z ${AIRLINENAME} ]] - then + if [[ -z ${AIRLINENAME} ]]; then NEWNAMES[${CALLSIGN}]="UNKNOWN" else NEWNAMES[${CALLSIGN}]="${AIRLINENAME}" fi - if [[ $CALLSIGN =~ ^N[0-9][0-9a-zA-Z]+$ ]] && [[ "${CALLSIGN:0:4}" != "NATO" ]] && [[ "${NEWVALUES[0]:0:1}" == "A" ]] - then + if [[ $CALLSIGN =~ ^N[0-9][0-9a-zA-Z]+$ ]] && [[ "${CALLSIGN:0:4}" != "NATO" ]] && [[ "${NEWVALUES[0]:0:1}" == "A" ]]; then printf " %s\n" "${CALLSIGN}" "${AIRLINENAME}" >&3 else printf " %s\n" "${AIRLINENAME}" >&3 || printf " \n" >&3 @@ -405,13 +406,10 @@ EOF printf " %s %s\n" "${NEWVALUES[5]}" "$DISTUNIT" >&3 # min distance # Print the noise values if we have determined that there is data - if [[ "$HASNOISE" == "true" ]] - then + if [[ "$HASNOISE" == "true" ]]; then # First the loudness field, which needs a color and a link to a noise graph: - if [[ "$LOUDNESS" != "" ]] - then - if [[ "$NOISEGRAPHLINK" != "" ]] - then + if [[ -n "$LOUDNESS" ]]; then + if [[ -n "$NOISEGRAPHLINK" ]]; then printf " %s dB\n" "$BGCOLOR" "$NOISEGRAPHLINK" "$LOUDNESS" >&3 else printf " %s dB\n" "$BGCOLOR" "$LOUDNESS" >&3 @@ -420,10 +418,8 @@ EOF printf " \n" >&3 # print an empty field fi - for i in {7..11} - do - if [[ "${NEWVALUES[i]}" != "" ]] - then + for i in {7..11}; do + if [[ "${NEWVALUES[i]}" != "" ]]; then printf " %s dBFS\n" "${NEWVALUES[i]}" >&3 # print actual value with "dBFS" unit else printf " \n" >&3 # print an empty field @@ -431,11 +427,9 @@ EOF done # print SpectroFile: - if [[ "$SPECTROPRINT" == "true" ]] - then - if [[ -f "$OUTFILEDIR/$SPECTROFILE" ]] - then - printf " Spectrogram\n" "$SPECTROFILE" >&3 + if [[ "$SPECTROPRINT" == "true" ]]; then + if [[ -n "$sf" ]] && [[ -f "$OUTFILEDIR/$sf" ]]; then + printf " Spectrogram\n" "$sf" >&3 else printf " \n" >&3 fi @@ -443,14 +437,11 @@ EOF fi # If there is a tweet value, then provide info and link as available - if [[ "$HASTWEET" == "true" ]] - then + if [[ "$HASTWEET" == "true" ]]; then # Was there a tweet? - if [[ "${NEWVALUES[1]::1}" == "@" ]] - then + if [[ "${NEWVALUES[1]::1}" == "@" ]]; then # Print "yes" and add a link if available - if [[ "${NEWVALUES[-1]::13}" == "https://t.co/" ]] - then + if [[ "${NEWVALUES[-1]::13}" == "https://t.co/" ]]; then printf " tweet\n" "$(tr -dc '[[:print:]]' <<< "${NEWVALUES[-1]}")" >&3 else printf " discord\n" >&3 @@ -850,8 +841,7 @@ gtag('config', 'UA-171737107-1'); EOF -if [[ "${AUTOREFRESH,,}" == "true" ]] -then +if [[ "${AUTOREFRESH,,}" == "true" ]]; then REFRESH_INT="$(sed -n 's/\(^\s*PF_INTERVAL=\)\(.*\)/\2/p' /usr/share/planefence/persist/planefence.config)" cat <>"$OUTFILEHTMTMP" From 49a517381b3fc042daae206e9d3e4e6825ea76f5 Mon Sep 17 00:00:00 2001 From: kx1t Date: Tue, 9 Apr 2024 12:28:31 +0200 Subject: [PATCH 05/15] fix linter complaints --- rootfs/usr/share/planefence/planefence.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index ac204bd..766ce15 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -73,7 +73,7 @@ fi # first get DISTANCE unit: DISTUNIT="mi" -DISTCONV=1 +#DISTCONV=1 if [[ -f "$SOCKETCONFIG" ]]; then case "$(grep "^distanceunit=" "$SOCKETCONFIG" |sed "s/distanceunit=//g")" in nauticalmile) @@ -442,7 +442,7 @@ EOF if [[ "${NEWVALUES[1]::1}" == "@" ]]; then # Print "yes" and add a link if available if [[ "${NEWVALUES[-1]::13}" == "https://t.co/" ]]; then - printf " tweet\n" "$(tr -dc '[[:print:]]' <<< "${NEWVALUES[-1]}")" >&3 + printf " tweet\n" "$(tr -dc '[:print:]' <<< "${NEWVALUES[-1]}")" >&3 else printf " discord\n" >&3 fi From a0407a88266341945540dd1f593959d7630e8bbb Mon Sep 17 00:00:00 2001 From: kx1t Date: Tue, 9 Apr 2024 12:29:02 +0200 Subject: [PATCH 06/15] updates --- rootfs/usr/share/planefence/planefence.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index 766ce15..3eb6743 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -3,7 +3,6 @@ #shellcheck disable=SC2015,SC1091 # # PLANEFENCE - a Bash shell script to render a HTML and CSV table with nearby aircraft -# based on socket30003 # # Usage: ./planefence.sh # From f62c68afc260bdb462785929cb51734067ed6c44 Mon Sep 17 00:00:00 2001 From: kx1t Date: Tue, 9 Apr 2024 12:34:52 +0200 Subject: [PATCH 07/15] fix bug related to showing individual spectrograms --- rootfs/usr/share/planefence/planefence.sh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index 3eb6743..630d559 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -240,13 +240,18 @@ EOF 10 min avg 1 hr avg EOF - # If there are spectrograms for today, then also make a column for these: - if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then - printf " Spectrogram\n" >&3 - SPECTROPRINT="true" - else - SPECTROPRINT="false" - fi + # # If there are spectrograms for today, then also make a column for these: + # if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then + # printf " Spectrogram\n" >&3 + # SPECTROPRINT="true" + # else + # SPECTROPRINT="false" + # fi + # ^^^ this doesn't really work - there won't be any spectrograms at the beginning of the day, and + # it will never create any, because SPECTROPRINT stays FALSE forever. + # Instead, we'll set SPECTROPRINT=true always when HASNOISE=true. This may cause an empty column, but that's + # preferred over not printing any spectrograms. + SPECTROPRINT="true" fi if [[ "$HASTWEET" == "true" ]] From 0579ddc5d77241bffae2d41814d81e7941366140 Mon Sep 17 00:00:00 2001 From: kx1t Date: Tue, 9 Apr 2024 12:46:43 +0200 Subject: [PATCH 08/15] bug fix --- rootfs/usr/share/planefence/planefence.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index 630d559..0b02400 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -239,6 +239,7 @@ EOF 5 min avg 10 min avg 1 hr avg + Spectrogram\n EOF # # If there are spectrograms for today, then also make a column for these: # if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then From 8793e1028b7c5270ae436cfca97f17b512a8b992 Mon Sep 17 00:00:00 2001 From: kx1t Date: Tue, 9 Apr 2024 16:31:05 +0200 Subject: [PATCH 09/15] minor cosmetic issue --- rootfs/usr/share/planefence/planefence.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index 0b02400..b0bf4d8 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -239,7 +239,7 @@ EOF 5 min avg 10 min avg 1 hr avg - Spectrogram\n + Spectrogram EOF # # If there are spectrograms for today, then also make a column for these: # if compgen -G "$OUTFILEDIR/noisecapt-spectro-$FENCEDATE*.png" >/dev/null; then @@ -943,7 +943,7 @@ printf "
    • Click on the Owner Information to see the FAA record for this plane [[ "$PLANEALERT" == "ON" ]] && printf "
    • See a list of aircraft matching the station's Alert List here\n" >> "$OUTFILEHTMTMP" printf "
    • Press the header of any of the columns to sort by that column.\n" >> "$OUTFILEHTMTMP" -printf "
    \n" >> "$OUTFILEHTMTMP" +printf "" >> "$OUTFILEHTMTMP" [[ "$BASETIME" != "" ]] && echo "12. $(bc -l <<< "$(date +%s.%2N) - $BASETIME")s -- starting to write the PF table to the website" || true From 04369723896a466e5b1552cce60fd4efe189b4c1 Mon Sep 17 00:00:00 2001 From: kx1t Date: Mon, 15 Apr 2024 21:33:13 +0200 Subject: [PATCH 10/15] add noisecapt MP3 files if available --- rootfs/usr/share/planefence/planefence.sh | 46 ++++++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index b0bf4d8..483ea35 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -295,13 +295,13 @@ EOF # Do some prep work: # -------------------------------------------------------------- - # Step 1/5. Replace the map zoom by whatever $HEATMAPZOOM contains + # Step 1/6. Replace the map zoom by whatever $HEATMAPZOOM contains # this used to not work (-z instead of -n), to speed it up now, do it on the whole INPUT at once instead per line - # Step 2/5. If there is no flight number, insert the word "link" + # Step 2/6. If there is no flight number, insert the word "link" [[ "${NEWVALUES[1]#@}" == "" ]] && NEWVALUES[1]+="link" - # Step 3/5. If there's noise data, get a background color: + # Step 3/6. If there's noise data, get a background color: # (only when we are printing noise data, and there's actual data in this record) LOUDNESS="" if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[9]}" != "" ]]; then @@ -311,7 +311,7 @@ EOF (( LOUDNESS <= GREENLIMIT )) && BGCOLOR="$GREEN" fi - # Step 4/5. Get a noise graph + # Step 4/6. Get a noise graph # (only when we are printing noise data, and there's actual data in this record) if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]]; then # First, the noise graph: @@ -338,7 +338,7 @@ EOF fi fi - # Step 5/5. Get a spectrogram + # Step 5/6. Get a spectrogram # (only when we are printing noise data, and there's actual data in this record) if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]]; then STARTTIME=$(date +%s -d "${NEWVALUES[2]}") @@ -362,6 +362,30 @@ EOF fi fi + # Step 6/6. Get a MP3 file + # (only when we are printing noise data, and there's actual data in this record) + if [[ "$HASNOISE" == "true" ]] && [[ "${NEWVALUES[7]}" != "" ]]; then + STARTTIME=$(date +%s -d "${NEWVALUES[2]}") + ENDTIME=$(date +%s -d "${NEWVALUES[3]}") + (( ENDTIME - STARTTIME < 30 )) && ENDTIME=$(( STARTTIME + 30 )) + + # get the measurement from noisecapt-"$FENCEDATE".log that contains the peak value + # limited by $STARTTIME and $ENDTIME, and then get the corresponding spectrogram file name + mp3time="$(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)" + mp3f="noisecapt-recording-$(date -d "@${mp3time}" +"%y%m%d-%H%M%S").mp3" + + if [[ ! -s "$OUTFILEDIR/$mp3f" ]]; then + # we don't have $sf locally, or if it's an empty file, we get it: + curl -sL "$REMOTENOISE/$mp3f" > "$OUTFILEDIR/$mp3f" + fi + # shellcheck disable=SC2012 + if [[ ! -s "$OUTFILEDIR/$mp3f" ]] || (( $(ls -s1 "$OUTFILEDIR/$mp3f" | awk '{print $1}') < 10 )); then + # we don't have $mp3f (or it's an empty file) and we can't get it; so let's erase it in case it's an empty file: + rm -f "$OUTFILEDIR/$mp3f" + mp3f="" + fi + fi + # -------------------------------------------------------------- # Now, we're ready to start putting things in the table: @@ -423,7 +447,17 @@ EOF printf " \n" >&3 # print an empty field fi - for i in {7..11}; do + if [[ "${NEWVALUES[7]}" != "" ]]; then + if [[ -n "$mp3f" ]] && [[ -f "$OUTFILEDIR/$mp3f" ]]; then + printf " %s\n" "$mp3f" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit + else + printf " %s dBFS\n" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit + fi + else + printf " \n" >&3 # print an empty field + fi + + for i in {8..11}; do if [[ "${NEWVALUES[i]}" != "" ]]; then printf " %s dBFS\n" "${NEWVALUES[i]}" >&3 # print actual value with "dBFS" unit else From 7dd510ec574bc89be9ea7b4e6a9c4276c9763643 Mon Sep 17 00:00:00 2001 From: kx1t <15090643+kx1t@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:09:54 +0200 Subject: [PATCH 11/15] Fix minor issues --- rootfs/usr/share/planefence/planefence.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index 483ea35..a11cb7a 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -379,7 +379,7 @@ EOF curl -sL "$REMOTENOISE/$mp3f" > "$OUTFILEDIR/$mp3f" fi # shellcheck disable=SC2012 - if [[ ! -s "$OUTFILEDIR/$mp3f" ]] || (( $(ls -s1 "$OUTFILEDIR/$mp3f" | awk '{print $1}') < 10 )); then + if [[ ! -s "$OUTFILEDIR/$mp3f" ]] || (( $(ls -s1 "$OUTFILEDIR/$mp3f" | awk '{print $1}') < 4 )); then # we don't have $mp3f (or it's an empty file) and we can't get it; so let's erase it in case it's an empty file: rm -f "$OUTFILEDIR/$mp3f" mp3f="" @@ -449,7 +449,7 @@ EOF if [[ "${NEWVALUES[7]}" != "" ]]; then if [[ -n "$mp3f" ]] && [[ -f "$OUTFILEDIR/$mp3f" ]]; then - printf " %s\n" "$mp3f" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit + printf " %s\n" "$mp3f" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit else printf " %s dBFS\n" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit fi From 44ddfadf5a78a60c4d213b9cb405fe278abcd256 Mon Sep 17 00:00:00 2001 From: kx1t <15090643+kx1t@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:13:19 +0200 Subject: [PATCH 12/15] Update planefence.sh --- rootfs/usr/share/planefence/planefence.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rootfs/usr/share/planefence/planefence.sh b/rootfs/usr/share/planefence/planefence.sh index a11cb7a..b9934fa 100755 --- a/rootfs/usr/share/planefence/planefence.sh +++ b/rootfs/usr/share/planefence/planefence.sh @@ -449,7 +449,7 @@ EOF if [[ "${NEWVALUES[7]}" != "" ]]; then if [[ -n "$mp3f" ]] && [[ -f "$OUTFILEDIR/$mp3f" ]]; then - printf " %s\n" "$mp3f" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit + printf " %s dBFS\n" "$mp3f" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit else printf " %s dBFS\n" "${NEWVALUES[7]}" >&3 # print actual value with "dBFS" unit fi From 73d194c63fee9a281a5001aaac2cf8492e234850 Mon Sep 17 00:00:00 2001 From: kx1t Date: Thu, 18 Apr 2024 14:21:14 +0200 Subject: [PATCH 13/15] help reduce Mastodon storage cost by defaulting uploaded images for PA notifications to 1 --- README-Mastodon.md | 3 +++ rootfs/usr/share/plane-alert/plane-alert.conf | 3 ++- rootfs/usr/share/plane-alert/plane-alert.sh | 2 +- rootfs/usr/share/planefence/prep-planefence.sh | 10 +++++----- rootfs/usr/share/planefence/stage/planefence.config | 5 ++++- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README-Mastodon.md b/README-Mastodon.md index 1824ba9..12dab96 100644 --- a/README-Mastodon.md +++ b/README-Mastodon.md @@ -45,6 +45,9 @@ Once you have an account, please do the following: ![image](https://user-images.githubusercontent.com/15090643/208438987-3e1fd9c2-5ce9-46c0-92e9-20bb78f55a8c.png) +Note -- if you post lots of traffic to Mastodon, please consider adding an Automatic Post Deletion time of 1 week. +This will help manage storage costs for the operator of the Mastodon server! + ## Configuring Planefence to use Mastodon Please set the following parameters in your `planefence.config` file: diff --git a/rootfs/usr/share/plane-alert/plane-alert.conf b/rootfs/usr/share/plane-alert/plane-alert.conf index 82ab9ea..45eda19 100644 --- a/rootfs/usr/share/plane-alert/plane-alert.conf +++ b/rootfs/usr/share/plane-alert/plane-alert.conf @@ -137,7 +137,8 @@ set +a # MASTODON_ACCESS_TOKEN contains the mastodon access token needed for posting MASTODON_SERVER= MASTODON_ACCESS_TOKEN= - MASTODON_VISIBILITY=unlisted + MASTODON_VISIBILITY=unlisted + MASTODON_MAXIMGS=1 # # AUTOREFRESH controls if the web page auto-refreshed. The refresh interval is the same as $PFINTERVAL. diff --git a/rootfs/usr/share/plane-alert/plane-alert.sh b/rootfs/usr/share/plane-alert/plane-alert.sh index 69fbe7f..26aeba2 100755 --- a/rootfs/usr/share/plane-alert/plane-alert.sh +++ b/rootfs/usr/share/plane-alert/plane-alert.sh @@ -403,7 +403,7 @@ then do fld="$(echo ${field[$i]}|xargs)" ext="${fld: -3}" - if [[ " jpg png peg bmp gif " =~ " $ext " ]] && (( ${#mast_id[@]} < 4 )) + if [[ " jpg png peg bmp gif " =~ " $ext " ]] && (( ${#mast_id[@]} < MASTODON_MAXIMGS )) then rm -f "/tmp/planeimg.*" [[ "$ext" == "peg" ]] && ext="jpeg" || true diff --git a/rootfs/usr/share/planefence/prep-planefence.sh b/rootfs/usr/share/planefence/prep-planefence.sh index 4e82b7a..d9a404d 100755 --- a/rootfs/usr/share/planefence/prep-planefence.sh +++ b/rootfs/usr/share/planefence/prep-planefence.sh @@ -299,20 +299,20 @@ then # strip http:// https:// [[ "${MASTODON_SERVER:0:7}" == "http://" ]] && MASTODON_SERVER="${MASTODON_SERVER:7}" || true [[ "${MASTODON_SERVER:0:8}" == "https://" ]] && MASTODON_SERVER="${MASTODON_SERVER:8}" || true - if [[ "${PF_MASTODON,,}" == "on" ]] - then + if chk_enabled "${PF_MASTODON,,}"; then configure_planefence "MASTODON_ACCESS_TOKEN" "$MASTODON_ACCESS_TOKEN" configure_planefence "MASTODON_SERVER" "$MASTODON_SERVER" - [[ -n "$PF_MASTODON_VISIBILITY" ]] && configure_planefence "MASTODON_VISIBILITY" "$PF_MASTODON_VISIBILITY" || configure_planefence "MASTODON_VISIBILITY" "unlisted" + configure_planefence "MASTODON_VISIBILITY" "${PF_MASTODON_VISIBILITY:-unlisted}" else configure_planefence "MASTODON_ACCESS_TOKEN" "" configure_planefence "MASTODON_SERVER" "" fi - if [[ "${PA_MASTODON,,}" == "on" ]] + if chk_enabled "${PA_MASTODON,,}" then configure_planealert "MASTODON_ACCESS_TOKEN" "$MASTODON_ACCESS_TOKEN" configure_planealert "MASTODON_SERVER" "$MASTODON_SERVER" - [[ -n "$PA_MASTODON_VISIBILITY" ]] && configure_planealert "MASTODON_VISIBILITY" "$PA_MASTODON_VISIBILITY" || configure_planealert "MASTODON_VISIBILITY" "unlisted" + configure_planealert "MASTODON_VISIBILITY" "${PA_MASTODON_VISIBILITY:-unlisted}" + configure_planealert "MASTODON_MAXIMGS" "${PA_MASTODON_MAXIMGS:-1}" else configure_planealert "MASTODON_ACCESS_TOKEN" "" configure_planealert "MASTODON_SERVER" "" diff --git a/rootfs/usr/share/planefence/stage/planefence.config b/rootfs/usr/share/planefence/stage/planefence.config index 258772b..b854cab 100644 --- a/rootfs/usr/share/planefence/stage/planefence.config +++ b/rootfs/usr/share/planefence/stage/planefence.config @@ -378,7 +378,9 @@ NOTIFICATION_SERVER=planefence-notifier # MASTODON_SERVER contains the server name (please omit http://), for example MASTODON_SERVER=airwaves.social # MASTODON_ACCESS_TOKEN contains the Access Token of the Mastodon Application # PF/PA_PF_MASTODON_VISIBILITY can be `public`, `unlisted`, or `private`. -# See here for a detailed walk-through: https://github.com/kx1t/docker-planefence/blob/dev/README-Mastodon.md +# PA_MASTODON_MAXIMGS contains the max number of images uploaded to Mastodon. If you are using airwaves.social, +# please be considerate of (expensive) disk space charges for the owner and keep this to 1 +# See here for a detailed walk-through: https://github.com/kx1t/docker-planefence/blob/main/README-Mastodon.md # PF_MASTODON=OFF PA_MASTODON=OFF @@ -386,6 +388,7 @@ MASTODON_SERVER=airwaves.social MASTODON_ACCESS_TOKEN= PF_MASTODON_VISIBILITY=unlisted PA_MASTODON_VISIBILITY=unlisted +PA_MASTODON_MAXIMGS=1 # # # --------------------------------------------------------------------- From 85325ce2936fa87af22ea94d49ceb3577701fe8b Mon Sep 17 00:00:00 2001 From: kx1t Date: Thu, 18 Apr 2024 16:29:25 +0200 Subject: [PATCH 14/15] clean-up --- .../usr/share/planefence/prep-planefence.sh | 199 ++++++++---------- 1 file changed, 91 insertions(+), 108 deletions(-) diff --git a/rootfs/usr/share/planefence/prep-planefence.sh b/rootfs/usr/share/planefence/prep-planefence.sh index d9a404d..64a5126 100755 --- a/rootfs/usr/share/planefence/prep-planefence.sh +++ b/rootfs/usr/share/planefence/prep-planefence.sh @@ -1,5 +1,5 @@ #!/command/with-contenv bash -#shellcheck shell=bash disable=SC2015,SC2268,SC2174 +#shellcheck shell=bash disable=SC2015,SC2268,SC2174,SC1091,SC2154 # ----------------------------------------------------------------------------------- # Copyright 2020, 2021 Ramon F. Kolb - licensed under the terms and conditions # of GPLv3. The terms and conditions of this license are included with the Github @@ -11,14 +11,14 @@ # # ----------------------------------------------------------------------------------- # -APPNAME="$(hostname)/planefence" +source /scripts/common + REMOTEURL=$(sed -n 's/\(^\s*REMOTEURL=\)\(.*\)/\2/p' /usr/share/planefence/planefence.conf) function configure_planefence() { local SETTING_NAME="$1" local SETTING_VALUE="$2" - if [[ "x$SETTING_VALUE" != "x" ]] - then + if [[ -n "$SETTING_VALUE" ]]; then sed -i "s~\(^\s*${SETTING_NAME}=\).*~\1${SETTING_VALUE}~" /usr/share/planefence/planefence.conf else sed -i "s|\(^\s*${SETTING_NAME}=\).*|\1|" /usr/share/planefence/planefence.conf @@ -27,8 +27,7 @@ function configure_planefence() { function configure_planealert() { local SETTING_NAME="$1" local SETTING_VALUE="$2" - if [[ "x$SETTING_VALUE" != "x" ]] - then + if [[ -n "$SETTING_VALUE" ]]; then sed -i "s~\(^\s*${SETTING_NAME}=\).*~\1${SETTING_VALUE}~" /usr/share/plane-alert/plane-alert.conf else sed -i "s|\(^\s*${SETTING_NAME}=\).*|\1|" /usr/share/plane-alert/plane-alert.conf @@ -39,7 +38,7 @@ function configure_both() { configure_planealert "$1" "$2" } -[[ "$LOGLEVEL" != "ERROR" ]] && echo "[$APPNAME][$(date)] Running PlaneFence configuration - either the container is restarted or a config change was detected." || true +[[ "$LOGLEVEL" != "ERROR" ]] && "${s6wrap[@]}" echo" Running PlaneFence configuration - either the container is restarted or a config change was detected." || true # Sometimes, variables are passed in through .env in the Docker-compose directory # However, if there is a planefence.config file in the ..../persist directory # (by default exposed to ~/.planefence) then export all of those variables as well @@ -50,8 +49,7 @@ chmod -f a=rwx /usr/share/planefence/persist chmod -fR a+rw /usr/share/planefence/persist/{.[!.]*,*} chmod a=rwx /usr/share/planefence/persist/planepix -if [[ -f /usr/share/planefence/persist/planefence.config ]] -then +if [[ -f /usr/share/planefence/persist/planefence.config ]]; then set -o allexport source /usr/share/planefence/persist/planefence.config set +o allexport @@ -94,8 +92,7 @@ mkdir -p /usr/share/planefence/html/plane-alert cp -n /usr/share/plane-alert/plane-alert-db.txt /usr/share/planefence/persist # # LOOPTIME is the time between two runs of PlaneFence (in seconds) -if [[ "$PF_INTERVAL" != "" ]] -then +if [[ "$PF_INTERVAL" != "" ]]; then export LOOPTIME=$PF_INTERVAL else @@ -109,31 +106,26 @@ fi mkdir -p /run/planefence # ----------------------------------------------------------------------------------- # Do one last check. If FEEDER_LAT= empty or 90.12345, then the user obviously hasn't touched the config file. -if [[ "x$FEEDER_LAT" == "x" ]] || [[ "$FEEDER_LAT" == "90.12345" ]] -then +if [[ -z "$FEEDER_LAT" ]] || [[ "$FEEDER_LAT" == "90.12345" ]]; then sleep 10s - echo "[$APPNAME][$(date)] ----------------------------------------------------------" - echo "[$APPNAME][$(date)] !!! STOP !!!! You haven't configured FEEDER_LON and/or FEEDER_LAT for PlaneFence !!!!" - echo "[$APPNAME][$(date)] Planefence will not run unless you edit it configuration." - echo "[$APPNAME][$(date)] You can do this by pressing CTRL-c now and typing:" - echo "[$APPNAME][$(date)] sudo nano -l ~/.planefence/planefence.config" - echo "[$APPNAME][$(date)] Once done, restart the container and this message should disappear." - echo "[$APPNAME][$(date)] ----------------------------------------------------------" - while true - do - sleep 99999 - done + "${s6wrap[@]}" echo" ----------------------------------------------------------" + "${s6wrap[@]}" echo" !!! STOP !!!! You haven\'t configured FEEDER_LON and/or FEEDER_LAT for PlaneFence !!!!" + "${s6wrap[@]}" echo" Planefence will not run unless you edit it configuration." + "${s6wrap[@]}" echo" You can do this by pressing CTRL-c now and typing:" + "${s6wrap[@]}" echo" sudo nano -l ~/.planefence/planefence.config" + "${s6wrap[@]}" echo" Once done, restart the container and this message should disappear." + "${s6wrap[@]}" echo" ----------------------------------------------------------" + exec sleep infinity fi # # Set logging in planefence.conf: # -if [[ "$PF_LOG" == "off" ]] -then +if chk_disabled "$PF_LOG"; then export LOGFILE=/dev/null sed -i 's/\(^\s*VERBOSE=\).*/\1'""'/' /usr/share/planefence/planefence.conf else - [[ "x$PF_LOG" == "x" ]] && export LOGFILE="/tmp/planefence.log" || export LOGFILE="$PF_LOG" + [[ -z "$PF_LOG" ]] && export LOGFILE="/tmp/planefence.log" || export LOGFILE="$PF_LOG" fi # echo pflog=$PF_LOG and logfile=$LOGFILE sed -i 's|\(^\s*LOGFILE=\).*|\1'"$LOGFILE"'|' /usr/share/planefence/planefence.conf @@ -141,39 +133,39 @@ sed -i 's|\(^\s*LOGFILE=\).*|\1'"$LOGFILE"'|' /usr/share/planefence/planefence.c # ----------------------------------------------------------------------------------- # # read the environment variables and put them in the planefence.conf file: -[[ "x$FEEDER_LAT" != "x" ]] && sed -i 's/\(^\s*LAT=\).*/\1'"\"$FEEDER_LAT\""'/' /usr/share/planefence/planefence.conf || { echo "[$APPNAME][$(date)] Error - \$FEEDER_LAT ($FEEDER_LAT) not defined"; while :; do sleep 2073600; done; } -[[ "x$FEEDER_LONG" != "x" ]] && sed -i 's/\(^\s*LON=\).*/\1'"\"$FEEDER_LONG\""'/' /usr/share/planefence/planefence.conf || { echo "[$APPNAME][$(date)] Error - \$FEEDER_LONG not defined"; while :; do sleep 2073600; done; } -[[ "x$PF_MAXALT" != "x" ]] && sed -i 's/\(^\s*MAXALT=\).*/\1'"\"$PF_MAXALT\""'/' /usr/share/planefence/planefence.conf -[[ "x$PF_MAXDIST" != "x" ]] && sed -i 's/\(^\s*DIST=\).*/\1'"\"$PF_MAXDIST\""'/' /usr/share/planefence/planefence.conf -[[ "x$PF_ELEVATION" != "x" ]] && sed -i 's/\(^\s*ALTCORR=\).*/\1'"\"$PF_ELEVATION\""'/' /usr/share/planefence/planefence.conf -[[ "x$PF_NAME" != "x" ]] && sed -i 's/\(^\s*MY=\).*/\1'"\"$PF_NAME\""'/' /usr/share/planefence/planefence.conf || sed -i 's/\(^\s*MY=\).*/\1\"My\"/' /usr/share/planefence/planefence.conf -[[ "x$PF_TRACKSVC" != "x" ]] && sed -i 's|\(^\s*TRACKSERVICE=\).*|\1'"\"$PF_TRACKSVC\""'|' /usr/share/planefence/planefence.conf -[[ "x$PF_MAPURL" != "x" ]] && sed -i 's|\(^\s*MYURL=\).*|\1'"\"$PF_MAPURL\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*MYURL=\).*|\1|' /usr/share/planefence/planefence.conf -[[ "x$PF_NOISECAPT" != "x" ]] && sed -i 's|\(^\s*REMOTENOISE=\).*|\1'"\"$PF_NOISECAPT\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*REMOTENOISE=\).*|\1|' /usr/share/planefence/planefence.conf -[[ "x$PF_FUDGELOC" != "x" ]] && sed -i 's|\(^\s*FUDGELOC=\).*|\1'"\"$PF_FUDGELOC\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*FUDGELOC=\).*|\1|' /usr/share/planefence/planefence.conf -[[ "$PF_OPENAIP_LAYER" == "ON" ]] && sed -i 's|\(^\s*OPENAIP_LAYER=\).*|\1'"\"ON\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*OPENAIP_LAYER=\).*|\1'"\"OFF\""'|' /usr/share/planefence/planefence.conf -[[ "x$PF_TWEET_MINTIME" != "x" ]] && sed -i 's|\(^\s*TWEET_MINTIME=\).*|\1'"$PF_TWEET_MINTIME"'|' /usr/share/planefence/planefence.conf +[[ -n "$FEEDER_LAT" ]] && sed -i 's/\(^\s*LAT=\).*/\1'"\"$FEEDER_LAT\""'/' /usr/share/planefence/planefence.conf || { "${s6wrap[@]}" echo" Error - \$FEEDER_LAT ($FEEDER_LAT) not defined"; while :; do sleep 2073600; done; } +[[ -n "$FEEDER_LONG" ]] && sed -i 's/\(^\s*LON=\).*/\1'"\"$FEEDER_LONG\""'/' /usr/share/planefence/planefence.conf || { "${s6wrap[@]}" echo" Error - \$FEEDER_LONG not defined"; while :; do sleep 2073600; done; } +[[ -n "$PF_MAXALT" ]] && sed -i 's/\(^\s*MAXALT=\).*/\1'"\"$PF_MAXALT\""'/' /usr/share/planefence/planefence.conf +[[ -n "$PF_MAXDIST" ]] && sed -i 's/\(^\s*DIST=\).*/\1'"\"$PF_MAXDIST\""'/' /usr/share/planefence/planefence.conf +[[ -n "$PF_ELEVATION" ]] && sed -i 's/\(^\s*ALTCORR=\).*/\1'"\"$PF_ELEVATION\""'/' /usr/share/planefence/planefence.conf +[[ -n "$PF_NAME" ]] && sed -i 's/\(^\s*MY=\).*/\1'"\"$PF_NAME\""'/' /usr/share/planefence/planefence.conf || sed -i 's/\(^\s*MY=\).*/\1\"My\"/' /usr/share/planefence/planefence.conf +[[ -n "$PF_TRACKSVC" ]] && sed -i 's|\(^\s*TRACKSERVICE=\).*|\1'"\"$PF_TRACKSVC\""'|' /usr/share/planefence/planefence.conf +[[ -n "$PF_MAPURL" ]] && sed -i 's|\(^\s*MYURL=\).*|\1'"\"$PF_MAPURL\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*MYURL=\).*|\1|' /usr/share/planefence/planefence.conf +[[ -n "$PF_NOISECAPT" ]] && sed -i 's|\(^\s*REMOTENOISE=\).*|\1'"\"$PF_NOISECAPT\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*REMOTENOISE=\).*|\1|' /usr/share/planefence/planefence.conf +[[ -n "$PF_FUDGELOC" ]] && sed -i 's|\(^\s*FUDGELOC=\).*|\1'"\"$PF_FUDGELOC\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*FUDGELOC=\).*|\1|' /usr/share/planefence/planefence.conf +chk_enabled "$PF_OPENAIP_LAYER" && sed -i 's|\(^\s*OPENAIP_LAYER=\).*|\1'"\"ON\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*OPENAIP_LAYER=\).*|\1'"\"OFF\""'|' /usr/share/planefence/planefence.conf +[[ -n "$PF_TWEET_MINTIME" ]] && sed -i 's|\(^\s*TWEET_MINTIME=\).*|\1'"$PF_TWEET_MINTIME"'|' /usr/share/planefence/planefence.conf [[ "$PF_TWEET_BEHAVIOR" == "PRE" ]] && sed -i 's|\(^\s*TWEET_BEHAVIOR=\).*|\1PRE|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*TWEET_BEHAVIOR=\).*|\1POST|' /usr/share/planefence/planefence.conf -[[ "$PF_PLANEALERT" == "ON" ]] && sed -i 's|\(^\s*PA_LINK=\).*|\1\"'"$PF_PA_LINK"'\"|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*PA_LINK=\).*|\1|' /usr/share/planefence/planefence.conf -[[ "$PF_TWEETEVERY" == "true" ]] && sed -i 's|\(^\s*TWEETEVERY=\).*|\1true|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*TWEETEVERY=\).*|\1false|' /usr/share/planefence/planefence.conf -[[ "x$PA_HISTTIME" != "x" ]] && sed -i 's|\(^\s*HISTTIME=\).*|\1\"'"$PA_HISTTIME"'\"|' /usr/share/plane-alert/plane-alert.conf -[[ "x$PF_ALERTHEADER" != "x" ]] && sed -i "s|\(^\s*ALERTHEADER=\).*|\1\'$PF_ALERTHEADER\'|" /usr/share/plane-alert/plane-alert.conf +chk_enabled "$PF_PLANEALERT" && sed -i 's|\(^\s*PA_LINK=\).*|\1\"'"$PF_PA_LINK"'\"|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*PA_LINK=\).*|\1|' /usr/share/planefence/planefence.conf +chk_enabled "$PF_TWEETEVERY" && sed -i 's|\(^\s*TWEETEVERY=\).*|\1true|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*TWEETEVERY=\).*|\1false|' /usr/share/planefence/planefence.conf +[[ -n "$PA_HISTTIME" ]] && sed -i 's|\(^\s*HISTTIME=\).*|\1\"'"$PA_HISTTIME"'\"|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_ALERTHEADER" ]] && sed -i "s|\(^\s*ALERTHEADER=\).*|\1\'$PF_ALERTHEADER\'|" /usr/share/plane-alert/plane-alert.conf -if [[ "x$PF_SOCK30003HOST" != "x" ]] -then +if [[ -n "$PF_SOCK30003HOST" ]]; then + # shellcheck disable=SC2001 a=$(sed 's|\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)|\1\_\2\_\3\_\4|g' <<< "$PF_SOCK30003HOST") sed -i 's|\(^\s*LOGFILEBASE=/run/socket30003/dump1090-\).*|\1'"$a"'-|' /usr/share/planefence/planefence.conf sed -i 's/127_0_0_1/'"$a"'/' /usr/share/planefence/planeheat.sh unset a else sleep 10s - echo "[$APPNAME][$(date)] ----------------------------------------------------------" - echo "[$APPNAME][$(date)] !!! STOP !!!! You haven't configured PF_SOCK30003HOST for PlaneFence !!!!" - echo "[$APPNAME][$(date)] Planefence will not run unless you edit it configuration." - echo "[$APPNAME][$(date)] You can do this by pressing CTRL-c now and typing:" - echo "[$APPNAME][$(date)] sudo nano -l ~/.planefence/planefence.config" - echo "[$APPNAME][$(date)] Once done, restart the container and this message should disappear." - echo "[$APPNAME][$(date)] ----------------------------------------------------------" + "${s6wrap[@]}" echo" ----------------------------------------------------------" + "${s6wrap[@]}" echo" !!! STOP !!!! You haven't configured PF_SOCK30003HOST for PlaneFence !!!!" + "${s6wrap[@]}" echo" Planefence will not run unless you edit it configuration." + "${s6wrap[@]}" echo" You can do this by pressing CTRL-c now and typing:" + "${s6wrap[@]}" echo" sudo nano -l ~/.planefence/planefence.config" + "${s6wrap[@]}" echo" Once done, restart the container and this message should disappear." + "${s6wrap[@]}" echo" ----------------------------------------------------------" while true do sleep 99999 @@ -181,10 +173,10 @@ else fi # # Deal with duplicates. Put IGNOREDUPES in its place and create (or delete) the link to the ignorelist: -[[ "x$PF_IGNOREDUPES" != "x" ]] && sed -i 's|\(^\s*IGNOREDUPES=\).*|\1ON|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*IGNOREDUPES=\).*|\1OFF|' /usr/share/planefence/planefence.conf -[[ "x$PF_COLLAPSEWITHIN" != "x" ]] && sed -i 's|\(^\s*COLLAPSEWITHIN=\).*|\1'"$PF_COLLAPSEWITHIN"'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*IGNOREDUPES=\).*|\1300|' /usr/share/planefence/planefence.conf -a=$(sed -n 's/^\s*IGNORELIST=\(.*\)/\1/p' /usr/share/planefence/planefence.conf | sed 's/\"//g') -[[ "$a" != "" ]] && ln -sf $a /usr/share/planefence/html/ignorelist.txt || rm -f /usr/share/planefence/html/ignorelist.txt +[[ -n "$PF_IGNOREDUPES" ]] && sed -i 's|\(^\s*IGNOREDUPES=\).*|\1ON|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*IGNOREDUPES=\).*|\1OFF|' /usr/share/planefence/planefence.conf +[[ -n "$PF_COLLAPSEWITHIN" ]] && sed -i 's|\(^\s*COLLAPSEWITHIN=\).*|\1'"$PF_COLLAPSEWITHIN"'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*IGNOREDUPES=\).*|\1300|' /usr/share/planefence/planefence.conf +a="$(sed -n 's/^\s*IGNORELIST=\(.*\)/\1/p' /usr/share/planefence/planefence.conf | sed 's/\"//g')" +[[ -n "$a" ]] && ln -sf "$a" /usr/share/planefence/html/ignorelist.txt || rm -f /usr/share/planefence/html/ignorelist.txt unset a # # ----------------------------------------------------------------------------------- @@ -193,8 +185,8 @@ unset a # sed -i 's/\(^\s*LAT=\).*/\1'"\"$FEEDER_LAT\""'/' /usr/share/planefence/planeheat.sh sed -i 's/\(^\s*LON=\).*/\1'"\"$FEEDER_LONG\""'/' /usr/share/planefence/planeheat.sh -[[ "x$PF_MAXALT" != "x" ]] && sed -i 's/\(^\s*MAXALT=\).*/\1'"\"$PF_MAXALT\""'/' /usr/share/planefence/planeheat.sh -[[ "x$PF_MAXDIST" != "x" ]] && sed -i 's/\(^\s*DIST=\).*/\1'"\"$PF_MAXDIST\""'/' /usr/share/planefence/planeheat.sh +[[ -n "$PF_MAXALT" ]] && sed -i 's/\(^\s*MAXALT=\).*/\1'"\"$PF_MAXALT\""'/' /usr/share/planefence/planeheat.sh +[[ -n "$PF_MAXDIST" ]] && sed -i 's/\(^\s*DIST=\).*/\1'"\"$PF_MAXDIST\""'/' /usr/share/planefence/planeheat.sh # ----------------------------------------------------------------------------------- # @@ -211,74 +203,68 @@ sed -i 's/\(^\s*LON=\).*/\1'"\"$FEEDER_LONG\""'/' /usr/share/planefence/planehea # # enable or disable tweeting: # -[[ "${PF_TWEET,,}" == "off" ]] && sed -i 's/\(^\s*PLANETWEET=\).*/\1/' /usr/share/planefence/planefence.conf -if [[ "${PF_TWEET,,}" == "on" ]] -then - if [[ ! -f ~/.twurlrc ]] - then - echo "[$APPNAME][$(date)] Warning: PF_TWEET is set to ON in .env file, but the Twitter account is not configured." - echo "[$APPNAME][$(date)] Sign up for a developer account at Twitter, create an app, and get a Consumer Key / Secret." - echo "[$APPNAME][$(date)] Then run this from the host machine: \"docker exec -it planefence /root/config_tweeting.sh\"" - echo "[$APPNAME][$(date)] For more information on how to sign up for a Twitter Developer Account, see this link:" - echo "[$APPNAME][$(date)] https://elfsight.com/blog/2020/03/how-to-get-twitter-api-key/" - echo "[$APPNAME][$(date)] PlaneFence will continue to start without Twitter functionality." +chk_disabled "${PF_TWEET}" && sed -i 's/\(^\s*PLANETWEET=\).*/\1/' /usr/share/planefence/planefence.conf +if chk_enabled "${PF_TWEET,,}"; then + if [[ ! -f ~/.twurlrc ]]; then + "${s6wrap[@]}" echo" Warning: PF_TWEET is set to ON in .env file, but the Twitter account is not configured." + "${s6wrap[@]}" echo" Sign up for a developer account at Twitter, create an app, and get a Consumer Key / Secret." + "${s6wrap[@]}" echo" Then run this from the host machine: \"docker exec -it planefence /root/config_tweeting.sh\"" + "${s6wrap[@]}" echo" For more information on how to sign up for a Twitter Developer Account, see this link:" + "${s6wrap[@]}" echo" https://elfsight.com/blog/2020/03/how-to-get-twitter-api-key/" + "${s6wrap[@]}" echo" PlaneFence will continue to start without Twitter functionality." sed -i 's/\(^\s*PLANETWEET=\).*/\1/' /usr/share/planefence/planefence.conf else sed -i 's|\(^\s*PLANETWEET=\).*|\1'"$(sed -n '/profiles:/{n;p;}' /root/.twurlrc | tr -d '[:blank:][=:=]')"'|' /usr/share/planefence/planefence.conf - [[ "x$PF_TWATTRIB" != "x" ]] && sed -i 's|\(^\s*ATTRIB=\).*|\1'"\"$PF_TWATTRIB\""'|' /usr/share/planefence/planefence.conf + [[ -n "$PF_TWATTRIB" ]] && sed -i 's|\(^\s*ATTRIB=\).*|\1'"\"$PF_TWATTRIB\""'|' /usr/share/planefence/planefence.conf fi fi # ----------------------------------------------------------------------------------- # # Change the heatmap height and width if they are defined in the .env parameter file: -[[ "x$PF_MAPHEIGHT" != "x" ]] && sed -i 's|\(^\s*HEATMAPHEIGHT=\).*|\1'"\"$PF_MAPHEIGHT\""'|' /usr/share/planefence/planefence.conf -[[ "x$PF_MAPWIDTH" != "x" ]] && sed -i 's|\(^\s*HEATMAPWIDTH=\).*|\1'"\"$PF_MAPWIDTH\""'|' /usr/share/planefence/planefence.conf -[[ "x$PF_MAPZOOM" != "x" ]] && sed -i 's|\(^\s*HEATMAPZOOM=\).*|\1'"\"$PF_MAPZOOM\""'|' /usr/share/planefence/planefence.conf +[[ -n "$PF_MAPHEIGHT" ]] && sed -i 's|\(^\s*HEATMAPHEIGHT=\).*|\1'"\"$PF_MAPHEIGHT\""'|' /usr/share/planefence/planefence.conf +[[ -n "$PF_MAPWIDTH" ]] && sed -i 's|\(^\s*HEATMAPWIDTH=\).*|\1'"\"$PF_MAPWIDTH\""'|' /usr/share/planefence/planefence.conf +[[ -n "$PF_MAPZOOM" ]] && sed -i 's|\(^\s*HEATMAPZOOM=\).*|\1'"\"$PF_MAPZOOM\""'|' /usr/share/planefence/planefence.conf # # Also do this for files in the past -- /usr/share/planefence/html/planefence-??????.html -if find /usr/share/planefence/html/planefence-??????.html >/dev/null 2>&1 -then +if find /usr/share/planefence/html/planefence-??????.html >/dev/null 2>&1; then for i in /usr/share/planefence/html/planefence-??????.html do - [[ "x$PF_MAPWIDTH" != "x" ]] && sed -i 's|\(^\s*
    ~/.planefence/plane-alert-db.txt" - echo "[$APPNAME][$(date)] and then restart this docker container" +if [[ ! -f /usr/share/planefence/persist/plane-alert-db.txt ]] && chk_enabled "$PF_PLANEALERT"; then + "${s6wrap[@]}" echo" Cannot find or create the plane-alert-db.txt file. Disabling Plane-Alert." + "${s6wrap[@]}" echo" Do this on the host to get a base file:" + "${s6wrap[@]}" echo" curl --compressed -s https://raw.githubusercontent.com/kx1t/docker-planefence/plane-alert/plane-alert-db.txt >~/.planefence/plane-alert-db.txt" + "${s6wrap[@]}" echo" and then restart this docker container" PF_PLANEALERT="OFF" fi # make sure $PLANEALERT is set to ON in the planefence.conf file, so it will be invoked: -[[ "$PF_PLANEALERT" == "ON" ]] && sed -i 's|\(^\s*PLANEALERT=\).*|\1'"\"ON\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*PLANEALERT=\).*|\1'"\"OFF\""'|' /usr/share/planefence/planefence.conf +chk_enabled "$PF_PLANEALERT" && sed -i 's|\(^\s*PLANEALERT=\).*|\1'"\"ON\""'|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*PLANEALERT=\).*|\1'"\"OFF\""'|' /usr/share/planefence/planefence.conf # Go get the plane-alert-db files: /usr/share/plane-alert/get-pa-alertlist.sh /usr/share/plane-alert/get-silhouettes.sh # Now make sure that the file containing the twitter IDs is rewritten with 1 ID per line -[[ "x$PF_PA_TWID" != "x" ]] && tr , "\n" <<< "$PF_PA_TWID" > /usr/share/plane-alert/plane-alert.twitterid || rm -f /usr/share/plane-alert/plane-alert.twitterid +[[ -n "$PF_PA_TWID" ]] && tr , "\n" <<< "$PF_PA_TWID" > /usr/share/plane-alert/plane-alert.twitterid || rm -f /usr/share/plane-alert/plane-alert.twitterid # and write the rest of the parameters into their place -[[ "x$PF_PA_TWID" != "x" ]] && [[ "$PF_PA_TWEET" == "DM" ]] && sed -i 's|\(^\s*TWITTER=\).*|\1DM|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*TWITTER=\).*|\1false|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_PA_TWID" ]] && [[ "$PF_PA_TWEET" == "DM" ]] && sed -i 's|\(^\s*TWITTER=\).*|\1DM|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*TWITTER=\).*|\1false|' /usr/share/plane-alert/plane-alert.conf [[ "$PF_PA_TWEET" == "TWEET" ]] && sed -i 's|\(^\s*TWITTER=\).*|\1TWEET|' /usr/share/plane-alert/plane-alert.conf [[ "$PF_PA_TWEET" != "TWEET" ]] && [[ "$PF_PA_TWEET" != "DM" ]] && sed -i 's|\(^\s*TWITTER=\).*|\1false|' /usr/share/plane-alert/plane-alert.conf configure_planefence "PF_DISCORD" "$PF_DISCORD" @@ -293,8 +279,7 @@ configure_both "NOTIFICATION_SERVER" "\"NOTIFICATION_SERVER\"" configure_planefence "OPENAIPKEY" "$PF_OPENAIPKEY" # Configure Mastodon parameters: -if [[ -n "$MASTODON_SERVER" ]] && [[ -n "$MASTODON_ACCESS_TOKEN" ]] -then +if [[ -n "$MASTODON_SERVER" ]] && [[ -n "$MASTODON_ACCESS_TOKEN" ]]; then MASTODON_SERVER="${MASTODON_SERVER,,}" # strip http:// https:// [[ "${MASTODON_SERVER:0:7}" == "http://" ]] && MASTODON_SERVER="${MASTODON_SERVER:7}" || true @@ -307,8 +292,7 @@ then configure_planefence "MASTODON_ACCESS_TOKEN" "" configure_planefence "MASTODON_SERVER" "" fi - if chk_enabled "${PA_MASTODON,,}" - then + if chk_enabled "${PA_MASTODON,,}"; then configure_planealert "MASTODON_ACCESS_TOKEN" "$MASTODON_ACCESS_TOKEN" configure_planealert "MASTODON_SERVER" "$MASTODON_SERVER" configure_planealert "MASTODON_VISIBILITY" "${PA_MASTODON_VISIBILITY:-unlisted}" @@ -319,11 +303,11 @@ then fi fi -[[ "x$PF_NAME" != "x" ]] && sed -i 's|\(^\s*NAME=\).*|\1'"\"$PF_NAME\""'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*NAME=\).*|\1My|' /usr/share/plane-alert/plane-alert.conf -[[ "x$PF_MAPURL" != "x" ]] && sed -i 's|\(^\s*ADSBLINK=\).*|\1'"\"$PF_MAPURL\""'|' /usr/share/plane-alert/plane-alert.conf -# removed for now - hardcoding PlaneAlert map zoom to 7 in plane-alert.conf: [[ "x$PF_MAPZOOM" != "x" ]] && sed -i 's|\(^\s*MAPZOOM=\).*|\1'"\"$PF_MAPZOOM\""'|' /usr/share/plane-alert/plane-alert.conf -[[ "x$PF_PARANGE" != "x" ]] && sed -i 's|\(^\s*RANGE=\).*|\1'"$PF_PARANGE"'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*RANGE=\).*|\1999999|' /usr/share/plane-alert/plane-alert.conf -[[ "x$PF_PA_SQUAWKS" != "x" ]] && sed -i 's|\(^\s*SQUAWKS=\).*|\1'"$PF_PA_SQUAWKS"'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*SQUAWKS=\).*|\1|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_NAME" ]] && sed -i 's|\(^\s*NAME=\).*|\1'"\"$PF_NAME\""'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*NAME=\).*|\1My|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_MAPURL" ]] && sed -i 's|\(^\s*ADSBLINK=\).*|\1'"\"$PF_MAPURL\""'|' /usr/share/plane-alert/plane-alert.conf +# removed for now - hardcoding PlaneAlert map zoom to 7 in plane-alert.conf: [[ -n "$PF_MAPZOOM" ]] && sed -i 's|\(^\s*MAPZOOM=\).*|\1'"\"$PF_MAPZOOM\""'|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_PARANGE" ]] && sed -i 's|\(^\s*RANGE=\).*|\1'"$PF_PARANGE"'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*RANGE=\).*|\1999999|' /usr/share/plane-alert/plane-alert.conf +[[ -n "$PF_PA_SQUAWKS" ]] && sed -i 's|\(^\s*SQUAWKS=\).*|\1'"$PF_PA_SQUAWKS"'|' /usr/share/plane-alert/plane-alert.conf || sed -i 's|\(^\s*SQUAWKS=\).*|\1|' /usr/share/plane-alert/plane-alert.conf configure_both "AUTOREFRESH" "${PF_AUTOREFRESH,,}" @@ -337,18 +321,17 @@ cp -f /usr/share/planefence/stage/sort-table.js /usr/share/planefence/html/plane sleep 1 if [[ "$PF_DISTUNIT" != $(sed -n 's/^\s*distanceunit=\(.*\)/\1/p' /usr/share/socket30003/socket30003.cfg) ]] \ || [[ "$PF_ALTUNIT" != $(sed -n 's/^\s*altitudeunit=\(.*\)/\1/p' /usr/share/socket30003/socket30003.cfg) ]] \ - || [[ "$PF_SPEEDUNIT" != $(sed -n 's/^\s*speedunit=\(.*\)/\1/p' /usr/share/socket30003/socket30003.cfg) ]] -then - [[ "x$PF_DISTUNIT" != "x" ]] && sed -i 's/\(^\s*distanceunit=\).*/\1'"$PF_DISTUNIT"'/' /usr/share/socket30003/socket30003.cfg - [[ "x$PF_SPEEDUNIT" != "x" ]] && sed -i 's/\(^\s*speedunit=\).*/\1'"$PF_SPEEDUNIT"'/' /usr/share/socket30003/socket30003.cfg - [[ "x$PF_ALTUNIT" != "x" ]] && sed -i 's/\(^\s*altitudeunit=\).*/\1'"$PF_ALTUNIT"'/' /usr/share/socket30003/socket30003.cfg + || [[ "$PF_SPEEDUNIT" != $(sed -n 's/^\s*speedunit=\(.*\)/\1/p' /usr/share/socket30003/socket30003.cfg) ]]; then + [[ -n "$PF_DISTUNIT" ]] && sed -i 's/\(^\s*distanceunit=\).*/\1'"$PF_DISTUNIT"'/' /usr/share/socket30003/socket30003.cfg + [[ -n "$PF_SPEEDUNIT" ]] && sed -i 's/\(^\s*speedunit=\).*/\1'"$PF_SPEEDUNIT"'/' /usr/share/socket30003/socket30003.cfg + [[ -n "$PF_ALTUNIT" ]] && sed -i 's/\(^\s*altitudeunit=\).*/\1'"$PF_ALTUNIT"'/' /usr/share/socket30003/socket30003.cfg fi # #-------------------------------------------------------------------------------- # Check if the remote airlinename server is online #[[ "$PF_CHECKREMOTEDB" != "OFF" ]] && a="$(curl -L -s https://get-airline.planefence.com/?flight=hello_from_$(grep 'PF_NAME' /usr/share/planefence/persist/planefence.config | awk -F '=' '{ print $2 }' | tr -dc '[:alnum:]')_bld_$([[ -f /usr/share/planefence/build ]] && cat /usr/share/planefence/build || cat /root/.buildtime | cut -c 1-23 | tr ' ' '_'))" || a="" #shellcheck disable=SC2046 -[[ "$PF_CHECKREMOTEDB" != "OFF" ]] && a="$(curl -L -s $REMOTEURL/?flight=hello_from_$(grep 'PF_NAME' /usr/share/planefence/persist/planefence.config | awk -F '=' '{ print $2 }' | tr -dc '[:alnum:]')_bld_$([[ -f /usr/share/planefence/branch ]] && cat /usr/share/planefence/branch || cat /root/.buildtime))" || a="" +! chk_disabled "$PF_CHECKREMOTEDB" && a="$(curl -L -s "$REMOTEURL"/?flight=hello_from_$(grep 'PF_NAME' /usr/share/planefence/persist/planefence.config | awk -F '=' '{ print $2 }' | tr -dc '[:alnum:]')_bld_$([[ -f /usr/share/planefence/branch ]] && cat /usr/share/planefence/branch || cat /root/.buildtime))" || a="" [[ "${a:0:4}" == "#100" ]] && sed -i 's|\(^\s*CHECKREMOTEDB=\).*|\1ON|' /usr/share/planefence/planefence.conf || sed -i 's|\(^\s*CHECKREMOTEDB=\).*|\1OFF|' /usr/share/planefence/planefence.conf # #-------------------------------------------------------------------------------- @@ -361,8 +344,8 @@ fi # if curl -L -s https://raw.githubusercontent.com/sdr-enthusiasts/plane-alert-db/main/planepix.txt > /usr/share/planefence/persist/planepix.txt.samplefile # then # chmod a+r /usr/share/planefence/persist/planepix.txt.samplefile -# echo "[$APPNAME][$(date)] Successfully downloaded planepix sample file to ~/.planefence/planepix.txt.samplefile directory." -# echo "[$APPNAME][$(date)] To use it, rename it to, or incorporate it into ~/.planefence/planepix.txt. Any entries in this file will replace the tar1090 screenshot with a picture of the plane." +# "${s6wrap[@]}" echo" Successfully downloaded planepix sample file to ~/.planefence/planepix.txt.samplefile directory." +# "${s6wrap[@]}" echo" To use it, rename it to, or incorporate it into ~/.planefence/planepix.txt. Any entries in this file will replace the tar1090 screenshot with a picture of the plane." # fi #-------------------------------------------------------------------------------- # Put the MOTDs in place: From 5c1f9ebc03de6dd1a87db2824707dc880f406979 Mon Sep 17 00:00:00 2001 From: kx1t Date: Wed, 24 Apr 2024 17:31:14 -0400 Subject: [PATCH 15/15] add basics for mastodon toot expiration --- README-Mastodon.md | 2 + rootfs/scripts/masto_expire.sh | 73 +++++++++++++++++++ rootfs/usr/share/plane-alert/plane-alert.conf | 1 + .../usr/share/planefence/prep-planefence.sh | 1 + .../share/planefence/stage/planefence.config | 1 + 5 files changed, 78 insertions(+) create mode 100755 rootfs/scripts/masto_expire.sh diff --git a/README-Mastodon.md b/README-Mastodon.md index 12dab96..0cc3bb3 100644 --- a/README-Mastodon.md +++ b/README-Mastodon.md @@ -59,12 +59,14 @@ PF_MASTODON=ON PA_MASTODON=ON PA_MASTODON_VISIBILITY=unlisted PF_MASTODON_VISIBILITY=unlisted +MASTODON_RETENTION_TIME=7 ``` Replace by the applicable server name and access token. If `PF_MASTODON` is not set to `ON`, then no PlaneFence Mastodon notifications will be sent. If `PA_MASTODON` is not set to `ON`, then no Plane-Alert Mastodon notifications will be sent. `Px_MASTODON_VISIBILITY` can be `public`, `unlisted`, or `private` +`MASTODON_RETENTION_TIME` is the retention time of any messages, in days (not yet enforced, future functionality) ## Summary of License Terms diff --git a/rootfs/scripts/masto_expire.sh b/rootfs/scripts/masto_expire.sh new file mode 100755 index 0000000..79d2300 --- /dev/null +++ b/rootfs/scripts/masto_expire.sh @@ -0,0 +1,73 @@ +#!/command/with-contenv bash +#shellcheck shell=bash disable=SC1091,SC2174 +# ----------------------------------------------------------------------------------- +# Copyright 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/docker-planefence/ +# +# This package may incorporate other software and license terms. +# ----------------------------------------------------------------------------------- + +if [[ -f /usr/share/planefence/persist/planefence.config ]]; then source /usr/share/planefence/persist/planefence.config; fi + +ACCESS_TOKEN=$MASTODON_ACCESS_TOKEN +INSTANCE_URL="https://$MASTODON_SERVER" + +RETENTION_DAYS="${MASTODON_RETENTION_TIME}" + +delete_toot() { + local toot_id="$1" + local result + if result="$(curl -s --fail -X DELETE -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/statuses/$toot_id" 2>&1)"; then + echo "successfully deleted" + else + echo "error: $result" + fi +} + +if [[ -z "$RETENTION_DAYS" ]]; then + echo "RETENTION_DAYS not set. Exiting." + exit 1 +fi + +unset toot_dates counter last_id +declare -A toot_dates + +now="$(date +%s)" + +masto_id="$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/accounts/verify_credentials" | jq -r '.id')" + +while : ; do + echo -n "Indexing Media IDs round $((++counter))" + toots="$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "$INSTANCE_URL/api/v1/accounts/$masto_id/statuses?limit=40${last_id:+&max_id=}${last_id}")" + # shellcheck disable=SC2207 + toot_ids=($(jq -r '.[] | .id' <<< "$toots" 2>/dev/null)) + if (( ${#toot_ids[@]} == 0)); then + echo "No more toots, we are done!" + exit + fi + last_id="${toot_ids[-1]}" + echo " ${#toot_ids[@]} toots" + for t in "${toot_ids[@]}"; do + if [[ -z "${toot_dates[$t]}" ]]; then + toot_dates[$t]="$(date -d "$(jq -r 'map(select(.id == "'"$t"'"))[].created_at' <<< "$toots")" +%s)" + echo -n "$t --> $(date -d @"${toot_dates[$t]}") " + if (( (now - toot_dates[$t])/(60*60*24) > RETENTION_DAYS )); then + echo -n " expired (age: $(( (now - toot_dates[$t])/(60*60*24) )) days): " + if [[ "$1" == "delete" ]]; then + echo -n "deleting... " + delete_toot "$t"; + else + echo "(not deleted)" + fi + else + echo " not expired (age: $(( (now + - toot_dates[$t])/(60*60*24) )) days)" + fi + else + echo "$t --> duplicate, we're done!" + exit + fi + done +done \ No newline at end of file diff --git a/rootfs/usr/share/plane-alert/plane-alert.conf b/rootfs/usr/share/plane-alert/plane-alert.conf index 45eda19..b1e5b93 100644 --- a/rootfs/usr/share/plane-alert/plane-alert.conf +++ b/rootfs/usr/share/plane-alert/plane-alert.conf @@ -139,6 +139,7 @@ set +a MASTODON_ACCESS_TOKEN= MASTODON_VISIBILITY=unlisted MASTODON_MAXIMGS=1 + MASTODON_RETENTION_TIME= # # AUTOREFRESH controls if the web page auto-refreshed. The refresh interval is the same as $PFINTERVAL. diff --git a/rootfs/usr/share/planefence/prep-planefence.sh b/rootfs/usr/share/planefence/prep-planefence.sh index 64a5126..84fa0c5 100755 --- a/rootfs/usr/share/planefence/prep-planefence.sh +++ b/rootfs/usr/share/planefence/prep-planefence.sh @@ -297,6 +297,7 @@ if [[ -n "$MASTODON_SERVER" ]] && [[ -n "$MASTODON_ACCESS_TOKEN" ]]; then configure_planealert "MASTODON_SERVER" "$MASTODON_SERVER" configure_planealert "MASTODON_VISIBILITY" "${PA_MASTODON_VISIBILITY:-unlisted}" configure_planealert "MASTODON_MAXIMGS" "${PA_MASTODON_MAXIMGS:-1}" + configure_planealert "MASTODON_RETENTION_TIME" "${MASTODON_RETENTION_TIME:-7}" else configure_planealert "MASTODON_ACCESS_TOKEN" "" configure_planealert "MASTODON_SERVER" "" diff --git a/rootfs/usr/share/planefence/stage/planefence.config b/rootfs/usr/share/planefence/stage/planefence.config index b854cab..45c5c00 100644 --- a/rootfs/usr/share/planefence/stage/planefence.config +++ b/rootfs/usr/share/planefence/stage/planefence.config @@ -389,6 +389,7 @@ MASTODON_ACCESS_TOKEN= PF_MASTODON_VISIBILITY=unlisted PA_MASTODON_VISIBILITY=unlisted PA_MASTODON_MAXIMGS=1 +MASTODON_RETENTION_TIME=7 # # # ---------------------------------------------------------------------