2023-08-31 02:59:09 +00:00
#!/command/with-contenv bash
2023-04-19 00:37:52 +00:00
# shellcheck shell=bash disable=SC2015,SC2016,SC1091
2023-04-08 21:07:11 +00:00
2024-03-29 23:21:45 +00:00
#---------------------------------------------------------------------------------------------
# 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/>.
#---------------------------------------------------------------------------------------------
2023-04-08 21:07:11 +00:00
source /scripts/common
2023-04-19 00:36:45 +00:00
source /scripts/interpret_ultrafeeder_config
2024-05-08 14:00:36 +00:00
s6wrap=(s6wrap --quiet --timestamps --prepend="${SCRIPT_NAME}")
2023-03-21 19:56:02 +00:00
mkdir -p /run/readsb
2024-05-08 14:25:42 +00:00
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
2024-05-26 18:40:46 +00:00
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
2023-03-21 19:56:02 +00:00
# Build the readsb command line based on options
READSB_BIN="/usr/local/bin/readsb"
READSB_CMD=("--net")
READSB_CMD+=("--quiet")
2023-04-08 21:07:11 +00:00
if chk_enabled "${READSB_NET_ONLY}"; then
2023-03-21 19:56:02 +00:00
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
2023-04-08 22:45:11 +00:00
if [ -n "${UUID}" ]; then
2023-03-21 19:56:02 +00:00
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")
2024-03-29 19:30:32 +00:00
READSB_CMD+=("--net-json-port=30047")
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--net-ri-port=30001")
READSB_CMD+=("--net-ro-port=30002")
READSB_CMD+=("--net-sbs-port=30003")
2024-03-29 19:30:32 +00:00
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}
2024-03-29 21:33:33 +00:00
ports="${READSB_NET_BEAST_INPUT_PORT}${READSB_NET_BEAST_INPUT_PORT:+,}30004,30104"
2024-03-29 20:55:15 +00:00
ports="$(sort -nu <<< "${ports//,/$'\n'}" | sed -z 's/\n/,/g;s/^,//g;s/,$//g')" # de-duplication
2024-03-29 19:30:32 +00:00
READSB_CMD+=("--net-bi-port=$ports")
# net-bo-port is 30005,${READSB_NET_BEAST_OUTPUT_PORT}
2024-03-29 20:55:15 +00:00
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
2024-03-29 19:30:32 +00:00
READSB_CMD+=("--net-bo-port=$ports")
# net-beast-reduce-out-port is 30006,${READSB_NET_BEAST_REDUCE_OUTPUT_PORT}
2024-03-29 20:55:15 +00:00
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
2024-03-29 19:30:32 +00:00
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}
2024-03-29 20:55:15 +00:00
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
2024-03-29 19:30:32 +00:00
READSB_CMD+=("--net-api-port=$ports")
#########################
2023-03-21 19:56:02 +00:00
2023-10-26 19:26:37 +00:00
if chk_enabled "${READSB_ENABLE_HEATMAP:-true}"; then
READSB_CMD+=("--heatmap-dir=/var/globe_history")
READSB_CMD+=("--heatmap=${READSB_HEATMAP_INTERVAL:-15}")
fi
2024-11-01 13:09:15 +00:00
if chk_enabled "${READSB_ENABLE_TRACES}"; then
READSB_CMD+=("--write-globe-history=/var/globe_history")
fi
2023-04-08 21:07:11 +00:00
if ! chk_enabled "$READSB_NET_SBS_DISABLE_REDUCE"; then
2023-03-30 17:28:25 +00:00
READSB_CMD+=("--net-sbs-reduce")
fi
2023-04-08 21:07:11 +00:00
if chk_enabled "${READSB_FORWARD_MLAT}"; then
2023-10-26 19:26:37 +00:00
"${s6wrap[@]}" --args echo "WARNING -- READSB_FORWARD_MLAT has been set! Do not feed the output of this container to any aggregators!"
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--forward-mlat")
fi
2023-04-08 21:07:11 +00:00
if chk_enabled "${READSB_FORWARD_MLAT_SBS}"; then
2023-10-26 19:26:37 +00:00
"${s6wrap[@]}" --args echo "WARNING -- READSB_FORWARD_MLAT_SBS has been set! Do not feed the SBS (BaseStation) output of this container to any aggregators!"
2023-03-30 17:28:25 +00:00
READSB_CMD+=("--forward-mlat-sbs")
fi
2023-03-21 19:56:02 +00:00
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
2024-06-29 11:47:24 +00:00
if chk_enabled "$GPSD_DEBUG"; then
READSB_CMD+=("--devel=debugGPS")
fi
2023-03-21 19:56:02 +00:00
# Handle --write-state-only-on-exit
2023-04-08 21:07:11 +00:00
if chk_enabled "${READSB_WRITE_STATE_ONLY_ON_EXIT}"; then
2023-03-21 19:56:02 +00:00
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"
2023-04-19 00:49:18 +00:00
# shellcheck disable=SC2153
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_MLAT"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--mlat")
fi
# Handle "--modeac"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_MODEAC"; then
2023-03-21 19:56:02 +00:00
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"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_STATS_RANGE"; then
2023-03-21 19:56:02 +00:00
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>"
2024-02-20 13:58:23 +00:00
# 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}")
2023-03-21 19:56:02 +00:00
2024-05-29 15:58:42 +00:00
if chk_enabled "$READSB_NET_BR_OPTIMIZE_FOR_MLAT"; then
READSB_CMD+=("--net-beast-reduce-optimize-for-mlat")
fi
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_NET_SBS_REDUCE"; then
2023-03-21 19:56:02 +00:00
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
2023-04-19 00:36:45 +00:00
# Strip any extraneous spaces:
READSB_NET_CONNECTOR="${READSB_NET_CONNECTOR#"${READSB_NET_CONNECTOR%%[![:space:]]*}"}" # strip leading space
READSB_NET_CONNECTOR="${READSB_NET_CONNECTOR//; /;}"
2023-03-21 19:56:02 +00:00
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
2023-04-19 00:36:45 +00:00
# Handle ULTRAFEEDER_CONFIG / ULTRAFEEDER_NET_CONNECTOR elements relevant to readsb:
2023-04-19 00:37:52 +00:00
[[ "${#READSB_CONF_ARR[@]}" -gt 0 ]] && READSB_CMD+=("${READSB_CONF_ARR[@]}") || true
2023-04-19 00:36:45 +00:00
2023-03-21 19:56:02 +00:00
# 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>"
2023-04-19 01:29:12 +00:00
READSB_CMD+=("--net-heartbeat=${READSB_NET_HEARTBEAT:-35}")
2023-03-21 19:56:02 +00:00
# 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
2024-03-14 12:59:06 +00:00
# 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}")
2023-03-21 19:56:02 +00:00
# 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"
2023-04-08 21:07:11 +00:00
if chk_enabled "$REASSB_NET_VERBATIM"; then
2023-03-21 19:56:02 +00:00
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
2024-07-04 17:32:55 +00:00
if chk_enabled "$TAR1090_ENABLE_AC_DB"; then
2024-04-13 21:52:17 +00:00
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
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--db-file=$TAR1090_INSTALL_DIR/aircraft.csv.gz")
fi
2024-09-17 21:15:01 +00:00
if ! chk_disabled "$TAR1090_DB_LONGTYPE"; then
READSB_CMD+=("--db-file-lt")
fi
2023-03-21 19:56:02 +00:00
fi
# Handle "--device-type"
if [[ -n "$READSB_DEVICE_TYPE" ]]; then
READSB_CMD+=("--device-type=$READSB_DEVICE_TYPE")
fi
2023-03-24 12:44:41 +00:00
# Handle "--gain"
2023-03-21 19:56:02 +00:00
if [[ -n "$READSB_GAIN" ]]; then
2023-03-24 12:44:41 +00:00
if [[ "${READSB_GAIN,,}" == "autogain" ]] && [[ -f /var/globe_history/autogain/gain ]]; then
read -r gain < /var/globe_history/autogain/gain
2023-04-08 21:07:11 +00:00
else
2023-10-26 03:34:55 +00:00
[[ "${READSB_GAIN,,}" == "autogain" ]] && gain="${READSB_AUTOGAIN_INITIAL_GAIN:-49.6}" || gain="${READSB_GAIN}"
2023-03-24 12:44:41 +00:00
fi
2023-04-08 22:24:39 +00:00
READSB_CMD+=("--gain=$gain")
2023-03-21 19:56:02 +00:00
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"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_CRC_OFF"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--beast-crc-off")
fi
# Handle "--beast-df045-on"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_DF045_ON"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--beast-df045-on")
fi
# Handle "--beast-df1117-on"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_DF1117_ON"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--beast-df1117-on")
fi
# Handle "--beast-fec-off"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_FEC_OFF"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--beast-fec-off")
fi
# Handle "--beast-mlat-off"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_MLAT_OFF"; then
2023-03-21 19:56:02 +00:00
READSB_CMD+=("--beast-mlat-off")
fi
# Handle "--beast-modeac"
2023-04-08 21:07:11 +00:00
if chk_enabled "$READSB_BEAST_MODEAC"; then
2023-03-21 19:56:02 +00:00
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
2023-03-28 20:01:27 +00:00
#####################
2023-04-08 22:24:39 +00:00
# Handle --write-prom=<filepath>
if chk_enabled "$PROMETHEUS_ENABLE"; then
2023-03-28 20:01:27 +00:00
READSB_CMD+=("--write-prom=/run/readsb-prometheus.prom")
fi
2024-08-03 18:19:14 +00:00
# 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
2024-05-08 14:00:36 +00:00
# shellcheck disable=SC2086
2023-10-26 19:26:37 +00:00
if [[ "${LOGLEVEL,,}" == "verbose" ]]; then
2024-05-29 15:49:23 +00:00
exec "${s6wrap[@]}" --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
2023-03-27 15:39:27 +00:00
elif [[ "${LOGLEVEL,,}" == "error" ]]; then
2024-05-29 15:49:23 +00:00
exec "${s6wrap[@]}" --ignore=stdout --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
2023-03-27 15:39:27 +00:00
elif [[ "${LOGLEVEL,,}" == "none" ]]; then
2024-05-29 15:49:23 +00:00
exec "${s6wrap[@]}" --ignore=stdout --ignore=stderr --args "${READSB_BIN}" "${READSB_CMD[@]}" $READSB_AUTOMATION_ARGS $READSB_EXTRA_ARGS
2023-03-27 15:39:27 +00:00
fi