#!/usr/bin/with-contenv bash # shellcheck shell=bash low="${READSB_AUTOGAIN_LOW_PCT:-0.5}" high="${READSB_AUTOGAIN_HIGH_PCT:-7.0}" ga=(0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6 -10) tmp=/var/globe_history/autogain mkdir -p $tmp mkdir -p /var/globe_history/autogain #work around stupid locale stuff export LC_ALL=C APP=dump1090-fa if [[ -f /run/dump1090-fa/stats.json ]]; then APP=dump1090-fa elif [[ -f /run/readsb/stats.json ]]; then APP=readsb fi stats=/run/$APP/stats.json if [[ "$1" == "reset" ]]; then echo "Reset AutoGain - restarting with initial values and initialization process" rm -f /var/globe_history/autogain/* >/dev/null 2>&1 pkill -f "s6-supervise autogain" exit 0 fi if ! [[ -f $stats ]]; then echo "stats.json not found, is the decoder running?"; exit 1; fi oldstrong=$(cat $tmp/strong 2>/dev/null) oldtotal=$(cat $tmp/total 2>/dev/null) if [[ -z $oldstrong ]] || [[ -z $oldtotal ]]; then oldstrong=0 oldtotal=0 fi if ! grep -qs total $stats | grep -qs -e strong_signals $stats; then echo "the decoder doesn't seem to be using an rtl-sdr device, can't help with that." exit 1 fi # start=$(jq '.total.start' < $stats) # end=$(jq '.total.end' < $stats) strong=$(jq '.total.local.strong_signals' < $stats | tee $tmp/strong) total=$(jq '.total.local.accepted | add' < $stats | tee $tmp/total) if [[ -z $strong ]] || [[ -z $total ]]; then echo "unrecognized format: $stats" exit 1 fi if ! awk "BEGIN{ exit ($total < 1000) }"; then echo "The decoder hasn't been running long enough, wait a bit!" exit 0 fi if (( oldtotal > total )) || (( oldstrong > strong )) || (( oldtotal == total )); then oldstrong=0 oldtotal=0 fi strong=$((strong - oldstrong)) total=$((total - oldtotal)) if [[ $total == 0 ]]; then percent=0 else percent=$(awk "BEGIN {printf \"%.3f\", $strong * 100 / $total}") fi strong=$percent if [[ $strong == "nan" ]]; then echo "Error, can't automatically adjust gain!"; exit 1; fi # Get the gain -- updated for docker-tar1090 use by kx1t if [[ ! -f /var/globe_history/autogain/gain ]]; then oldgain="49.6" echo "Initial run. Starting point for adjusting gain is $oldgain" else read -r oldgain < /var/globe_history/autogain/gain oldgain="${oldgain:-49.6}" # needed for stupidity reasons fi gain_index=28 for i in "${!ga[@]}"; do if ! awk "BEGIN{ exit (${oldgain} <= ${ga[$i]}) }"; then gain_index="${i}" break fi done if ! awk "BEGIN{ exit (${oldgain} > 49.6) }"; then gain_index=28 fi if [[ "$oldgain" == "-10" ]]; then gain_index=28 fi if ! awk "BEGIN{ exit ($strong > $low) }" && ! awk "BEGIN{ exit ($strong < $high) }"; then echo "No gain change needed, ${strong}% percentage of messages >-3dB is in nominal range. (${low}% < ${strong}% < ${high}%)" exit 0 fi if ! awk "BEGIN{ exit ($strong < $low) }" && [[ $gain_index == 28 ]]; then echo "Could have used some more gain, but gain is already at maximum! Strong (>-3dB) messages ${strong}% < ${low}%" exit 0 fi if ! awk "BEGIN{ exit ($strong < $low) }"; then gain_index=$((gain_index+1)) action=Increasing fi if ! awk "BEGIN{ exit ($strong > $high) }" && [[ $gain_index == 0 ]]; then echo "Could have used some lower gain, but gain already at minimum! Strong (>-3dB) messages ${strong}% > ${high}%" exit 0 fi if ! awk "BEGIN{ exit ($strong > $high) }"; then gain_index=$((gain_index-1)) action=Decreasing fi gain="${ga[$gain_index]}" if [[ $gain == "" ]] || [[ $gain == "-10" ]]; then echo "Could have used some more gain, but gain is already at maximum! Strong (>-3dB) messages ${strong}% < ${low}%" exit 0 fi # Set the gain -- updated for docker-tar1090 use by kx1t echo "$gain" > /var/globe_history/autogain/gain pkill readsb #reset numbers echo 0 > $tmp/strong echo 0 > $tmp/total if [[ "$action" == "Increasing" ]]; then echo "Increasing gain to $gain (${strong}% messages >-3dB exceed lower boundary of ${low}%)" elif [[ "$action" == "Decreasing" ]]; then echo "Decreasing gain to $gain (${strong}% messages >-3dB exceed upper boundary of ${high}%)" fi