Initial commit — DRB client (edge node) stack

Includes edge-node (FastAPI/MQTT/Discord voice), op25-container (SDR decoder),
and icecast (audio streaming).
This commit is contained in:
Logan
2026-04-05 19:01:51 -04:00
commit 1a9c92b6db
47 changed files with 2496 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
FROM python:3.11-slim
RUN apt-get update && apt-get install -y \
ffmpeg \
libopus0 \
libopus-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app/ ./app/
COPY tests/ ./tests/
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
View File
+39
View File
@@ -0,0 +1,39 @@
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
# Node identity
node_id: str
node_name: str = "Unnamed Node"
node_lat: float = 0.0
node_lon: float = 0.0
# MQTT
mqtt_broker: str
mqtt_port: int = 1883
mqtt_user: Optional[str] = None
mqtt_pass: Optional[str] = None
# C2 server (audio upload destination); None disables upload
c2_url: Optional[str] = None
# Local Icecast
icecast_host: str = "localhost"
icecast_port: int = 8000
icecast_mount: str = "/radio"
icecast_source_password: str = "hackme"
# OP25 container
op25_api_url: str = "http://localhost:8001"
op25_terminal_url: str = "http://localhost:8081"
# Paths (volume mounts)
config_path: str = "/configs"
recordings_path: str = "/recordings"
class Config:
env_file = ".env"
settings = Settings()
+117
View File
@@ -0,0 +1,117 @@
import asyncio
from pathlib import Path
from datetime import datetime, timezone
from typing import Optional
import httpx
from app.config import settings
from app.internal import credentials
from app.internal.logger import logger
MAX_RECORDING_SECONDS = 600 # 10 min safety cap; FFmpeg terminates long-running calls
class CallRecorder:
def __init__(self):
self._process: Optional[asyncio.subprocess.Process] = None
self._current_call_id: Optional[str] = None
self._current_file: Optional[Path] = None
self._icecast_url = (
f"http://{settings.icecast_host}:{settings.icecast_port}{settings.icecast_mount}"
)
self._recordings_dir = Path(settings.recordings_path)
async def start_recording(self, call_id: str) -> bool:
if self._process:
logger.warning("Recording already running — ignoring start.")
return False
self._recordings_dir.mkdir(parents=True, exist_ok=True)
ts = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
self._current_file = self._recordings_dir / f"{ts}_{call_id}.mp3"
self._current_call_id = call_id
cmd = [
"ffmpeg", "-y",
"-i", self._icecast_url,
"-acodec", "copy",
"-t", str(MAX_RECORDING_SECONDS),
str(self._current_file),
]
try:
self._process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
)
logger.info(f"Recording started: {self._current_file.name}")
return True
except Exception as e:
logger.error(f"FFmpeg start failed: {e}")
self._process = None
self._current_file = None
self._current_call_id = None
return False
async def stop_recording(self) -> Optional[Path]:
if not self._process:
return None
proc = self._process
output_file = self._current_file
self._process = None
self._current_file = None
self._current_call_id = None
try:
proc.terminate()
await asyncio.wait_for(proc.wait(), timeout=5)
except asyncio.TimeoutError:
proc.kill()
except ProcessLookupError:
pass
if output_file and output_file.exists() and output_file.stat().st_size > 0:
logger.info(f"Recording saved: {output_file.name} ({output_file.stat().st_size} bytes)")
return output_file
logger.warning("Recording file empty or missing — discarding.")
return None
async def upload_recording(self, file_path: Path, call_id: str) -> Optional[str]:
if not settings.c2_url:
logger.info("No C2_URL configured — skipping upload.")
return None
upload_url = f"{settings.c2_url}/upload"
api_key = credentials.get_api_key()
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
try:
async with httpx.AsyncClient(timeout=120) as client:
with open(file_path, "rb") as f:
r = await client.post(
upload_url,
files={"file": (file_path.name, f, "audio/mpeg")},
data={"call_id": call_id, "node_id": settings.node_id},
headers=headers,
)
r.raise_for_status()
audio_url = r.json().get("url")
logger.info(f"Upload complete: {audio_url}")
return audio_url
except Exception as e:
logger.error(f"Upload failed: {e}")
return None
finally:
try:
file_path.unlink()
except Exception:
pass
@property
def is_recording(self) -> bool:
return self._process is not None
call_recorder = CallRecorder()
@@ -0,0 +1,43 @@
import json
from pathlib import Path
from typing import Optional
from app.config import settings
from app.models import NodeConfig, SystemConfig
from app.internal.logger import logger
_CONFIG_FILE = Path(settings.config_path) / "node_config.json"
def load_node_config() -> NodeConfig:
if _CONFIG_FILE.exists():
try:
data = json.loads(_CONFIG_FILE.read_text())
return NodeConfig(**data)
except Exception as e:
logger.warning(f"Could not load node config, using defaults: {e}")
return NodeConfig(
node_id=settings.node_id,
node_name=settings.node_name,
lat=settings.node_lat,
lon=settings.node_lon,
configured=False,
)
def save_node_config(config: NodeConfig) -> None:
_CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
_CONFIG_FILE.write_text(config.model_dump_json(indent=2))
logger.info("Node config saved.")
def apply_system_config(system_config: SystemConfig) -> bool:
"""Write the OP25-compatible config blob to /configs/active.cfg.json."""
try:
active_cfg = Path(settings.config_path) / "active.cfg.json"
active_cfg.write_text(json.dumps(system_config.config, indent=2))
logger.info(f"System config applied: {system_config.name}")
return True
except Exception as e:
logger.error(f"Failed to write system config: {e}")
return False
+39
View File
@@ -0,0 +1,39 @@
"""
Manages the persisted node API key.
The key is provisioned by the C2 server after an admin approves the node.
It arrives via MQTT and is saved to /configs/credentials.json so it survives
container restarts.
"""
import json
from pathlib import Path
from app.config import settings
from app.internal.logger import logger
_CREDS_FILE = Path(settings.config_path) / "credentials.json"
_api_key: str | None = None
def load() -> None:
"""Load persisted credentials from disk on startup."""
global _api_key
if _CREDS_FILE.exists():
try:
data = json.loads(_CREDS_FILE.read_text())
_api_key = data.get("api_key")
if _api_key:
logger.info("Node credentials loaded from disk.")
except Exception as e:
logger.warning(f"Could not read credentials file: {e}")
def get_api_key() -> str | None:
return _api_key
def save_api_key(key: str) -> None:
global _api_key
_api_key = key
_CREDS_FILE.parent.mkdir(parents=True, exist_ok=True)
_CREDS_FILE.write_text(json.dumps({"api_key": key}))
logger.info("Node API key saved to disk.")
+119
View File
@@ -0,0 +1,119 @@
import asyncio
from typing import Optional
import discord
from discord.ext import commands
from app.config import settings
from app.internal.logger import logger
BOT_READY_TIMEOUT = 15 # seconds to wait for Discord bot to become ready
class RadioBot:
def __init__(self):
self._bot: Optional[commands.Bot] = None
self._voice_client: Optional[discord.VoiceClient] = None
self._task: Optional[asyncio.Task] = None
self._ready_event: Optional[asyncio.Event] = None
self._current_token: Optional[str] = None
self._icecast_url = (
f"http://{settings.icecast_host}:{settings.icecast_port}{settings.icecast_mount}"
)
async def join(self, guild_id: int, channel_id: int, token: str) -> bool:
# (Re)start the bot if the token changed or the bot isn't running
if self._current_token != token or not self._is_bot_running():
if not await self._start_bot(token):
return False
guild = self._bot.get_guild(guild_id)
if not guild:
logger.error(f"Guild {guild_id} not found — bot may not be a member.")
return False
channel = guild.get_channel(channel_id)
if not isinstance(channel, discord.VoiceChannel):
logger.error(f"Channel {channel_id} is not a voice channel.")
return False
try:
if self._voice_client and self._voice_client.is_connected():
await self._voice_client.disconnect(force=True)
self._voice_client = await channel.connect()
self._play_stream()
logger.info(f"Streaming to #{channel.name} in {guild.name}")
return True
except Exception as e:
logger.error(f"Failed to join voice channel: {e}")
return False
async def leave(self) -> bool:
if self._voice_client and self._voice_client.is_connected():
try:
await self._voice_client.disconnect(force=True)
self._voice_client = None
logger.info("Disconnected from voice channel.")
return True
except Exception as e:
logger.error(f"Failed to disconnect: {e}")
return False
async def stop(self):
await self.leave()
if self._task:
self._task.cancel()
if self._bot:
await self._bot.close()
self._bot = None
self._task = None
self._current_token = None
self._ready_event = None
def _play_stream(self):
if not self._voice_client:
return
source = discord.FFmpegPCMAudio(
self._icecast_url,
before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
)
self._voice_client.play(
discord.PCMVolumeTransformer(source, volume=1.0),
after=lambda e: logger.error(f"Stream ended unexpectedly: {e}") if e else None,
)
async def _start_bot(self, token: str) -> bool:
await self.stop() # clean up any previous instance
intents = discord.Intents.default()
intents.voice_states = True
self._bot = commands.Bot(command_prefix="!", intents=intents)
self._ready_event = asyncio.Event()
self._current_token = token
@self._bot.event
async def on_ready():
logger.info(f"Discord bot ready: {self._bot.user} ({self._bot.user.id})")
self._ready_event.set()
self._task = asyncio.create_task(self._bot.start(token))
try:
await asyncio.wait_for(self._ready_event.wait(), timeout=BOT_READY_TIMEOUT)
return True
except asyncio.TimeoutError:
logger.error("Timed out waiting for Discord bot to become ready.")
await self.stop()
return False
def _is_bot_running(self) -> bool:
return (
self._bot is not None
and self._task is not None
and not self._task.done()
)
@property
def is_connected(self) -> bool:
return self._voice_client is not None and self._voice_client.is_connected()
radio_bot = RadioBot()
+10
View File
@@ -0,0 +1,10 @@
import logging
import sys
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger("drb-edge-node")
@@ -0,0 +1,127 @@
import asyncio
import uuid
from datetime import datetime, timezone
from typing import Optional, Callable, Awaitable
from app.internal.op25_client import op25_client
from app.internal.logger import logger
CallbackFn = Callable[[dict], Awaitable[None]]
HANG_THRESHOLD = 3 # polls before declaring a call ended (1 poll/sec → 3s hang time)
POLL_INTERVAL = 1.0 # seconds
class MetadataWatcher:
def __init__(self):
self._running = False
self._current_tgid: Optional[int] = None
self._hang_counter: int = 0
self._active_call_id: Optional[str] = None
self._call_started_at: Optional[datetime] = None
# Set these before calling start()
self.on_call_start: Optional[CallbackFn] = None
self.on_call_end: Optional[CallbackFn] = None
async def start(self):
self._running = True
asyncio.create_task(self._poll_loop())
logger.info("Metadata watcher started.")
async def stop(self):
self._running = False
if self._active_call_id:
await self._end_call()
async def _poll_loop(self):
while self._running:
try:
await self._tick()
except Exception as e:
logger.warning(f"Metadata poll error: {e}")
await asyncio.sleep(POLL_INTERVAL)
async def _tick(self):
status = await op25_client.get_terminal_status()
if not status:
# OP25 not responding — hang-out any active call
if self._active_call_id:
self._hang_counter += 1
if self._hang_counter >= HANG_THRESHOLD:
await self._end_call()
return
# OP25 terminal returns either a list of channels or a single dict
channels = status if isinstance(status, list) else [status]
active_tgid: Optional[int] = None
active_meta: dict = {}
for ch in channels:
tgid = ch.get("tgid") or ch.get("tg_id")
if tgid and str(tgid) not in ("0", "", "None"):
active_tgid = int(tgid)
active_meta = ch
break
if active_tgid:
self._hang_counter = 0
if self._current_tgid != active_tgid:
# Talkgroup changed — close previous call and open a new one
if self._active_call_id:
await self._end_call()
self._current_tgid = active_tgid
await self._start_call(active_tgid, active_meta)
else:
# No active talkgroup
if self._active_call_id:
self._hang_counter += 1
if self._hang_counter >= HANG_THRESHOLD:
await self._end_call()
async def _start_call(self, tgid: int, meta: dict):
self._active_call_id = str(uuid.uuid4())
self._call_started_at = datetime.now(timezone.utc)
payload = {
"call_id": self._active_call_id,
"tgid": tgid,
"tgid_name": meta.get("tag") or meta.get("tgid_tag") or "",
"freq": meta.get("freq"),
"srcaddr": meta.get("srcaddr"),
"started_at": self._call_started_at.isoformat(),
}
logger.info(f"Call start: tgid={tgid} id={self._active_call_id}")
if self.on_call_start:
await self.on_call_start(payload)
async def _end_call(self):
if not self._active_call_id:
return
payload = {
"call_id": self._active_call_id,
"tgid": self._current_tgid,
"started_at": self._call_started_at.isoformat() if self._call_started_at else None,
"ended_at": datetime.now(timezone.utc).isoformat(),
}
logger.info(f"Call end: id={self._active_call_id}")
self._active_call_id = None
self._current_tgid = None
self._hang_counter = 0
self._call_started_at = None
if self.on_call_end:
await self.on_call_end(payload)
@property
def active_call_id(self) -> Optional[str]:
return self._active_call_id
@property
def current_tgid(self) -> Optional[int]:
return self._current_tgid
@property
def is_active(self) -> bool:
return self._active_call_id is not None
metadata_watcher = MetadataWatcher()
+159
View File
@@ -0,0 +1,159 @@
import asyncio
import json
from datetime import datetime, timezone
from typing import Optional, Callable, Awaitable, Dict, Any
import paho.mqtt.client as mqtt
from app.config import settings
from app.internal.logger import logger
CommandCallback = Callable[[Dict[str, Any]], Awaitable[None]]
ConfigCallback = Callable[[Dict[str, Any]], Awaitable[None]]
ApiKeyCallback = Callable[[Dict[str, Any]], Awaitable[None]]
class MQTTManager:
def __init__(self):
self._client: Optional[mqtt.Client] = None
self._loop: Optional[asyncio.AbstractEventLoop] = None
self._connected = False
self._connect_task: Optional[asyncio.Task] = None
self.on_command: Optional[CommandCallback] = None
self.on_config_push: Optional[ConfigCallback] = None
self.on_api_key: Optional[ApiKeyCallback] = None
nid = settings.node_id
self._t_checkin = f"nodes/{nid}/checkin"
self._t_status = f"nodes/{nid}/status"
self._t_metadata = f"nodes/{nid}/metadata"
self._t_commands = f"nodes/{nid}/commands"
self._t_config = f"nodes/{nid}/config"
self._t_api_key = f"nodes/{nid}/api_key"
self._t_discovery = "nodes/discovery/request"
def _build_client(self) -> mqtt.Client:
client = mqtt.Client(
callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
client_id=settings.node_id,
)
if settings.mqtt_user:
client.username_pw_set(settings.mqtt_user, settings.mqtt_pass)
lwt = json.dumps({
"node_id": settings.node_id,
"status": "offline",
"timestamp": datetime.now(timezone.utc).isoformat(),
})
client.will_set(self._t_status, lwt, qos=1, retain=True)
client.reconnect_delay_set(min_delay=2, max_delay=60)
client.on_connect = self._on_connect
client.on_disconnect = self._on_disconnect
client.on_message = self._on_message
return client
def _on_connect(self, client, userdata, flags, reason_code, properties):
if reason_code == 0:
self._connected = True
client.subscribe(self._t_commands, qos=1)
client.subscribe(self._t_config, qos=1)
client.subscribe(self._t_api_key, qos=2)
client.subscribe(self._t_discovery, qos=0)
logger.info("MQTT connected.")
asyncio.run_coroutine_threadsafe(self._publish_checkin(), self._loop)
else:
logger.error(f"MQTT connect refused: {reason_code}")
def _on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties):
self._connected = False
logger.warning(f"MQTT disconnected: {reason_code}")
def _on_message(self, client, userdata, msg):
try:
payload = json.loads(msg.payload.decode())
except Exception:
payload = msg.payload.decode()
if msg.topic == self._t_commands and self.on_command:
asyncio.run_coroutine_threadsafe(self.on_command(payload), self._loop)
elif msg.topic == self._t_config and self.on_config_push:
asyncio.run_coroutine_threadsafe(self.on_config_push(payload), self._loop)
elif msg.topic == self._t_api_key and self.on_api_key:
asyncio.run_coroutine_threadsafe(self.on_api_key(payload), self._loop)
elif msg.topic == self._t_discovery:
asyncio.run_coroutine_threadsafe(self._publish_checkin(), self._loop)
async def connect(self):
self._loop = asyncio.get_event_loop()
self._client = self._build_client()
self._connect_task = asyncio.create_task(self._connect_with_retry())
async def _connect_with_retry(self):
"""Attempt the initial TCP connect, retrying with backoff until it succeeds."""
delay = 5
logger.info(f"MQTT connecting to {settings.mqtt_broker}:{settings.mqtt_port}")
while True:
try:
self._client.connect(settings.mqtt_broker, settings.mqtt_port, keepalive=60)
self._client.loop_start()
# paho loop_start + reconnect_delay_set handles all subsequent reconnects
return
except Exception as e:
logger.warning(f"MQTT connect failed ({e}) — retrying in {delay}s")
await asyncio.sleep(delay)
delay = min(delay * 2, 60)
async def disconnect(self):
if self._connect_task:
self._connect_task.cancel()
if self._client:
await self.publish_status("offline")
self._client.loop_stop()
self._client.disconnect()
async def publish_status(self, status: str, extra: dict = None):
payload = {
"node_id": settings.node_id,
"status": status,
"timestamp": datetime.now(timezone.utc).isoformat(),
**(extra or {}),
}
self._publish(self._t_status, payload, qos=1, retain=True)
async def publish_metadata(self, event_type: str, data: dict):
payload = {
"event": event_type,
"node_id": settings.node_id,
"timestamp": datetime.now(timezone.utc).isoformat(),
**data,
}
self._publish(self._t_metadata, payload, qos=1)
async def _publish_checkin(self):
payload = {
"node_id": settings.node_id,
"name": settings.node_name,
"lat": settings.node_lat,
"lon": settings.node_lon,
"timestamp": datetime.now(timezone.utc).isoformat(),
}
self._publish(self._t_checkin, payload, qos=1)
def _publish(self, topic: str, payload: dict, qos: int = 0, retain: bool = False):
if self._client and self._connected:
self._client.publish(topic, json.dumps(payload), qos=qos, retain=retain)
else:
logger.debug(f"MQTT not connected, dropping publish to {topic}")
async def heartbeat_loop(self):
while True:
if self._connected:
await self._publish_checkin()
await asyncio.sleep(30)
@property
def is_connected(self) -> bool:
return self._connected
mqtt_manager = MQTTManager()
+63
View File
@@ -0,0 +1,63 @@
import httpx
from typing import Optional, Dict, Any
from app.config import settings
from app.internal.logger import logger
class OP25Client:
def __init__(self):
self.api_url = settings.op25_api_url
self.terminal_url = settings.op25_terminal_url
async def start(self) -> bool:
try:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.post(f"{self.api_url}/op25/start")
r.raise_for_status()
return True
except Exception as e:
logger.error(f"OP25 start failed: {e}")
return False
async def stop(self) -> bool:
try:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.post(f"{self.api_url}/op25/stop")
r.raise_for_status()
return True
except Exception as e:
logger.error(f"OP25 stop failed: {e}")
return False
async def status(self) -> Optional[Dict[str, Any]]:
try:
async with httpx.AsyncClient(timeout=5) as client:
r = await client.get(f"{self.api_url}/op25/status")
r.raise_for_status()
return r.json()
except Exception as e:
logger.error(f"OP25 status failed: {e}")
return None
async def generate_config(self, config: Dict[str, Any]) -> bool:
try:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.post(f"{self.api_url}/op25/generate-config", json=config)
r.raise_for_status()
return True
except Exception as e:
logger.error(f"OP25 generate-config failed: {e}")
return False
async def get_terminal_status(self) -> Optional[Any]:
"""Poll the OP25 HTTP terminal for current call metadata."""
try:
async with httpx.AsyncClient(timeout=3) as client:
r = await client.get(f"{self.terminal_url}/0/status.json")
r.raise_for_status()
return r.json()
except Exception:
return None
op25_client = OP25Client()
+141
View File
@@ -0,0 +1,141 @@
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.config import settings
from app.models import SystemConfig
from app.internal.logger import logger
from app.internal.mqtt_manager import mqtt_manager
from app.internal import credentials
from app.internal.metadata_watcher import metadata_watcher
from app.internal.call_recorder import call_recorder
from app.internal.discord_radio import radio_bot
from app.internal.config_manager import (
load_node_config,
save_node_config,
apply_system_config,
)
from app.routers import api, ui
# ---------------------------------------------------------------------------
# Event handlers wired up at startup
# ---------------------------------------------------------------------------
async def on_call_start(data: dict):
await mqtt_manager.publish_status("recording")
await mqtt_manager.publish_metadata("call_start", data)
await call_recorder.start_recording(data["call_id"])
async def on_call_end(data: dict):
file_path = await call_recorder.stop_recording()
if file_path:
audio_url = await call_recorder.upload_recording(file_path, data["call_id"])
if audio_url:
data["audio_url"] = audio_url
await mqtt_manager.publish_metadata("call_end", data)
await mqtt_manager.publish_status("online")
async def on_command(payload: dict):
action = payload.get("action")
logger.info(f"Command received: {action}")
if action == "discord_join":
token = payload.get("token")
if not token:
logger.error("discord_join command missing token — ignoring.")
return
await radio_bot.join(
guild_id=int(payload["guild_id"]),
channel_id=int(payload["channel_id"]),
token=token,
)
elif action == "discord_leave":
await radio_bot.leave()
elif action == "op25_restart":
from app.internal.op25_client import op25_client
await op25_client.stop()
await asyncio.sleep(2)
await op25_client.start()
else:
logger.warning(f"Unknown command: {action}")
async def on_api_key(payload: dict):
key = payload.get("api_key")
if key:
credentials.save_api_key(key)
logger.info("Node API key received and saved.")
async def on_config_push(payload: dict):
"""C2 pushes a system config — apply it and restart OP25 with the new settings."""
try:
config = SystemConfig(**payload)
except Exception as e:
logger.error(f"Invalid config push payload: {e}")
return
node_cfg = load_node_config()
node_cfg.assigned_system_id = config.system_id
node_cfg.system_config = config
node_cfg.configured = True
save_node_config(node_cfg)
apply_system_config(config)
# Restart OP25 so it picks up the new config
from app.internal.op25_client import op25_client
await op25_client.stop()
await asyncio.sleep(2)
await op25_client.start()
logger.info(f"Config push applied: {config.name}")
# ---------------------------------------------------------------------------
# App lifecycle
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info(f"Edge node starting — ID: {settings.node_id}")
# Load persisted credentials (API key provisioned by C2 after approval)
credentials.load()
# Wire callbacks
metadata_watcher.on_call_start = on_call_start
metadata_watcher.on_call_end = on_call_end
mqtt_manager.on_command = on_command
mqtt_manager.on_config_push = on_config_push
mqtt_manager.on_api_key = on_api_key
# Start services (radio_bot starts on-demand when a discord_join command arrives)
await mqtt_manager.connect()
await metadata_watcher.start()
# Report initial status and resume OP25 if node was already configured before this restart
node_cfg = load_node_config()
initial_status = "online" if node_cfg.configured else "unconfigured"
await mqtt_manager.publish_status(initial_status)
if node_cfg.configured:
from app.internal.op25_client import op25_client
logger.info("Node is configured — starting OP25.")
await op25_client.start()
heartbeat_task = asyncio.create_task(mqtt_manager.heartbeat_loop())
yield # --- app running ---
logger.info("Edge node shutting down.")
heartbeat_task.cancel()
await metadata_watcher.stop()
await radio_bot.stop()
await mqtt_manager.disconnect()
app = FastAPI(title=f"DRB Edge Node — {settings.node_id}", lifespan=lifespan)
app.include_router(api.router)
app.include_router(ui.router)
+51
View File
@@ -0,0 +1,51 @@
from pydantic import BaseModel
from typing import Optional, Dict, Any
from enum import Enum
from datetime import datetime
class NodeStatus(str, Enum):
ONLINE = "online"
OFFLINE = "offline"
RECORDING = "recording"
UNCONFIGURED = "unconfigured"
class CallStatus(str, Enum):
ACTIVE = "active"
ENDED = "ended"
class SystemConfig(BaseModel):
system_id: str
name: str
type: str # P25, DMR, NBFM
config: Dict[str, Any] # OP25-compatible config blob passed through to op25-container
class NodeConfig(BaseModel):
node_id: str
node_name: str
lat: float
lon: float
assigned_system_id: Optional[str] = None
system_config: Optional[SystemConfig] = None
configured: bool = False
class CallEvent(BaseModel):
call_id: str
node_id: str
system_id: Optional[str] = None
talkgroup_id: Optional[int] = None
talkgroup_name: Optional[str] = None
started_at: datetime
ended_at: Optional[datetime] = None
audio_url: Optional[str] = None
status: CallStatus = CallStatus.ACTIVE
class DiscordCommand(BaseModel):
action: str # "join" or "leave"
guild_id: Optional[str] = None
channel_id: Optional[str] = None
+82
View File
@@ -0,0 +1,82 @@
from fastapi import APIRouter, HTTPException
from app.config import settings
from app.models import SystemConfig
from app.internal.op25_client import op25_client
from app.internal.config_manager import load_node_config, save_node_config, apply_system_config
from app.internal.call_recorder import call_recorder
from app.internal.discord_radio import radio_bot
from app.internal.metadata_watcher import metadata_watcher
router = APIRouter(prefix="/api", tags=["api"])
@router.get("/status")
async def get_status():
node_cfg = load_node_config()
op25_status = await op25_client.status()
return {
"node_id": settings.node_id,
"node_name": settings.node_name,
"lat": settings.node_lat,
"lon": settings.node_lon,
"configured": node_cfg.configured,
"assigned_system_id": node_cfg.assigned_system_id,
"is_recording": call_recorder.is_recording,
"active_tgid": metadata_watcher.current_tgid,
"active_call_id": metadata_watcher.active_call_id,
"discord_connected": radio_bot.is_connected,
"icecast_url": (
f"http://{settings.icecast_host}:{settings.icecast_port}{settings.icecast_mount}"
),
"op25": op25_status,
}
@router.post("/op25/start")
async def start_op25():
ok = await op25_client.start()
if not ok:
raise HTTPException(500, "Failed to start OP25")
return {"ok": True}
@router.post("/op25/stop")
async def stop_op25():
ok = await op25_client.stop()
if not ok:
raise HTTPException(500, "Failed to stop OP25")
return {"ok": True}
@router.get("/config")
async def get_config():
return load_node_config()
@router.post("/config/system")
async def set_system_config(config: SystemConfig):
"""
Apply a system config locally — called by the web UI or pushed by C2.
Writes the OP25 config and persists the node config.
"""
node_cfg = load_node_config()
node_cfg.assigned_system_id = config.system_id
node_cfg.system_config = config
node_cfg.configured = True
save_node_config(node_cfg)
apply_system_config(config)
return {"ok": True}
@router.post("/discord/join")
async def discord_join(guild_id: int, channel_id: int):
ok = await radio_bot.join(guild_id, channel_id)
if not ok:
raise HTTPException(500, "Failed to join voice channel")
return {"ok": True}
@router.post("/discord/leave")
async def discord_leave():
await radio_bot.leave()
return {"ok": True}
+12
View File
@@ -0,0 +1,12 @@
from pathlib import Path
from fastapi import APIRouter
from fastapi.responses import HTMLResponse
router = APIRouter(tags=["ui"])
_TEMPLATE = Path(__file__).parent.parent / "templates" / "index.html"
@router.get("/", response_class=HTMLResponse)
async def index():
return _TEMPLATE.read_text()
+196
View File
@@ -0,0 +1,196 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DRB Edge Node</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: ui-monospace, "Cascadia Code", "Source Code Pro", monospace;
background: #0f1117;
color: #e2e8f0;
min-height: 100vh;
padding: 2rem;
}
h1 { font-size: 1.25rem; font-weight: 600; color: #f8fafc; margin-bottom: 0.25rem; }
.subtitle { font-size: 0.8rem; color: #64748b; margin-bottom: 2rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }
.card {
background: #1e2130;
border: 1px solid #2d3250;
border-radius: 8px;
padding: 1.25rem;
}
.card-title {
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.08em;
color: #64748b;
margin-bottom: 0.75rem;
}
.badge {
display: inline-block;
padding: 0.2rem 0.6rem;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.badge-online { background: #14532d; color: #4ade80; }
.badge-offline { background: #450a0a; color: #f87171; }
.badge-recording { background: #431407; color: #fb923c; animation: pulse 1.5s infinite; }
.badge-unconfigured { background: #1e1b4b; color: #818cf8; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
.row { display: flex; justify-content: space-between; align-items: center; padding: 0.4rem 0; border-bottom: 1px solid #2d3250; }
.row:last-child { border-bottom: none; }
.row .label { font-size: 0.78rem; color: #94a3b8; }
.row .value { font-size: 0.78rem; color: #e2e8f0; text-align: right; max-width: 60%; word-break: break-all; }
.listen-btn {
display: block;
width: 100%;
margin-top: 0.75rem;
padding: 0.6rem;
background: #2563eb;
color: #fff;
border: none;
border-radius: 6px;
font-size: 0.8rem;
font-family: inherit;
cursor: pointer;
text-align: center;
text-decoration: none;
}
.listen-btn:hover { background: #1d4ed8; }
.config-section { margin-top: 1.5rem; }
.config-section h2 { font-size: 0.85rem; color: #94a3b8; margin-bottom: 1rem; }
.alert {
background: #1e1b4b;
border: 1px solid #4338ca;
border-radius: 6px;
padding: 0.75rem 1rem;
font-size: 0.8rem;
color: #818cf8;
margin-bottom: 1rem;
}
.updated { font-size: 0.7rem; color: #475569; margin-top: 2rem; }
</style>
</head>
<body>
<h1 id="node-name">DRB Edge Node</h1>
<p class="subtitle" id="node-id">Loading…</p>
<div class="grid">
<div class="card">
<div class="card-title">Status</div>
<div class="row">
<span class="label">State</span>
<span class="value"><span id="status-badge" class="badge"></span></span>
</div>
<div class="row">
<span class="label">MQTT</span>
<span class="value" id="mqtt-status"></span>
</div>
<div class="row">
<span class="label">Discord</span>
<span class="value" id="discord-status"></span>
</div>
<div class="row">
<span class="label">Recording</span>
<span class="value" id="recording-status"></span>
</div>
</div>
<div class="card">
<div class="card-title">Current Call</div>
<div class="row">
<span class="label">Talkgroup</span>
<span class="value" id="tgid"></span>
</div>
<div class="row">
<span class="label">Call ID</span>
<span class="value" id="call-id"></span>
</div>
<div class="row">
<span class="label">System</span>
<span class="value" id="system-id"></span>
</div>
<div class="row">
<span class="label">OP25</span>
<span class="value" id="op25-status"></span>
</div>
</div>
<div class="card">
<div class="card-title">Node Info</div>
<div class="row">
<span class="label">Lat / Lon</span>
<span class="value" id="location"></span>
</div>
<div class="row">
<span class="label">Configured</span>
<span class="value" id="configured"></span>
</div>
<a id="icecast-link" class="listen-btn" href="#" target="_blank">Listen via Icecast</a>
</div>
</div>
<div id="unconfigured-banner" class="config-section" style="display:none">
<div class="alert">
This node has not been assigned a system yet. Connect it to a C2 server or configure it manually via the API (<code>POST /api/config/system</code>).
</div>
</div>
<p class="updated">Last updated: <span id="last-updated"></span></p>
<script>
async function refresh() {
try {
const r = await fetch('/api/status');
if (!r.ok) return;
const d = await r.json();
document.getElementById('node-name').textContent = d.node_name || 'DRB Edge Node';
document.getElementById('node-id').textContent = d.node_id || '';
const status = d.is_recording ? 'recording' : (d.configured ? 'online' : 'unconfigured');
const badge = document.getElementById('status-badge');
badge.textContent = status;
badge.className = 'badge badge-' + status;
document.getElementById('discord-status').textContent = d.discord_connected ? 'Connected' : 'Not connected';
document.getElementById('recording-status').textContent = d.is_recording ? 'Yes' : 'No';
document.getElementById('tgid').textContent = d.active_tgid ?? '—';
document.getElementById('call-id').textContent = d.active_call_id ? d.active_call_id.slice(0, 8) + '…' : '—';
document.getElementById('system-id').textContent = d.assigned_system_id ?? '—';
document.getElementById('op25-status').textContent = d.op25?.running ? 'Running' : 'Stopped';
document.getElementById('location').textContent = `${d.lat}, ${d.lon}`;
document.getElementById('configured').textContent = d.configured ? 'Yes' : 'No';
const icecastLink = document.getElementById('icecast-link');
icecastLink.href = d.icecast_url || '#';
document.getElementById('unconfigured-banner').style.display = d.configured ? 'none' : 'block';
document.getElementById('last-updated').textContent = new Date().toLocaleTimeString();
} catch (e) {
console.error('Status fetch failed:', e);
}
}
refresh();
setInterval(refresh, 3000);
</script>
</body>
</html>
+3
View File
@@ -0,0 +1,3 @@
[pytest]
asyncio_mode = auto
testpaths = tests
+9
View File
@@ -0,0 +1,9 @@
fastapi
uvicorn[standard]
pydantic-settings
paho-mqtt>=2.0.0
httpx
discord.py[voice]
PyNaCl
pytest
pytest-asyncio
View File
+5
View File
@@ -0,0 +1,5 @@
import os
# Satisfy pydantic-settings required fields before any app module is imported.
os.environ.setdefault("NODE_ID", "test-node-01")
os.environ.setdefault("MQTT_BROKER", "localhost")
+131
View File
@@ -0,0 +1,131 @@
"""
Unit tests for config_manager — pure file I/O, no external services.
"""
import json
import pytest
from pathlib import Path
from unittest.mock import patch
from app.models import NodeConfig, SystemConfig
# ---------------------------------------------------------------------------
# save / load round-trip
# ---------------------------------------------------------------------------
def test_save_and_load_roundtrip(tmp_path):
config = NodeConfig(
node_id="test-node-01",
node_name="Test Node",
lat=40.7128,
lon=-74.0060,
configured=True,
assigned_system_id="sys-001",
)
config_file = tmp_path / "node_config.json"
with patch("app.internal.config_manager._CONFIG_FILE", config_file):
import app.internal.config_manager as cm
cm.save_node_config(config)
loaded = cm.load_node_config()
assert loaded.node_id == config.node_id
assert loaded.node_name == config.node_name
assert loaded.lat == pytest.approx(config.lat)
assert loaded.lon == pytest.approx(config.lon)
assert loaded.configured is True
assert loaded.assigned_system_id == "sys-001"
def test_save_writes_valid_json(tmp_path):
config = NodeConfig(
node_id="node-x",
node_name="X",
lat=1.0,
lon=2.0,
)
config_file = tmp_path / "node_config.json"
with patch("app.internal.config_manager._CONFIG_FILE", config_file):
import app.internal.config_manager as cm
cm.save_node_config(config)
data = json.loads(config_file.read_text())
assert data["node_id"] == "node-x"
assert data["configured"] is False
# ---------------------------------------------------------------------------
# load fallback behaviour
# ---------------------------------------------------------------------------
def test_load_missing_file_returns_defaults(tmp_path):
config_file = tmp_path / "nonexistent.json"
with patch("app.internal.config_manager._CONFIG_FILE", config_file):
import app.internal.config_manager as cm
result = cm.load_node_config()
assert result.configured is False
def test_load_invalid_json_returns_defaults(tmp_path):
config_file = tmp_path / "node_config.json"
config_file.write_text("not valid json {{{")
with patch("app.internal.config_manager._CONFIG_FILE", config_file):
import app.internal.config_manager as cm
result = cm.load_node_config()
assert result.configured is False
def test_load_partial_json_returns_defaults(tmp_path):
"""A truncated file (e.g. mid-write crash) should fall back to defaults."""
config_file = tmp_path / "node_config.json"
config_file.write_text('{"node_id": "x"') # truncated
with patch("app.internal.config_manager._CONFIG_FILE", config_file):
import app.internal.config_manager as cm
result = cm.load_node_config()
assert result.configured is False
# ---------------------------------------------------------------------------
# apply_system_config
# ---------------------------------------------------------------------------
def test_apply_system_config_writes_json(tmp_path):
system = SystemConfig(
system_id="sys-001",
name="Test System",
type="P25",
config={"freq": 851.0125, "nac": 0x293},
)
with patch("app.internal.config_manager.settings") as mock_settings:
mock_settings.config_path = str(tmp_path)
import app.internal.config_manager as cm
result = cm.apply_system_config(system)
assert result is True
written = json.loads((tmp_path / "active.cfg.json").read_text())
assert written["freq"] == pytest.approx(851.0125)
assert written["nac"] == 0x293
def test_apply_system_config_returns_false_on_error(tmp_path):
system = SystemConfig(
system_id="sys-001",
name="Test",
type="DMR",
config={},
)
with patch("app.internal.config_manager.settings") as mock_settings:
# Point at a path that can't be written (a file, not a dir)
blocker = tmp_path / "blocker"
blocker.touch()
mock_settings.config_path = str(blocker)
import app.internal.config_manager as cm
result = cm.apply_system_config(system)
assert result is False
@@ -0,0 +1,190 @@
"""
Unit tests for MetadataWatcher state machine.
All OP25 HTTP calls are mocked — no running services required.
"""
import pytest
from unittest.mock import AsyncMock, patch
from app.internal.metadata_watcher import MetadataWatcher, HANG_THRESHOLD
@pytest.fixture
def watcher():
w = MetadataWatcher()
w.on_call_start = AsyncMock()
w.on_call_end = AsyncMock()
return w
# ---------------------------------------------------------------------------
# Call start
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_call_starts_when_tgid_appears(watcher):
status = [{"tgid": 1234, "tag": "Police Dispatch"}]
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert watcher.is_active
assert watcher.current_tgid == 1234
watcher.on_call_start.assert_called_once()
payload = watcher.on_call_start.call_args[0][0]
assert payload["tgid"] == 1234
assert payload["tgid_name"] == "Police Dispatch"
assert "call_id" in payload
assert "started_at" in payload
@pytest.mark.asyncio
async def test_tgid_zero_does_not_start_call(watcher):
status = [{"tgid": 0}]
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert not watcher.is_active
watcher.on_call_start.assert_not_called()
@pytest.mark.asyncio
async def test_tgid_none_string_does_not_start_call(watcher):
status = [{"tgid": "None"}]
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert not watcher.is_active
@pytest.mark.asyncio
async def test_op25_offline_does_not_start_call(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=None)):
await watcher._tick()
assert not watcher.is_active
watcher.on_call_start.assert_not_called()
# ---------------------------------------------------------------------------
# Hang / call end
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_hang_below_threshold_keeps_call_alive(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1234}])):
await watcher._tick()
assert watcher.is_active
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 0}])):
for _ in range(HANG_THRESHOLD - 1):
await watcher._tick()
assert watcher.is_active
watcher.on_call_end.assert_not_called()
@pytest.mark.asyncio
async def test_hang_at_threshold_ends_call(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1234}])):
await watcher._tick()
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 0}])):
for _ in range(HANG_THRESHOLD):
await watcher._tick()
assert not watcher.is_active
watcher.on_call_end.assert_called_once()
payload = watcher.on_call_end.call_args[0][0]
assert "call_id" in payload
assert "ended_at" in payload
@pytest.mark.asyncio
async def test_op25_offline_triggers_hang_and_ends_call(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1234}])):
await watcher._tick()
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=None)):
for _ in range(HANG_THRESHOLD):
await watcher._tick()
assert not watcher.is_active
watcher.on_call_end.assert_called_once()
@pytest.mark.asyncio
async def test_hang_counter_resets_when_tgid_returns(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1234}])):
await watcher._tick()
# Partial hang — not enough to end
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 0}])):
for _ in range(HANG_THRESHOLD - 1):
await watcher._tick()
assert watcher.is_active
# tgid returns — counter resets
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1234}])):
await watcher._tick()
assert watcher._hang_counter == 0
assert watcher.is_active
# ---------------------------------------------------------------------------
# Talkgroup changes
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_talkgroup_change_closes_old_and_opens_new(watcher):
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 1111}])):
await watcher._tick()
first_call_id = watcher.active_call_id
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=[{"tgid": 2222}])):
await watcher._tick()
assert watcher.is_active
assert watcher.current_tgid == 2222
assert watcher.active_call_id != first_call_id
watcher.on_call_end.assert_called_once()
assert watcher.on_call_start.call_count == 2
# ---------------------------------------------------------------------------
# Status format variations
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_single_dict_status_instead_of_list(watcher):
"""OP25 terminal may return a bare dict instead of a list."""
status = {"tgid": 9999, "tag": "Fire"}
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert watcher.current_tgid == 9999
@pytest.mark.asyncio
async def test_tg_id_key_alias(watcher):
"""Some OP25 builds use 'tg_id' instead of 'tgid'."""
status = [{"tg_id": 5555, "tag": "EMS"}]
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert watcher.current_tgid == 5555
@pytest.mark.asyncio
async def test_multichannel_uses_first_active(watcher):
"""When multiple channels are returned, first active tgid wins."""
status = [
{"tgid": 0},
{"tgid": 7777, "tag": "Roads"},
{"tgid": 8888, "tag": "Other"},
]
with patch("app.internal.metadata_watcher.op25_client.get_terminal_status", new=AsyncMock(return_value=status)):
await watcher._tick()
assert watcher.current_tgid == 7777