#!/usr/bin/env bash # SHIELD/ATLAS RTL-SDR ADS-B bridge. # Generated by https://shield-atlas-production.up.railway.app/setup/sdr-bridge.sh on 2026-05-04T03:54:00.601Z set -euo pipefail SERVER_URL="${SERVER_URL:-https://shield-atlas-production.up.railway.app}" INTERVAL_S="${INTERVAL_S:-2}" DUMP1090_PORT="${DUMP1090_PORT:-30053}" DUMP1090_JSON_DIR="${DUMP1090_JSON_DIR:-/run/dump1090-fa}" WORKDIR="${HOME}/shield-sensor-bay" mkdir -p "${WORKDIR}" cd "${WORKDIR}" echo "================================================================" echo " SHIELD ATLAS RTL-SDR ADS-B BRIDGE" echo "================================================================" echo " Server URL : ${SERVER_URL}" echo " Interval : ${INTERVAL_S}s" echo " JSON dir : ${DUMP1090_JSON_DIR}" echo "================================================================" OS="$(uname -s)" HAS_APT=0 HAS_BREW=0 command -v apt-get >/dev/null 2>&1 && HAS_APT=1 command -v brew >/dev/null 2>&1 && HAS_BREW=1 # 1) Install dependencies install_linux() { echo "INFO Linux detected. Installing rtl-sdr + dump1090-fa via apt (sudo prompt expected)..." sudo apt-get update # dump1090-fa is in the FlightAware repo; we fall back to plain dump1090-mutability if the FlightAware repo is not enabled. if apt-cache show dump1090-fa >/dev/null 2>&1; then sudo apt-get install -y rtl-sdr dump1090-fa curl sudo systemctl enable dump1090-fa sudo systemctl restart dump1090-fa JSON_PATH="/run/dump1090-fa/aircraft.json" elif apt-cache show dump1090-mutability >/dev/null 2>&1; then sudo apt-get install -y rtl-sdr dump1090-mutability curl sudo systemctl enable dump1090-mutability sudo systemctl restart dump1090-mutability JSON_PATH="/run/dump1090-mutability/aircraft.json" else echo "ERROR: neither dump1090-fa nor dump1090-mutability available via apt." >&2 echo " Add the FlightAware apt repo or install dump1090 manually:" >&2 echo " https://www.flightaware.com/adsb/piaware/install" >&2 exit 5 fi echo "OK dump1090 installed and started" } install_macos() { echo "INFO macOS detected. Installing rtl-sdr + dump1090 via brew..." brew install librtlsdr dump1090 jq curl || true # mac dump1090 writes JSON to ~/dump1090-data when --write-json is set JSON_PATH="${HOME}/dump1090-data/aircraft.json" mkdir -p "${HOME}/dump1090-data" # Start dump1090 in the background if not already running if ! pgrep -x dump1090 > /dev/null; then echo "INFO starting dump1090 in BACKGROUND (macOS brew has no launchd unit)" echo "INFO log: ${WORKDIR}/dump1090.log" echo "INFO to stop later: pkill dump1090" nohup dump1090 --net --write-json "${HOME}/dump1090-data" --quiet > "${WORKDIR}/dump1090.log" 2>&1 & sleep 3 fi echo "OK dump1090 running" } if [[ "${OS}" == "Linux" && "${HAS_APT}" == "1" ]]; then install_linux elif [[ "${OS}" == "Darwin" && "${HAS_BREW}" == "1" ]]; then install_macos else echo "ERROR: unsupported OS / package manager. Manually install rtl-sdr + dump1090 and re-run." >&2 exit 6 fi # 2) Sanity: confirm dongle present if command -v rtl_test >/dev/null 2>&1; then echo "INFO probing dongle (rtl_test -t, 5s)..." if ! timeout 5 rtl_test -t 2>&1 | head -20; then echo "WARN rtl_test errored. The dongle may need to be replugged or kernel module rtl2832 blacklisted." >&2 fi fi # 3) Sanity: confirm aircraft.json being produced echo "INFO waiting up to 30s for dump1090 to start producing aircraft.json..." for i in $(seq 1 30); do if [[ -f "${JSON_PATH}" ]]; then echo "OK ${JSON_PATH} present" break fi sleep 1 done if [[ ! -f "${JSON_PATH}" ]]; then echo "ERROR: ${JSON_PATH} never appeared. dump1090 may not be running." >&2 exit 7 fi # 4) Probe SHIELD server if ! curl -fsS --max-time 5 "${SERVER_URL}/api/sensor-bay/status" > /dev/null; then echo "ERROR: cannot reach ${SERVER_URL} from this laptop." >&2 exit 8 fi echo "OK SHIELD server reachable" cleanup() { echo; echo "Stopped after ${TICK:-0} ticks."; exit 0; } trap cleanup INT TERM TICK=0 echo "RUN forwarding ADS-B every ${INTERVAL_S}s. Ctrl-C to stop." echo "----------------------------------------------------------------" while true; do TICK=$((TICK+1)) if [[ -r "${JSON_PATH}" ]]; then HTTP_CODE="$(curl -fsS -m 5 -o /dev/null -w '%{http_code}' \ -H 'content-type: application/json' \ -X POST --data-binary "@${JSON_PATH}" \ "${SERVER_URL}/api/sensor-bay/adsb" || echo 000)" AC_COUNT="$(grep -o '"hex"' "${JSON_PATH}" | wc -l | tr -d ' ')" printf '[%s] tick %4d aircraft=%s POST=%s\n' "$(date +%H:%M:%S)" "${TICK}" "${AC_COUNT}" "${HTTP_CODE}" else printf '[%s] tick %4d json missing — dump1090 stalled?\n' "$(date +%H:%M:%S)" "${TICK}" fi sleep "${INTERVAL_S}" done