1
0
Fork 0
mirror of https://github.com/sdr-enthusiasts/docker-adsb-ultrafeeder.git synced 2024-12-11 23:20:10 +00:00
docker-adsb-ultrafeeder/rootfs/etc/s6-overlay/scripts/readsb

383 lines
12 KiB
Text
Executable file

#!/command/with-contenv bash
# shellcheck shell=bash disable=SC2015,SC2016,SC1091
#---------------------------------------------------------------------------------------------
# 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
source /scripts/interpret_ultrafeeder_config
s6wrap=(s6wrap --quiet --timestamps --prepend="${SCRIPT_NAME}")
mkdir -p /run/readsb
LOGLEVEL="${LOGLEVEL,,}"
LOGLEVEL="${LOGLEVEL:-verbose}"
chk_disabled "$LOGLEVEL" && LOGLEVEL="none" || true
if ! [[ "$LOGLEVEL" =~ ^(verbose|error|none)$ ]]; then
[[ -n "$LOGLEVEL" ]] && "${s6wrap[@]}" --args echo "[WARNING] LOGLEVEL set to an unknown value. Defaulting to \"verbose\"" || true
LOGLEVEL="verbose"
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
READSB_BIN="/usr/local/bin/readsb"
READSB_CMD=("--net")
READSB_CMD+=("--quiet")
if chk_enabled "${READSB_NET_ONLY}"; then
READSB_CMD+=("--net-only")
fi
if [ -n "${LAT}" ]; then
READSB_CMD+=(--lat "${LAT}")
elif [ -n "${READSB_LAT}" ]; then
READSB_CMD+=(--lat "${READSB_LAT}")
fi
if [ -n "${LONG}" ]; then
READSB_CMD+=(--lon "${LONG}")
elif [ -n "${READSB_LON}" ]; then
READSB_CMD+=(--lon "${READSB_LON}")
fi
if [ -n "${MLATHOST}" ]; then
READSB_CMD+=("--net-connector=${MLATHOST},${MLATPORT},beast_in")
fi
if [ -n "${BEASTHOST}" ]; then
READSB_CMD+=("--net-connector=${BEASTHOST},${BEASTPORT},beast_in")
fi
if [ -n "${UUID}" ]; then
READSB_CMD+=("--uuid-file=/run/uuid")
fi
READSB_CMD+=("--write-json=/run/readsb")
READSB_CMD+=("--write-state=/var/globe_history")
READSB_CMD+=("--json-trace-interval=15")
READSB_CMD+=("--json-reliable=1")
READSB_CMD+=("--net-json-port=30047")
READSB_CMD+=("--net-ri-port=30001")
READSB_CMD+=("--net-ro-port=30002")
READSB_CMD+=("--net-sbs-port=30003")
READSB_CMD+=("--net-sbs-in-port=32006")
# Handle a bunch of custom port assignments:
#
# net-bi-port is 30004,30104,${READSB_NET_BEAST_INPUT_PORT}
ports="${READSB_NET_BEAST_INPUT_PORT}${READSB_NET_BEAST_INPUT_PORT:+,}30004,30104"
ports="$(sort -nu <<< "${ports//,/$'\n'}" | sed -z 's/\n/,/g;s/^,//g;s/,$//g')" # de-duplication
READSB_CMD+=("--net-bi-port=$ports")
# net-bo-port is 30005,${READSB_NET_BEAST_OUTPUT_PORT}
ports="${READSB_NET_BEAST_OUTPUT_PORT}${READSB_NET_BEAST_OUTPUT_PORT:+,}30005"
ports="$(sort -nu <<< "${ports//,/$'\n'}" | sed -z 's/\n/,/g;s/^,//g;s/,$//g')" # de-duplication
READSB_CMD+=("--net-bo-port=$ports")
# net-beast-reduce-out-port is 30006,${READSB_NET_BEAST_REDUCE_OUTPUT_PORT}
ports="${READSB_NET_BEAST_REDUCE_OUTPUT_PORT}${READSB_NET_BEAST_REDUCE_OUTPUT_PORT:+,}30006"
ports="$(sort -nu <<< "${ports//,/$'\n'}" | sed -z 's/\n/,/g;s/^,//g;s/,$//g')" # de-duplication
READSB_CMD+=("--net-beast-reduce-out-port=$ports")
# net-bo-port is 30152,${READSB_ENABLE_API:+unix:/run/readsb/api.sock},${READSB_NET_API_PORT}
ports="${READSB_NET_API_PORT}${READSB_NET_API_PORT:+,}30152"
if chk_enabled "$READSB_ENABLE_API"; then ports="${ports},unix:/run/readsb/api.sock"; fi
ports="$(sort -nu <<< "${ports//,/$'\n'}" | sed -z 's/\n/,/g;s/^,//g;s/,$//g')" # de-duplication
READSB_CMD+=("--net-api-port=$ports")
#########################
if chk_enabled "${READSB_ENABLE_HEATMAP:-true}"; then
READSB_CMD+=("--heatmap-dir=/var/globe_history")
READSB_CMD+=("--heatmap=${READSB_HEATMAP_INTERVAL:-15}")
fi
if ! chk_enabled "$READSB_NET_SBS_DISABLE_REDUCE"; then
READSB_CMD+=("--net-sbs-reduce")
fi
if chk_enabled "${READSB_FORWARD_MLAT}"; then
"${s6wrap[@]}" --args echo "WARNING -- READSB_FORWARD_MLAT has been set! Do not feed the output of this container to any aggregators!"
READSB_CMD+=("--forward-mlat")
fi
if chk_enabled "${READSB_FORWARD_MLAT_SBS}"; then
"${s6wrap[@]}" --args echo "WARNING -- READSB_FORWARD_MLAT_SBS has been set! Do not feed the SBS (BaseStation) output of this container to any aggregators!"
READSB_CMD+=("--forward-mlat-sbs")
fi
if [[ -n "$READSB_RX_LOCATION_ACCURACY" ]]; then
READSB_CMD+=("--json-location-accuracy=$READSB_RX_LOCATION_ACCURACY")
fi
if [[ -n "$READSB_JSON_INTERVAL" ]]; then
READSB_CMD+=("--write-json-every=$READSB_JSON_INTERVAL")
fi
if [ -n "${READSB_DEBUG}" ]; then
READSB_CMD+=("--debug=$READSB_DEBUG")
fi
if chk_enabled "$GPSD_DEBUG"; then
READSB_CMD+=("--devel=debugGPS")
fi
# Handle --write-state-only-on-exit
if chk_enabled "${READSB_WRITE_STATE_ONLY_ON_EXIT}"; then
READSB_CMD+=("--write-state-only-on-exit")
fi
# Handle "--max-range=<dist>"
if [[ -n "$READSB_MAX_RANGE" ]]; then
READSB_CMD+=("--max-range=$READSB_MAX_RANGE")
fi
# Handle "--mlat"
# shellcheck disable=SC2153
if chk_enabled "$READSB_MLAT"; then
READSB_CMD+=("--mlat")
fi
# Handle "--modeac"
if chk_enabled "$READSB_MODEAC"; then
READSB_CMD+=("--modeac")
fi
# Handle "--stats-every=<sec>"
if [[ -n "$READSB_STATS_EVERY" ]]; then
READSB_CMD+=("--stats-every=$READSB_STATS_EVERY")
fi
# Handle "--stats-range"
if chk_enabled "$READSB_STATS_RANGE"; then
READSB_CMD+=("--stats-range")
fi
if [[ -n "$READSB_RANGE_OUTLINE_HOURS" ]]; then
READSB_CMD+=("--range-outline-hours=$READSB_RANGE_OUTLINE_HOURS")
fi
# Handle "--net-beast-reduce-interval=<seconds>"
# Default it to 0.5 second if no value is provided, which is reasonable for feeding aggregators
READSB_CMD+=("--net-beast-reduce-interval=${READSB_NET_BEAST_REDUCE_INTERVAL:-0.5}")
if chk_enabled "$READSB_NET_BR_OPTIMIZE_FOR_MLAT"; then
READSB_CMD+=("--net-beast-reduce-optimize-for-mlat")
fi
if chk_enabled "$READSB_NET_SBS_REDUCE"; then
READSB_CMD+=("--net-sbs-reduce")
fi
if [[ -n "$READSB_NET_BEAST_REDUCE_FILTER_DIST" ]]; then
READSB_CMD+=("--net-beast-reduce-filter-dist=$READSB_NET_BEAST_REDUCE_FILTER_DIST")
fi
if [[ -n "$READSB_JSON_TRACE_INTERVAL" ]]; then
READSB_CMD+=("--json-trace-interval=$READSB_JSON_TRACE_INTERVAL")
fi
if [[ -n "$READSB_NET_BEAST_REDUCE_FILTER_ALT" ]]; then
READSB_CMD+=("--net-beast-reduce-filter-alt=$READSB_NET_BEAST_REDUCE_FILTER_ALT")
fi
# Handle "--net-buffer=<n>"
if [[ -n "$READSB_NET_BUFFER" ]]; then
READSB_CMD+=("--net-buffer=$READSB_NET_BUFFER")
fi
# Handle "--net-connector=<ip,port,protocol>"
if [[ -n "$READSB_NET_CONNECTOR" ]]; then
# Strip any extraneous spaces:
READSB_NET_CONNECTOR="${READSB_NET_CONNECTOR#"${READSB_NET_CONNECTOR%%[![:space:]]*}"}" # strip leading space
READSB_NET_CONNECTOR="${READSB_NET_CONNECTOR//; /;}"
IFS=';' read -r -a READSB_NET_CONNECTOR_ARRAY <<< "$READSB_NET_CONNECTOR"
for NET_CONNECTOR_ELEMENT in "${READSB_NET_CONNECTOR_ARRAY[@]}"
do
READSB_CMD+=("--net-connector=${NET_CONNECTOR_ELEMENT// /}")
done
fi
# Handle ULTRAFEEDER_CONFIG / ULTRAFEEDER_NET_CONNECTOR elements relevant to readsb:
[[ "${#READSB_CONF_ARR[@]}" -gt 0 ]] && READSB_CMD+=("${READSB_CONF_ARR[@]}") || true
# Handle "--net-connector-delay=<seconds>"
if [[ -n "$READSB_NET_CONNECTOR_DELAY" ]]; then
READSB_CMD+=("--net-connector-delay=$READSB_NET_CONNECTOR_DELAY")
fi
# Handle "--net-heartbeat=<rate>"
READSB_CMD+=("--net-heartbeat=${READSB_NET_HEARTBEAT:-35}")
# Handle "--net-ri-port=<ports>"
if [[ -n "$READSB_NET_RAW_INPUT_PORT" ]]; then
READSB_CMD+=("--net-ri-port=$READSB_NET_RAW_INPUT_PORT")
fi
# Handle "--net-ro-interval=<rate>"
if [[ -n "$READSB_NET_RAW_OUTPUT_INTERVAL" ]]; then
READSB_CMD+=("--net-ro-interval=$READSB_NET_RAW_OUTPUT_INTERVAL")
fi
# use 0.12s/120ms to reduce number of packets sent to aggregators (compared to ro-interval 50ms)
# most aggregator feed clients historically use 200ms so this is already a reduction in jitter / latency
READSB_CMD+=("--net-ro-interval-beast-reduce=${READSB_NET_RO_INTERVAL_BEAST_REDUCE:-0.12}")
# Handle "--net-ri-port=<ports>"
if [[ -n "$READSB_NET_RAW_OUTPUT_PORT" ]]; then
READSB_CMD+=("--net-ro-port=$READSB_NET_RAW_OUTPUT_PORT")
fi
# Handle "--net-ro-size=<size>"
if [[ -n "$READSB_NET_RAW_OUTPUT_SIZE" ]]; then
READSB_CMD+=("--net-ro-size=$READSB_NET_RAW_OUTPUT_SIZE")
fi
# Handle "--net-sbs-in-port=<ports>"
if [[ -n "$READSB_NET_SBS_INPUT_PORT" ]]; then
READSB_CMD+=("--net-sbs-in-port=$READSB_NET_SBS_INPUT_PORT")
fi
# Handle "--net-sbs-port=<ports>"
if [[ -n "$READSB_NET_SBS_OUTPUT_PORT" ]]; then
READSB_CMD+=("--net-sbs-port=$READSB_NET_SBS_OUTPUT_PORT")
fi
# Handle "--net-verbatim"
if chk_enabled "$REASSB_NET_VERBATIM"; then
READSB_CMD+=("--net-verbatim")
fi
# Handle "--net-vrs-port=<ports>"
if [[ -n "$READSB_NET_VRS_PORT" ]]; then
READSB_CMD+=("--net-vrs-port=$READSB_NET_VRS_PORT")
fi
# make sure the db file exists, and if it does, use it
if chk_enabled "$TAR1090_ENABLE_AC_DB"; then
if [[ -e $TAR1090_UPDATE_DIR/aircraft.csv.gz ]]; then
READSB_CMD+=("--db-file=$TAR1090_UPDATE_DIR/aircraft.csv.gz")
elif [[ -e $TAR1090_INSTALL_DIR/aircraft.csv.gz ]]; then
# fallback to container supplied not updated csv.gz
READSB_CMD+=("--db-file=$TAR1090_INSTALL_DIR/aircraft.csv.gz")
fi
if ! chk_disabled "$TAR1090_DB_LONGTYPE"; then
READSB_CMD+=("--db-file-lt")
fi
fi
# Handle "--device-type"
if [[ -n "$READSB_DEVICE_TYPE" ]]; then
READSB_CMD+=("--device-type=$READSB_DEVICE_TYPE")
fi
# Handle "--gain"
if [[ -n "$READSB_GAIN" ]]; then
if [[ "${READSB_GAIN,,}" == "autogain" ]] && [[ -f /var/globe_history/autogain/gain ]]; then
read -r gain < /var/globe_history/autogain/gain
else
[[ "${READSB_GAIN,,}" == "autogain" ]] && gain="${READSB_AUTOGAIN_INITIAL_GAIN:-49.6}" || gain="${READSB_GAIN}"
fi
READSB_CMD+=("--gain=$gain")
fi
##### RTL-SDR OPTIONS #####
# Handle "--device=<index|serial>"
if [[ -n "$READSB_RTLSDR_DEVICE" ]]; then
READSB_CMD+=("--device=$READSB_RTLSDR_DEVICE")
fi
# Handle "--enable-agc"
if [[ -n "$READSB_RTLSDR_ENABLE_AGC" ]]; then
READSB_CMD+=("--enable-agc")
fi
# Handle "--ppm=<correction>"
if [[ -n "$READSB_RTLSDR_PPM" ]]; then
READSB_CMD+=("--ppm=$READSB_RTLSDR_PPM")
fi
##### MODE-S BEAST OPTIONS #####
# Handle "--beast-crc-off"
if chk_enabled "$READSB_BEAST_CRC_OFF"; then
READSB_CMD+=("--beast-crc-off")
fi
# Handle "--beast-df045-on"
if chk_enabled "$READSB_BEAST_DF045_ON"; then
READSB_CMD+=("--beast-df045-on")
fi
# Handle "--beast-df1117-on"
if chk_enabled "$READSB_BEAST_DF1117_ON"; then
READSB_CMD+=("--beast-df1117-on")
fi
# Handle "--beast-fec-off"
if chk_enabled "$READSB_BEAST_FEC_OFF"; then
READSB_CMD+=("--beast-fec-off")
fi
# Handle "--beast-mlat-off"
if chk_enabled "$READSB_BEAST_MLAT_OFF"; then
READSB_CMD+=("--beast-mlat-off")
fi
# Handle "--beast-modeac"
if chk_enabled "$READSB_BEAST_MODEAC"; then
READSB_CMD+=("--beast-modeac")
fi
# Handle "--beast-serial=<path>"
if [[ -n "$READSB_BEAST_SERIAL" ]]; then
READSB_CMD+=("--beast-serial=$READSB_BEAST_SERIAL")
fi
if [[ -n "$READSB_BEAST_BAUDRATE" ]]; then
READSB_CMD+=("--beast-baudrate=$READSB_BEAST_BAUDRATE")
fi
#####################
# Handle --write-prom=<filepath>
if chk_enabled "$PROMETHEUS_ENABLE"; then
READSB_CMD+=("--write-prom=/run/readsb-prometheus.prom")
fi
# wait 15 seconds if this is not the first startup
if [[ $(s6-svdt /run/service/readsb | wc -l) != 0 ]]; then
"${s6wrap[@]}" --args echo "delaying restart by 15 seconds"
sleep 15
fi
# shellcheck disable=SC2086
if [[ "${LOGLEVEL,,}" == "verbose" ]]; then
exec "${s6wrap[@]}" --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
elif [[ "${LOGLEVEL,,}" == "error" ]]; then
exec "${s6wrap[@]}" --ignore=stdout --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
elif [[ "${LOGLEVEL,,}" == "none" ]]; then
exec "${s6wrap[@]}" --ignore=stdout --ignore=stderr --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
fi