feat: CAM-based use case detection engine, decouple DENM from sensor triggers
Added: - UseCaseDetectionEngine: IMA-B, IMA-S, RTW-B, LTW-B, SMVA/BCW-B via per-station CAM history, yaw-rate/heading-trend turn detection, Info/Awareness/Warning alerts - Ego state from obu_gnss (~4Hz, yaw rate) with phone GPS fallback when stale - Human-readable alert narratives + technical detail in V2X Monitor - Settings > Use Case Alerts per-use-case toggles Fixed: - DENM no longer auto-fires on braking/stopping; manual-only (antenna/RSU test) - Removed unwired CamBuilder.kt (dead code, contradicted CAM-only architecture) - TripRecordingService no longer depends on MQTT/OBU connectivity
This commit is contained in:
@@ -5,6 +5,7 @@ import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.content.pm.ServiceInfo
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
@@ -12,6 +13,7 @@ import android.hardware.SensorManager
|
||||
import android.location.Location
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
import com.google.android.gms.location.LocationCallback
|
||||
@@ -26,6 +28,7 @@ import com.hawhamburg.micr0bu.data.db.AppDatabase
|
||||
import com.hawhamburg.micr0bu.domain.detection.DetectionConfig
|
||||
import com.hawhamburg.micr0bu.domain.detection.EventDetector
|
||||
import com.hawhamburg.micr0bu.domain.detection.EventType
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -44,7 +47,16 @@ import kotlin.math.sqrt
|
||||
* • Accelerometer + Gyroscope at [SensorManager.SENSOR_DELAY_GAME] (~50 Hz)
|
||||
* • FusedLocationProviderClient at 1 Hz
|
||||
* • Accel/gyro magnitudes + last GPS state → [EventDetector] → Room via [TripRepository]
|
||||
*
|
||||
* Per the requirements doc (Section 11), Phase A is independent of OBU connectivity — no
|
||||
* cable, no MQTT broker, no hardware required — so this service has no MQTT/DENM dependency.
|
||||
* Detected events are recorded for trip history/CSV only; a future phase will feed them into
|
||||
* the CAM-based Use Case Detection Engine (`com.hawhamburg.micr0bu.domain.usecase`) as
|
||||
* corroborating signals (Section 11.4). No DENM is triggered by detected events — the only
|
||||
* way to send a DENM is the manual "Send Test DENM" button in the V2X Monitor screen, kept
|
||||
* solely to verify the OBU↔RSU antenna link independent of any detected event.
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class TripRecordingService : Service() {
|
||||
|
||||
// ── Constants ─────────────────────────────────────────────────────────────
|
||||
@@ -66,6 +78,11 @@ class TripRecordingService : Service() {
|
||||
private val serviceScope = CoroutineScope(Dispatchers.Default + serviceJob)
|
||||
private var timerJob: Job? = null
|
||||
|
||||
// Held only while a trip is actively recording — keeps the CPU alive for sensor
|
||||
// and GPS callbacks when the screen is off. Released immediately on stop so the
|
||||
// phone can sleep normally when the app is idle.
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
// ── Dependencies ──────────────────────────────────────────────────────────
|
||||
|
||||
private lateinit var sensorManager: SensorManager
|
||||
@@ -207,6 +224,14 @@ class TripRecordingService : Service() {
|
||||
// ── Trip control ─────────────────────────────────────────────────────────
|
||||
|
||||
private fun startTrip() {
|
||||
// Keep the CPU awake for the duration of the trip so sensor and GPS callbacks
|
||||
// are delivered reliably when the screen is off. Released in stopTrip().
|
||||
val pm = getSystemService(POWER_SERVICE) as PowerManager
|
||||
wakeLock = pm.newWakeLock(
|
||||
PowerManager.PARTIAL_WAKE_LOCK,
|
||||
"micr0bu:TripRecording",
|
||||
).also { it.acquire() }
|
||||
|
||||
detector.reset()
|
||||
brakingCount = 0
|
||||
turningCount = 0
|
||||
@@ -272,11 +297,21 @@ class TripRecordingService : Service() {
|
||||
registerSensors()
|
||||
requestLocationUpdates()
|
||||
|
||||
// Start foreground ASAP (within 5 s required by Android)
|
||||
startForeground(NOTIFICATION_ID, buildNotification(0L, 0))
|
||||
// Start foreground ASAP (within 5 s required by Android).
|
||||
// Specify FOREGROUND_SERVICE_TYPE_LOCATION so Android 10+ knows why we need
|
||||
// continuous location access, and so the notification is re-posted automatically
|
||||
// if the user dismisses it (Android 13+ allows users to swipe foreground
|
||||
// service notifications away, but the system re-shows them after ~1 s).
|
||||
startForeground(
|
||||
NOTIFICATION_ID,
|
||||
buildNotification(0L),
|
||||
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION,
|
||||
)
|
||||
}
|
||||
|
||||
private fun stopTrip() {
|
||||
wakeLock?.let { if (it.isHeld) it.release() }
|
||||
wakeLock = null
|
||||
timerJob?.cancel()
|
||||
unregisterSensors()
|
||||
removeLocationUpdates()
|
||||
@@ -377,7 +412,7 @@ class TripRecordingService : Service() {
|
||||
.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
private fun buildNotification(elapsedSeconds: Long, eventCount: Int) =
|
||||
private fun buildNotification(elapsedSeconds: Long) =
|
||||
NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("Recording trip")
|
||||
.setContentText(
|
||||
@@ -390,17 +425,22 @@ class TripRecordingService : Service() {
|
||||
.setContentIntent(
|
||||
PendingIntent.getActivity(
|
||||
this, 0,
|
||||
Intent(this, MainActivity::class.java),
|
||||
PendingIntent.FLAG_IMMUTABLE,
|
||||
Intent(this, MainActivity::class.java).apply {
|
||||
// Bring the existing activity to the front rather than
|
||||
// launching a second instance on top of the running one.
|
||||
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
|
||||
Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
},
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||
)
|
||||
)
|
||||
.setAutoCancel(false)
|
||||
.build()
|
||||
|
||||
private fun updateNotification() {
|
||||
val elapsed = TripServiceBus.state.value.elapsedSeconds
|
||||
val total = brakingCount + turningCount + stoppingCount
|
||||
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
nm.notify(NOTIFICATION_ID, buildNotification(elapsed, total))
|
||||
nm.notify(NOTIFICATION_ID, buildNotification(elapsed))
|
||||
}
|
||||
|
||||
private fun formatElapsed(seconds: Long): String {
|
||||
|
||||
Reference in New Issue
Block a user