Repository navigation
Expand file tree
/
Copy pathdump3411.py
More file actions
290 lines (259 loc) · 10.6 KB
/
Copy pathdump3411.py
File metadata and controls
290 lines (259 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
dump3411 / dump3411.py
Single-process orchestrator. Runs both radios (BLE + WiFi) as daemon threads
feeding one shared in-memory ``Tracker``, plus a stdlib HTTP server that
serves the tracker snapshot at ``/data/remoteid.json``. See FEED.md.
Usage:
sudo python3 dump3411.py --wifi-iface wlan1 --serve 0.0.0.0:8754
The ``--serve`` flag is optional. Without it the process detects-and-journals
exactly like the standalone feeders but maintains the in-memory cache too —
useful if a future tool reads the tracker over some other interface.
The standalone ``ble_feeder.py`` and ``wifi_feeder.py`` scripts remain
runnable on their own for debugging a single radio in isolation.
"""
import argparse
import asyncio
import logging
import os
import signal
import sys
import threading
import time
from typing import Tuple
import feed_server
from ble_feeder import BLEFeeder
from tracker import Tracker
from wifi_feeder import CHANNELS_24, MAX_CHANNEL_CHOICES, WiFiFeeder
from version import get_version
# Logging is configured (root) by the feeder modules at import time; reuse it.
log = logging.getLogger("dump3411.main")
# -- CLI -----------------------------------------------------------------------
def _parse_addr(s: str) -> Tuple[str, int]:
"""Parse ``--serve`` argument: ``HOST:PORT``, ``:PORT``, or just ``PORT``."""
if ":" in s:
host, port_str = s.rsplit(":", 1)
else:
host, port_str = "", s
return (host or "0.0.0.0"), int(port_str)
def _parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(
description="dump3411 unified Remote ID detector + JSON feed (offline)",
)
p.add_argument(
"--ble-adapter", default="hci0",
help="HCI adapter for BLE scan (default: hci0)",
)
p.add_argument(
"--wifi-iface", default="wlan1",
help="USB monitor-mode interface for WiFi capture (default: wlan1)",
)
p.add_argument(
"--channel-dwell", type=float, default=0.2,
help="Seconds per channel before WiFi hopper moves on (default: 0.2)",
)
p.add_argument(
"--channel", type=int, default=None, metavar="CH",
help="Stay on a single fixed WiFi channel (1 to --max-channel) instead "
"of hopping (default: hop 1 to --max-channel)",
)
p.add_argument(
"--max-channel", type=int, default=CHANNELS_24[-1], choices=MAX_CHANNEL_CHOICES,
help="Highest 2.4 GHz WiFi channel to use (default: 11). 12-13 are legal "
"outside North America and need a matching regulatory domain.",
)
p.add_argument(
"--serve", default=None, metavar="HOST:PORT",
help="Serve /data/remoteid.json on HOST:PORT (e.g. 0.0.0.0:8754). "
"Omit for detection-only mode.",
)
p.add_argument(
"--ttl", type=float, default=60.0,
help="Seconds of no-messages before a drone is dropped from the feed "
"(default: 60)",
)
p.add_argument(
"--verbose", "-v", action="store_true",
help="Log every decoded message, not just Basic ID / Location",
)
# MQTT publisher — optional. Each flag falls back to an env var so the
# systemd unit can pull credentials from an EnvironmentFile without
# exposing them via `systemctl cat`.
p.add_argument(
"--mqtt-broker", default=os.environ.get("MQTT_BROKER"),
metavar="HOST[:PORT]",
help="Publish detections to an MQTT broker (e.g. mqtt.lan:1883). "
"Also read from $MQTT_BROKER. Requires paho-mqtt.",
)
p.add_argument(
"--mqtt-topic-prefix",
default=os.environ.get("MQTT_TOPIC_PREFIX", "dump3411"),
help="MQTT topic prefix (default: dump3411). "
"Also read from $MQTT_TOPIC_PREFIX.",
)
p.add_argument(
"--mqtt-user", default=os.environ.get("MQTT_USER"),
help="MQTT username. Also read from $MQTT_USER.",
)
p.add_argument(
"--mqtt-password", default=os.environ.get("MQTT_PASSWORD"),
help="MQTT password. Also read from $MQTT_PASSWORD.",
)
# Persistent history — optional. Disabled by default for SD-card safety.
p.add_argument(
"--history-db", default=os.environ.get("HISTORY_DB"),
metavar="PATH",
help="Persist per-message detections to a SQLite log at PATH. "
"Also read from $HISTORY_DB. Disabled when unset.",
)
p.add_argument(
"--history-max-mb", type=float,
default=float(os.environ.get("HISTORY_MAX_MB", "100")),
help="Cap the history DB at N megabytes (default: 100). "
"Also read from $HISTORY_MAX_MB.",
)
p.add_argument(
"--history-retention-days", type=float,
default=float(os.environ.get("HISTORY_RETENTION_DAYS", "30")),
help="Drop history rows older than N days (default: 30). "
"Also read from $HISTORY_RETENTION_DAYS.",
)
p.add_argument(
"--history-debounce-s", type=float,
default=float(os.environ.get("HISTORY_DEBOUNCE_S", "1.0")),
help="Min seconds between per-drone history writes (default: 1.0). "
"Also read from $HISTORY_DEBOUNCE_S.",
)
p.add_argument(
"--history-recent-days", type=float,
default=float(os.environ.get("HISTORY_RECENT_DAYS", "7")),
help="Lookback window for the dashboard's Recent detections table "
"and /history/recent.json's default (default: 7 days). "
"Also read from $HISTORY_RECENT_DAYS.",
)
args = p.parse_args()
if args.channel is not None and not 1 <= args.channel <= args.max_channel:
p.error(f"--channel must be 1-{args.max_channel} (raise --max-channel for 12-13)")
return args
# -- Main ----------------------------------------------------------------------
def main() -> None:
args = _parse_args()
log.info("dump3411 %s starting", get_version())
tracker = Tracker(ttl_seconds=args.ttl)
# MQTT publisher — only built if a broker is configured. Constructed before
# the radios so the tracker callbacks are wired before any message arrives.
publisher = None
if args.mqtt_broker:
try:
from mqtt_publisher import MqttPublisher
except ImportError as e:
log.error("--mqtt-broker requires paho-mqtt: %s", e)
sys.exit(2)
try:
publisher = MqttPublisher(
broker=args.mqtt_broker,
topic_prefix=args.mqtt_topic_prefix,
username=args.mqtt_user,
password=args.mqtt_password,
tracker=tracker,
)
except RuntimeError as e:
log.error("%s", e)
sys.exit(2)
# Persistent history writer — also optional.
history = None
if args.history_db:
from history import HistoryWriter
history = HistoryWriter(
db_path=args.history_db,
max_mb=args.history_max_mb,
retention_days=args.history_retention_days,
debounce_s=args.history_debounce_s,
recent_days=args.history_recent_days,
)
# Fan tracker callbacks out to every configured sink. Each sink gets the
# imperial row dict that goes into the JSON feed — same source of truth.
on_change_sinks = []
on_expire_sinks = []
if publisher is not None:
on_change_sinks.append(publisher.on_drone_change)
on_expire_sinks.append(publisher.on_drone_expire)
if history is not None:
on_change_sinks.append(history.on_drone_change)
# History intentionally has no expire hook — TTL eviction in the
# tracker doesn't mean we should forget the flight on disk.
def _fan_change(uas_id, row, _sinks=on_change_sinks):
for cb in _sinks:
try:
cb(uas_id, row)
except Exception:
log.exception("on_change sink raised for %s", uas_id)
def _fan_expire(uas_id, _sinks=on_expire_sinks):
for cb in _sinks:
try:
cb(uas_id)
except Exception:
log.exception("on_expire sink raised for %s", uas_id)
if on_change_sinks or on_expire_sinks:
tracker.set_callbacks(
on_change=_fan_change if on_change_sinks else None,
on_expire=_fan_expire if on_expire_sinks else None,
)
ble = BLEFeeder(adapter=args.ble_adapter, verbose=args.verbose,
tracker=tracker)
wifi = WiFiFeeder(iface=args.wifi_iface, verbose=args.verbose,
channel_dwell=args.channel_dwell, channel=args.channel,
max_channel=args.max_channel, tracker=tracker)
# Daemon threads: radios produce, sweeper expires stale entries.
threading.Thread(target=lambda: asyncio.run(ble.run()),
daemon=True, name="ble").start()
threading.Thread(target=wifi.run,
daemon=True, name="wifi").start()
threading.Thread(target=tracker.sweep_loop,
daemon=True, name="sweeper").start()
# Shutdown coordination — main thread either blocks in serve_forever or on
# a flag; signal handlers nudge it.
shutdown = threading.Event()
server = None
if args.serve:
host, port = _parse_addr(args.serve)
server = feed_server.make_server((host, port), tracker, history=history)
log.info(f"feed listening on http://{host}:{port}/data/remoteid.json")
if history is not None:
log.info(f"history at {args.history_db}; map view: "
f"http://{host}:{port}/map?uas_id=<UAS-ID>")
else:
log.info("detection-only mode (no --serve)")
if publisher is not None:
publisher.start()
if history is not None:
history.start()
def _handle_term(signum, _frame):
log.info(f"signal {signum} received — shutting down")
shutdown.set()
if server is not None:
# server.shutdown() must run off the serving thread.
threading.Thread(target=server.shutdown, daemon=True).start()
signal.signal(signal.SIGTERM, _handle_term)
signal.signal(signal.SIGINT, _handle_term)
try:
if server is not None:
server.serve_forever()
else:
while not shutdown.wait(1.0):
pass
finally:
wifi.stop()
tracker.stop()
if publisher is not None:
publisher.stop()
if history is not None:
history.stop()
# Wifi cleanup (restore_managed_mode) runs in its thread's finally.
# Give it a moment before the process exits and kills daemon threads.
time.sleep(1.5)
if server is not None:
server.server_close()
log.info("dump3411 exiting")
if __name__ == "__main__":
main()