diff options
Diffstat (limited to 'android-app/app/src/main')
| -rw-r--r-- | android-app/app/src/main/kotlin/com/example/androidapp/gps/LocationService.kt | 99 |
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? +) |
