summaryrefslogtreecommitdiff
path: root/android-app/app/src/main/kotlin/com
diff options
context:
space:
mode:
authorClaude Agent <agent@claude.ai>2026-03-25 01:57:17 +0000
committerPeter Stone <thepeterstone@gmail.com>2026-03-25 04:55:58 +0000
commit0294c6fccc5a1dac7d4fb0ac084b273683e47d32 (patch)
treecd2f23567324a1881835444ab4022efd6e2ed575 /android-app/app/src/main/kotlin/com
parente5cd0ce6bf65fff1bbbb5d8e12c4076da088ebe1 (diff)
feat(safety): log wind and current conditions at MOB activation (Section 4.6)
Per COMPONENT_DESIGN.md Section 4.6, the MOB navigation view must display wind and current conditions at the time of the event. - MobEvent: add nullable windSpeedKt, windDirectionDeg, currentSpeedKt, currentDirectionDeg fields captured at the exact moment of activation - MobAlarmManager.activate(): accept optional wind/current params and forward them into MobEvent (defaults to null for backward compatibility) - LocationService (new): aggregates live SensorData (resolves true wind via TrueWindCalculator) and marine-forecast current conditions; snapshot() provides a point-in-time EnvironmentalSnapshot for safety-critical logging - MobAlarmManagerTest: add tests for wind/current storage and null defaults - LocationServiceTest (new): covers snapshot, true-wind resolution, current-condition updates, and the latestSensor flow Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'android-app/app/src/main/kotlin/com')
-rw-r--r--android-app/app/src/main/kotlin/com/example/androidapp/gps/LocationService.kt99
1 files changed, 99 insertions, 0 deletions
diff --git a/android-app/app/src/main/kotlin/com/example/androidapp/gps/LocationService.kt b/android-app/app/src/main/kotlin/com/example/androidapp/gps/LocationService.kt
new file mode 100644
index 0000000..c6ff8b7
--- /dev/null
+++ b/android-app/app/src/main/kotlin/com/example/androidapp/gps/LocationService.kt
@@ -0,0 +1,99 @@
+package com.example.androidapp.gps
+
+import com.example.androidapp.data.model.SensorData
+import com.example.androidapp.wind.TrueWindCalculator
+import com.example.androidapp.wind.ApparentWind
+import com.example.androidapp.wind.TrueWindData
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
+
+/**
+ * Aggregates real-time location and environmental sensor data for use throughout
+ * the safety subsystem (Section 4.6 of COMPONENT_DESIGN.md).
+ *
+ * Call [updateSensorData] whenever new NMEA or Signal K data arrives and
+ * [updateCurrentConditions] when a fresh marine-forecast response is received.
+ * Use [snapshot] to capture a point-in-time reading at safety-critical moments
+ * such as MOB activation.
+ */
+class LocationService(
+ private val windCalculator: TrueWindCalculator = TrueWindCalculator()
+) {
+
+ private val _latestSensor = MutableStateFlow<SensorData?>(null)
+ /** The most recently received unified sensor reading. */
+ val latestSensor: StateFlow<SensorData?> = _latestSensor.asStateFlow()
+
+ private val _latestTrueWind = MutableStateFlow<TrueWindData?>(null)
+ /** Most recent resolved true-wind vector, updated whenever a full sensor reading arrives. */
+ val latestTrueWind: StateFlow<TrueWindData?> = _latestTrueWind.asStateFlow()
+
+ private val _currentSpeedKt = MutableStateFlow<Double?>(null)
+ private val _currentDirectionDeg = MutableStateFlow<Double?>(null)
+
+ /**
+ * Ingest a new sensor reading. If the reading carries apparent wind, boat speed,
+ * and heading, true wind is resolved immediately via [TrueWindCalculator] and
+ * stored in [latestTrueWind].
+ */
+ fun updateSensorData(data: SensorData) {
+ _latestSensor.value = data
+
+ val aws = data.apparentWindSpeedKt
+ val awa = data.apparentWindAngleDeg
+ val bsp = data.speedOverGroundKt // use SOG as proxy when BSP is absent
+ val hdg = data.headingTrueDeg
+
+ if (aws != null && awa != null && bsp != null && hdg != null) {
+ _latestTrueWind.value = windCalculator.update(
+ apparent = ApparentWind(speedKt = aws, angleDeg = awa),
+ bsp = bsp,
+ hdgDeg = hdg
+ )
+ }
+ }
+
+ /**
+ * Update the ocean current conditions from the latest marine-forecast response.
+ *
+ * @param speedKt Current speed in knots (null to clear)
+ * @param directionDeg Direction the current flows TOWARD, in degrees (null to clear)
+ */
+ fun updateCurrentConditions(speedKt: Double?, directionDeg: Double?) {
+ _currentSpeedKt.value = speedKt
+ _currentDirectionDeg.value = directionDeg
+ }
+
+ /**
+ * Captures a snapshot of wind and current conditions at the current moment.
+ *
+ * All fields are nullable — only data that was available at snapshot time is
+ * populated. This snapshot is intended to be logged alongside a [MobEvent]
+ * at the instant of MOB activation.
+ */
+ fun snapshot(): EnvironmentalSnapshot {
+ val trueWind = _latestTrueWind.value
+ return EnvironmentalSnapshot(
+ windSpeedKt = trueWind?.speedKt,
+ windDirectionDeg = trueWind?.directionDeg,
+ currentSpeedKt = _currentSpeedKt.value,
+ currentDirectionDeg = _currentDirectionDeg.value
+ )
+ }
+}
+
+/**
+ * Point-in-time snapshot of wind and current conditions.
+ *
+ * @param windSpeedKt True Wind Speed in knots; null if sensors were unavailable.
+ * @param windDirectionDeg True Wind Direction (degrees true, wind comes FROM); null if unavailable.
+ * @param currentSpeedKt Ocean current speed in knots; null if forecast was unavailable.
+ * @param currentDirectionDeg Ocean current direction (degrees, flows TOWARD); null if unavailable.
+ */
+data class EnvironmentalSnapshot(
+ val windSpeedKt: Double?,
+ val windDirectionDeg: Double?,
+ val currentSpeedKt: Double?,
+ val currentDirectionDeg: Double?
+)