Files
venus/dbus-lightning/strike_buffer.py
dev 9756538f16 Initial commit: Venus OS boat addons monorepo
Organizes 11 projects for Cerbo GX/Venus OS into a single repository:
- axiom-nmea: Raymarine LightHouse protocol decoder
- dbus-generator-ramp: Generator current ramp controller
- dbus-lightning: Blitzortung lightning monitor
- dbus-meteoblue-forecast: Meteoblue weather forecast
- dbus-no-foreign-land: noforeignland.com tracking
- dbus-tides: Tide prediction from depth + harmonics
- dbus-vrm-history: VRM cloud history proxy
- dbus-windy-station: Windy.com weather upload
- mfd-custom-app: MFD app deployment package
- venus-html5-app: Custom Victron HTML5 app fork
- watermaker: Watermaker PLC control UI

Adds root README, .gitignore, project template, and per-project
.gitignore files. Sensitive config files excluded via .gitignore
with .example templates provided.

Made-with: Cursor
2026-03-16 17:04:16 +00:00

109 lines
3.3 KiB
Python

"""
In-memory rolling buffer for lightning strikes with distance/bearing filtering.
"""
import math
import threading
import time
from collections import deque
def haversine_miles(lat1, lon1, lat2, lon2):
"""Great-circle distance in statute miles."""
R = 3958.8
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = (math.sin(dlat / 2) ** 2 +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
math.sin(dlon / 2) ** 2)
return R * 2 * math.asin(min(1.0, math.sqrt(a)))
def bearing_degrees(lat1, lon1, lat2, lon2):
"""Forward azimuth from point 1 to point 2 in degrees (0=N, 90=E)."""
lat1_r, lat2_r = math.radians(lat1), math.radians(lat2)
dlon = math.radians(lon2 - lon1)
x = math.sin(dlon) * math.cos(lat2_r)
y = (math.cos(lat1_r) * math.sin(lat2_r) -
math.sin(lat1_r) * math.cos(lat2_r) * math.cos(dlon))
return (math.degrees(math.atan2(x, y)) + 360) % 360
class Strike:
__slots__ = ('timestamp', 'lat', 'lon', 'distance', 'bearing')
def __init__(self, timestamp, lat, lon, distance, bearing):
self.timestamp = timestamp
self.lat = lat
self.lon = lon
self.distance = distance
self.bearing = bearing
class StrikeBuffer:
"""Thread-safe rolling buffer of nearby lightning strikes."""
def __init__(self, radius_miles, max_age_seconds):
self.radius_miles = radius_miles
self.max_age_seconds = max_age_seconds
self._buffer = deque()
self._lock = threading.Lock()
self._total_received = 0
self._total_kept = 0
def add(self, lat, lon, timestamp_ms, vessel_lat, vessel_lon):
"""Filter and add a strike if within radius. Returns True if kept."""
distance = haversine_miles(vessel_lat, vessel_lon, lat, lon)
if distance > self.radius_miles:
self._total_received += 1
return False
brg = bearing_degrees(vessel_lat, vessel_lon, lat, lon)
strike = Strike(
timestamp=timestamp_ms,
lat=lat,
lon=lon,
distance=distance,
bearing=brg,
)
with self._lock:
self._buffer.append(strike)
self._total_received += 1
self._total_kept += 1
self._prune()
return True
def _prune(self):
"""Remove strikes older than max_age_seconds. Must hold lock."""
cutoff_ms = (time.time() - self.max_age_seconds) * 1000
while self._buffer and self._buffer[0].timestamp < cutoff_ms:
self._buffer.popleft()
def get_strikes(self, since_seconds=None):
"""Return list of Strike objects, optionally filtered by age."""
with self._lock:
self._prune()
if since_seconds is None:
return list(self._buffer)
cutoff_ms = (time.time() - since_seconds) * 1000
return [s for s in self._buffer if s.timestamp >= cutoff_ms]
def clear(self):
with self._lock:
self._buffer.clear()
@property
def count(self):
with self._lock:
return len(self._buffer)
@property
def stats(self):
return {
'total_received': self._total_received,
'total_kept': self._total_kept,
'buffered': self.count,
}