forked from wiedehopf/adsb-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autogain-install.sh
executable file
·240 lines (182 loc) · 6.12 KB
/
autogain-install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
umask 022
cd /tmp
# remove old version:
rm -f /usr/local/bin/dump1090-fa-autogain
rm -f /etc/default/dump1090-fa-autogain
rm -f /etc/cron.d/dump1090-fa-autogain
systemctl disable dump1090-fa-autogain.timer &>/dev/null
systemctl stop dump1090-fa-autogain.timer &>/dev/null
rm -f /lib/systemd/system/dump1090-fa-autogain.service
rm -f /lib/systemd/system/dump1090-fa-autogain.timer
if ! [[ -f /etc/default/readsb ]] && grep -qs /etc/default/dump1090-fa -e 'CONFIG_STYLE.*6' && ! [[ -f /boot/piaware-config.txt ]]; then
echo ------------
echo dump1090-fa new config style is not supported, sorry.
echo Also dump1090-fa version 6 has its own autogain, that should be sufficient.
echo Try readsb if you want to continue using this script:
echo "https://github.com/wiedehopf/adsb-scripts/wiki/Automatic-installation-for-readsb"
echo ------------
systemctl disable autogain1090.timer &>/dev/null
systemctl stop autogain1090.timer &>/dev/null
rm -f /lib/systemd/system/autogain1090.service
rm -f /lib/systemd/system/autogain1090.timer
exit 1
fi
if ! command -v jq &> /dev/null; then
apt update
apt install -y --no-install-suggests --no-install-recommends jq
hash -r
if ! command -v jq &> /dev/null; then
echo "ERROR: couldn't install jq! FATAL!"
exit 1
fi
fi
# script to change gain
mkdir -p /usr/local/bin
cat >/usr/local/bin/autogain1090 <<"EOF"
#!/bin/bash
low=0.5
high=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)
source /etc/default/autogain1090
tmp=/var/tmp/autogain1090
mkdir -p $tmp
#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 ! [[ -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 1
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
if [ -f /boot/adsb-config.txt ]; then
oldgain=$(grep -P -e 'GAIN=\K[0-9-.]*' -o /boot/adsb-config.txt)
else
oldgain=$(grep -P -e 'gain \K[0-9-.]*' -o /etc/default/$APP)
fi
if [[ "$oldgain" == "" ]]; then
oldgain=44
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=29
fi
if ! awk "BEGIN{ exit ($strong > $low) }" && ! awk "BEGIN{ exit ($strong < $high) }"; then
echo "No gain change needed, percentage of messages >-3dB is in nominal range. (${strong}%)"
exit 0
fi
if ! awk "BEGIN{ exit ($strong < $low) }" && [[ $gain_index == 29 ]]; then
echo "Gain already at maximum! (${strong}% messages >-3dB)"
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 "Gain already at minimum! (${strong}% messages >-3dB)"
exit 0
fi
if ! awk "BEGIN{ exit ($strong > $high) }"; then
gain_index=$(($gain_index-1))
action=Decreasing
fi
gain="${ga[$gain_index]}"
if [[ $gain == "" ]]; then echo "Gain already at maximum! (${strong}% messages >-3dB)"; exit 0; fi
if [ -f /boot/piaware-config.txt ]; then
piaware-config rtlsdr-gain $gain
fi
if [ -f /boot/adsb-config.txt ]; then
sed --follow-symlinks -i -E -e "s/^GAIN.*/GAIN=$gain/" /boot/adsb-config.txt
else
if ! grep gain /etc/default/$APP &>/dev/null && grep -E 'device-type.*rtlsdr' /etc/default/$APP &>/dev/null; then
sed --follow-symlinks -i -e 's/RECEIVER_OPTIONS="/RECEIVER_OPTIONS="--gain 49.6 /' /etc/default/$APP
fi
sed --follow-symlinks -i -E -e "s/--gain -?[0-9]*\.?[0-9]*/--gain $gain/" /etc/default/$APP
fi
systemctl restart $APP
#reset numbers
echo 0 > $tmp/strong
echo 0 > $tmp/total
echo "$action gain to $gain (${strong}% messages >-3dB)"
EOF
chmod a+x /usr/local/bin/autogain1090
config_file=/etc/default/autogain1090
if ! [ -f $config_file ]; then
cat >/etc/default/autogain1090 <<"EOF"
#!/bin/bash
low=0.5
high=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)
EOF
fi
rm -f /etc/cron.d/autogain1090
cat >/lib/systemd/system/autogain1090.service <<"EOF"
[Unit]
Description=autogain for readsb or dump1090-fa
[Service]
ExecStart=/usr/local/bin/autogain1090
EOF
cat >/lib/systemd/system/autogain1090.timer <<"EOF"
[Unit]
Description=Nightly automatic gain adjustment for readsb or dump1090-fa
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=30m
[Install]
WantedBy=timers.target
EOF
if grep jessie /etc/os-release >/dev/null; then
sed --follow-symlinks -i -e '/Randomized/d' /lib/systemd/system/autogain1090.timer
fi
systemctl daemon-reload
systemctl enable autogain1090.timer
systemctl restart autogain1090.timer
echo --------------
echo "All done!"