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
+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()