1
0
Fork 0
mirror of https://github.com/sdr-enthusiasts/docker-adsb-ultrafeeder.git synced 2024-11-21 21:30:11 +00:00

move gpsd_mlat_restart into mlat-client scripting

add some wait time for receiver to be stationary
This commit is contained in:
Matthias Wirth 2024-05-26 20:40:46 +02:00
parent 04e7707600
commit e1280accdd
7 changed files with 106 additions and 98 deletions

View file

@ -1,2 +0,0 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/gpsd_mlat_restart

View file

@ -1 +0,0 @@
longrun

View file

@ -1,54 +0,0 @@
#!/command/with-contenv bash
# shellcheck shell=bash disable=SC2015,SC2016,SC1091,SC2001,SC2154
#---------------------------------------------------------------------------------------------
# Copyright (C) 2023-2024, Ramon F. Kolb (kx1t) and contributors
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------------------------
source /scripts/common
# Wait until all mlat-client instances have started
while [[ ! -f /run/.all_mlatclient_instances_have_started ]]; do
sleep 5 & wait $!
done
rm -f /run/.all_mlatclient_instances_have_started
# Check every 60 secs if GPSD has been enabled
while [[ ! -f /run/readsb/gpsd.json ]] || [[ "$(jq -r .lat /run/readsb/gpsd.json)" == "null" ]]; do
sleep 60 & wait $!
done
# Now wait a bit to allow GPS to establish itself
sleep "${GPSD_INITIAL_WAIT:-60}"
# Get initial lat/lon/alt
lat="${LAT:-${READSB_LAT:-$(jq -r .lat /run/readsb/gpsd.json)}}"
lon="${LONG:-${READSB_LON:-$(jq -r .lon /run/readsb/gpsd.json)}}"
"${s6wrap[@]}" echo "GPSD is active. Your mlat-client(s) will restart with new coordinates if you move more than ${GPSD_MIN_DISTANCE:-20} meters"
while :; do
new_lat="$(jq -r .lat /run/readsb/gpsd.json)"
new_lon="$(jq -r .lon /run/readsb/gpsd.json)"
distance="$(nice -n 20 distance "$lat" "$lon" "$new_lat" "$new_lon")"
if (( ${distance%%.*} > ${GPSD_MIN_DISTANCE:-20} )); then
"${s6wrap[@]}" echo "Receiver moved ${distance%%.*} meters - restarting all mlat-clients"
# kill the mlat-client instances so they get restarted with the new GPS coords
pkill -f "/usr/bin/python3 /usr/bin/mlat-client" >/dev/null 2>&1
lat="$new_lat"
lon="$new_lon"
fi
sleep "${GPSD_CHECK_INTERVAL:-30}" & wait $!
done

View file

@ -51,18 +51,67 @@ then
exec sleep infinity exec sleep infinity
fi fi
if [[ -z "$LAT$READSB_LAT" ]] && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_CONFIG" && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_NET_CONNECTOR"; then function check_gpsd() {
if [[ -z "$GPSD" ]] || ! [[ -f /run/readsb/gpsd.json ]]; then
return 1
fi
if new_lat="$(jq -r .lat /run/readsb/gpsd.json)" \
&& [[ "$new_lat" != "null" ]] \
&& new_lon="$(jq -r .lon /run/readsb/gpsd.json)" \
&& [[ "$new_lon" != "null" ]] \
&& new_alt="$(jq -r .altMSL /run/readsb/gpsd.json)" \
&& [[ "$new_alt" != "null" ]] \
then
# yay, vars are set and not null
if ! [[ -f "$LOCATION_PERSIST" ]]; then
echo "$new_lat" "$new_lon" "$new_alt" > "$LOCATION_PERSIST"
fi
return 0
else
new_lat=""
new_lon=""
new_alt=""
return 1
fi
}
if grep -qs "gpsd" <<< "$ULTRAFEEDER_CONFIG" || grep -qs "gpsd" <<< "$ULTRAFEEDER_NET_CONNECTOR"; then
GPSD=1
LOCATION_PERSIST=/var/globe_history/gpsd_last_location
if [[ -f "$LOCATION_PERSIST" ]] && { grep -qs "gpsd" <<< "$ULTRAFEEDER_CONFIG" || grep -qs "gpsd" <<< "$ULTRAFEEDER_NET_CONNECTOR"; }; then
read new_lat new_lon new_alt < "$LOCATION_PERSIST"
"${s6wrap[@]}" --args echo "Using last known GPSD location for startup: $new_lat,$new_lon (alt: $new_alt)"
else
# wait for gpsd to continue with startup
"${s6wrap[@]}" --args echo "GPSD configured, no location set, no persistent location found, waiting for gpsd to provide location data"
while ! check_gpsd; do
sleep "${GPSD_CHECK_INTERVAL:-30}" & wait $!
done
"${s6wrap[@]}" --args echo "GPSD has provided location data, starting mlat-clients"
fi
# initialize "old" location for gpsd movement detection
old_lat="$new_lat"
old_lon="$new_lon"
# in seconds
no_movement_required=19
# set no_movement to a number higher than the required time of no movement
# this way on startup there is no bogus message printed about starting mlat-clients with a new location
no_movement=$(( 2 * no_movement_required ))
else
if [[ -z "$LAT$READSB_LAT" ]]; then
"${s6wrap[@]}" --args echo "ERROR: READSB_LAT or LAT must be defined or GPSD must be enabled - MLAT will be disabled." "${s6wrap[@]}" --args echo "ERROR: READSB_LAT or LAT must be defined or GPSD must be enabled - MLAT will be disabled."
exec sleep infinity exec sleep infinity
fi fi
if [[ -z "$LONG$READSB_LON" ]]; then
if [[ -z "$LONG$READSB_LON" ]] && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_CONFIG" && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_NET_CONNECTOR"; then
"${s6wrap[@]}" --args echo "ERROR: READSB_LON or LONG must be defined or GPSD must be enabled - MLAT will be disabled." "${s6wrap[@]}" --args echo "ERROR: READSB_LON or LONG must be defined or GPSD must be enabled - MLAT will be disabled."
exec sleep infinity exec sleep infinity
fi fi
if [[ -z "$ALT$READSB_ALT" ]] && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_CONFIG" && ! grep -qi "gpsd_in" <<< "$ULTRAFEEDER_NET_CONNECTOR"; then if [[ -z "$ALT$READSB_ALT" ]]; then
"${s6wrap[@]}" --args echo "ERROR: READSB_ALT or ALT must be defined or GPSD must be enabled - MLAT will be disabled." "${s6wrap[@]}" --args echo "ERROR: READSB_ALT or ALT must be defined or GPSD must be enabled - MLAT will be disabled."
exec sleep infinity exec sleep infinity
fi
fi fi
# MLAT_CONFIG has the following format: # MLAT_CONFIG has the following format:
@ -176,20 +225,6 @@ do
fi fi
# add LAT/LON/ALT to instance: # add LAT/LON/ALT to instance:
# see if we need to use GPS coords:
unset new_lat
unset new_lon
unset new_alt
if [[ -f /run/.gpsd_is_active && -f /run/readsb/receiver.json ]]; then
new_lat="$(jq -r .lat /run/readsb/receiver.json)"
new_lon="$(jq -r .lon /run/readsb/receiver.json)"
new_alt="$(jq -r .alt_m_amsl /run/readsb/receiver.json)"
if [[ "$new_lat" == "null" ]] || [[ "$new_lon" == "null" ]] || [[ "$new_alt" == "null" ]]; then
unset new_lat
unset new_lon
unset new_alt
fi
fi
if [[ -n "${new_lat:-$lat_arg}" ]]; then if [[ -n "${new_lat:-$lat_arg}" ]]; then
MLAT_PARAM+=(--lat "${new_lat:-$lat_arg}") MLAT_PARAM+=(--lat "${new_lat:-$lat_arg}")
@ -255,11 +290,43 @@ do
done done
sleep 5 & wait $! sleep 5 & wait $!
touch /run/.all_mlatclient_instances_have_started
# Now iterate over all MLAT-client instances and check if they are still running: # Now iterate over all MLAT-client instances and check if they are still running:
while true while true
do do
if check_gpsd; then
distance="$(distance "$old_lat" "$old_lon" "$new_lat" "$new_lon")"
if (( ${distance%%.*} > ${GPSD_MIN_DISTANCE:-20} )); then
# kill the mlat-client instances so they get restarted with the new GPS coords
if pkill -f "/usr/bin/python3 /usr/bin/mlat-client" >/dev/null 2>&1; then
"${s6wrap[@]}" --args echo "Receiver moved ${distance%%.*} meters - Stopping all mlat-clients"
else
"${s6wrap[@]}" --args echo "Receiver moved ${distance%%.*} meters"
fi
old_lat="$new_lat"
old_lon="$new_lon"
# new location means the receiver has moved, sleep a bit and then check again
no_movement=0
sleep "${GPSD_CHECK_INTERVAL:-30}" & wait $!
# as the recevier has moved, mlat-clients are not restarted until there has been no movement for some time
# thus we continue skipping the mlat-client restart section of the loop
continue
else
# no movement during the checking interval, allow mlat-clients to be restarted
(( no_movement += ${GPSD_CHECK_INTERVAL:-30} )) || true
if (( no_movement < no_movement_required )); then
sleep "${GPSD_CHECK_INTERVAL:-30}" & wait $!
continue
elif (( no_movement / ${GPSD_CHECK_INTERVAL:-30} == no_movement_required / ${GPSD_CHECK_INTERVAL:-30} )); then
"${s6wrap[@]}" --args echo "Receiver stationary - starting all mlat-clients with new location"
echo "$new_lat" "$new_lon" "$new_alt" > "$LOCATION_PERSIST"
fi
fi
fi
for mlat_pid in "${!pid_array[@]}" for mlat_pid in "${!pid_array[@]}"
do do
if ! kill -0 "${mlat_pid}" >/dev/null 2>&1 if ! kill -0 "${mlat_pid}" >/dev/null 2>&1
@ -277,16 +344,11 @@ do
execstring="$(echo ${MLAT_CMD} ${pid_array[$mlat_pid]} | xargs)" execstring="$(echo ${MLAT_CMD} ${pid_array[$mlat_pid]} | xargs)"
# If GPSD is active, then replace the lat/lon/alt params with the ones from GPSD # If GPSD is active, then replace the lat/lon/alt params with the ones from GPSD
if [[ -f /run/readsb/gpsd.json ]] && [[ "$(jq -r .lat /run/readsb/gpsd.json)" != "null" ]]; then if [[ -n new_lat ]];then
new_lat="$(jq -r .lat /run/readsb/gpsd.json)"
new_lon="$(jq -r .lon /run/readsb/gpsd.json)"
new_alt="$(jq -r .altMSL /run/readsb/gpsd.json)"
if [[ "$new_lat" != "null" ]] && [[ "$new_lon" != "null" ]] && [[ "$new_alt" != "null" ]]; then
execstring="$(sed "s/^\(.*\s\+--lat\s\+\)[0-9.-]\+\(.*\)$/\1${new_lat}\2/g" <<< "$execstring")" execstring="$(sed "s/^\(.*\s\+--lat\s\+\)[0-9.-]\+\(.*\)$/\1${new_lat}\2/g" <<< "$execstring")"
execstring="$(sed "s/^\(.*\s\+--lon\s\+\)[0-9.-]\+\(.*\)$/\1${new_lon}\2/g" <<< "$execstring")" execstring="$(sed "s/^\(.*\s\+--lon\s\+\)[0-9.-]\+\(.*\)$/\1${new_lon}\2/g" <<< "$execstring")"
execstring="$(sed "s/^\(.*\s\+--alt\s\+\)[mft0-9.-]\+\(.*\)$/\1${new_alt}m\2/g" <<< "$execstring")" execstring="$(sed "s/^\(.*\s\+--alt\s\+\)[mft0-9.-]\+\(.*\)$/\1${new_alt}m\2/g" <<< "$execstring")"
fi fi
fi
#shellcheck disable=SC2069,SC2086 #shellcheck disable=SC2069,SC2086
if [[ "${LOGLEVEL}" == "verbose" ]]; then if [[ "${LOGLEVEL}" == "verbose" ]]; then
@ -303,10 +365,5 @@ do
fi fi
done done
# If GPSD is running, do more frequent checks than if it's not running sleep "${GPSD_CHECK_INTERVAL:-30}" & wait $!
if [[ -f /run/readsb/gpsd.json ]] && [[ "$(jq -r .lat /run/readsb/gpsd.json)" != "null" ]]; then
sleep 1 & wait $!
else
sleep 10 & wait $!
fi
done done

View file

@ -31,6 +31,14 @@ if ! [[ "$LOGLEVEL" =~ ^(verbose|error|none)$ ]]; then
LOGLEVEL="verbose" LOGLEVEL="verbose"
fi fi
LOCATION_PERSIST=/var/globe_history/gpsd_last_location
if [[ -f "$LOCATION_PERSIST" ]] && { grep -qs "gpsd" <<< "$ULTRAFEEDER_CONFIG" || grep -qs "gpsd" <<< "$ULTRAFEEDER_NET_CONNECTOR"; }; then
read LAT LON ALT < "$LOCATION_PERSIST"
READSB_LAT=""
READSB_LON=""
READSB_ALT=""
fi
# Build the readsb command line based on options # Build the readsb command line based on options
READSB_BIN="/usr/local/bin/readsb" READSB_BIN="/usr/local/bin/readsb"