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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
package org.terst.nav
import android.util.Log
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.location.Location
import android.os.IBinder
import android.os.Looper
import androidx.core.app.NotificationCompat
import com.google.android.gms.location.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import org.terst.nav.nmea.NmeaParser
import org.terst.nav.nmea.NmeaStreamManager
import org.terst.nav.sensors.DepthData
import org.terst.nav.sensors.HeadingData
import org.terst.nav.sensors.WindData
import org.terst.nav.gps.GpsPosition
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
data class GpsData(
val latitude: Double,
val longitude: Double,
val speedOverGround: Float, // m/s
val courseOverGround: Float // degrees
) {
fun toLocation(): Location {
val location = Location("GpsData")
location.latitude = latitude
location.longitude = longitude
location.speed = speedOverGround
location.bearing = courseOverGround
return location
}
}
class LocationService : Service() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var locationCallback: LocationCallback
private lateinit var anchorAlarmManager: AnchorAlarmManager
private lateinit var barometerSensorManager: BarometerSensorManager
private lateinit var nmeaParser: NmeaParser
private lateinit var nmeaStreamManager: NmeaStreamManager
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val NOTIFICATION_CHANNEL_ID = "location_service_channel"
private val NOTIFICATION_ID = 123
private var isAlarmTriggered = false // To prevent repeated alarm triggering
override fun onCreate() {
super.onCreate()
Log.d("LocationService", "Service created")
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
anchorAlarmManager = AnchorAlarmManager(this) // Initialize with service context
barometerSensorManager = BarometerSensorManager(this)
nmeaParser = NmeaParser()
nmeaStreamManager = NmeaStreamManager(nmeaParser, serviceScope)
createNotificationChannel()
// Observe barometer status and update our public state
serviceScope.launch {
barometerSensorManager.barometerStatus.collect { status ->
_barometerStatus.value = status
}
}
// Collect NMEA GPS positions
serviceScope.launch {
nmeaStreamManager.nmeaGpsPosition.collectLatest { gpsPosition ->
_nmeaGpsPositionFlow.emit(gpsPosition)
}
}
// Collect NMEA Wind Data
serviceScope.launch {
nmeaStreamManager.nmeaWindData.collectLatest { windData ->
_nmeaWindDataFlow.emit(windData)
}
}
// Collect NMEA Depth Data
serviceScope.launch {
nmeaStreamManager.nmeaDepthData.collectLatest { depthData ->
_nmeaDepthDataFlow.emit(depthData)
}
}
// Collect NMEA Heading Data
serviceScope.launch {
nmeaStreamManager.nmeaHeadingData.collectLatest { headingData ->
_nmeaHeadingDataFlow.emit(headingData)
}
}
// Mock tidal current data generator
serviceScope.launch {
while (true) {
val currents = MockTidalCurrentGenerator.generateMockCurrents()
_tidalCurrentState.update { it.copy(currents = currents) }
kotlinx.coroutines.delay(60000) // Update every minute
}
}
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
locationResult.lastLocation?.let { location ->
val gpsData = GpsData(
latitude = location.latitude,
longitude = location.longitude,
speedOverGround = location.speed,
courseOverGround = location.bearing
)
serviceScope.launch {
_locationFlow.emit(gpsData) // Emit to shared flow (Android system GPS)
}
// Check for anchor drag if anchor watch is active
checkAnchorDrag(location)
}
}
}
}
/**
* Checks if the current location is outside the anchor watch circle.
*/
private fun checkAnchorDrag(location: Location) {
_anchorWatchState.update { currentState ->
if (currentState.isActive && currentState.anchorLocation != null) {
val isDragging = currentState.isDragging(location)
if (isDragging) {
Log.w("AnchorWatch", "!!! ANCHOR DRAG DETECTED !!! Distance: ${currentState.anchorLocation.distanceTo(location)}m, Radius: ${currentState.watchCircleRadiusMeters}m")
if (!isAlarmTriggered) {
anchorAlarmManager.startAlarm()
isAlarmTriggered = true
}
} else {
Log.d("AnchorWatch", "Anchor holding. Distance: ${currentState.anchorLocation.distanceTo(location)}m, Radius: ${currentState.watchCircleRadiusMeters}m")
if (isAlarmTriggered) {
anchorAlarmManager.stopAlarm()
isAlarmTriggered = false
}
}
} else {
// If anchor watch is not active, ensure alarm is stopped
if (isAlarmTriggered) {
anchorAlarmManager.stopAlarm()
isAlarmTriggered = false
}
}
currentState
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_START_FOREGROUND_SERVICE -> {
Log.d("LocationService", "Starting foreground service")
startForeground(NOTIFICATION_ID, createNotification())
serviceScope.launch {
_currentPowerMode.emit(PowerMode.FULL) // Set initial power mode to FULL
startLocationUpdatesInternal(PowerMode.FULL)
}
barometerSensorManager.start()
nmeaStreamManager.start(NMEA_GATEWAY_IP, NMEA_GATEWAY_PORT)
}
ACTION_STOP_FOREGROUND_SERVICE -> {
Log.d("LocationService", "Stopping foreground service")
stopLocationUpdatesInternal()
barometerSensorManager.stop()
nmeaStreamManager.stop()
stopSelf()
}
ACTION_START_ANCHOR_WATCH -> {
Log.d("LocationService", "Received ACTION_START_ANCHOR_WATCH")
val radius = intent.getDoubleExtra(EXTRA_WATCH_RADIUS, AnchorWatchState.DEFAULT_WATCH_CIRCLE_RADIUS_METERS)
serviceScope.launch {
startAnchorWatch(radius)
setPowerMode(PowerMode.ANCHOR_WATCH)
}
}
ACTION_STOP_ANCHOR_WATCH -> {
Log.d("LocationService", "Received ACTION_STOP_ANCHOR_WATCH")
stopAnchorWatch()
setPowerMode(PowerMode.FULL) // Revert to full power mode after stopping anchor watch
}
ACTION_UPDATE_WATCH_RADIUS -> {
Log.d("LocationService", "Received ACTION_UPDATE_WATCH_RADIUS")
val radius = intent.getDoubleExtra(EXTRA_WATCH_RADIUS, AnchorWatchState.DEFAULT_WATCH_CIRCLE_RADIUS_METERS)
updateWatchCircleRadius(radius)
}
ACTION_TOGGLE_TIDAL_VISIBILITY -> {
val isVisible = intent.getBooleanExtra(EXTRA_TIDAL_VISIBILITY, false)
_tidalCurrentState.update { it.copy(isVisible = isVisible) }
}
}
return START_NOT_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null // Not a bound service
}
override fun onDestroy() {
super.onDestroy()
Log.d("LocationService", "Service destroyed")
stopLocationUpdatesInternal()
anchorAlarmManager.stopAlarm()
barometerSensorManager.stop()
nmeaStreamManager.stop() // Stop NMEA stream when service is destroyed
_anchorWatchState.value = AnchorWatchState(isActive = false)
isAlarmTriggered = false // Reset alarm trigger state
serviceScope.cancel() // Cancel the coroutine scope
}
@SuppressLint("MissingPermission")
private fun startLocationUpdatesInternal(powerMode: PowerMode) {
Log.d("LocationService", "Requesting location updates with PowerMode: ${powerMode.name}, interval: ${powerMode.gpsUpdateIntervalMillis}ms")
val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, powerMode.gpsUpdateIntervalMillis)
.setMinUpdateIntervalMillis(powerMode.gpsUpdateIntervalMillis / 2) // Half the interval for minUpdateInterval
.build()
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
private fun stopLocationUpdatesInternal() {
Log.d("LocationService", "Removing location updates")
fusedLocationClient.removeLocationUpdates(locationCallback)
}
fun setPowerMode(powerMode: PowerMode) {
serviceScope.launch {
if (_currentPowerMode.value != powerMode) {
// Emit the new power mode first
_currentPowerMode.emit(powerMode)
Log.d("LocationService", "Power mode changing to ${powerMode.name}. Restarting location updates.")
// Stop current updates if running
stopLocationUpdatesInternal()
// Start new updates with the new power mode's interval
startLocationUpdatesInternal(powerMode)
} else {
Log.d("LocationService", "Power mode already ${powerMode.name}. No change needed.")
}
}
}
private fun createNotificationChannel() {
val serviceChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Location Service Channel",
NotificationManager.IMPORTANCE_LOW
)
val manager = getSystemService(NotificationManager::class.java) as NotificationManager
manager.createNotificationChannel(serviceChannel)
}
private fun createNotification(): Notification {
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0,
notificationIntent,
PendingIntent.FLAG_IMMUTABLE
)
return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle("Sailing Companion")
.setContentText("Tracking your location in the background...")
.setSmallIcon(R.drawable.ic_anchor)
.setContentIntent(pendingIntent)
.build()
}
/**
* Starts the anchor watch with the current location as the anchor point.
* @param radiusMeters The watch circle radius in meters.
*/
@SuppressLint("MissingPermission")
suspend fun startAnchorWatch(radiusMeters: Double = AnchorWatchState.DEFAULT_WATCH_CIRCLE_RADIUS_METERS) {
val lastLocation = fusedLocationClient.lastLocation.await()
lastLocation?.let { location ->
_anchorWatchState.update { AnchorWatchState(
anchorLocation = location,
watchCircleRadiusMeters = radiusMeters,
setTimeMillis = System.currentTimeMillis(),
isActive = true
) }
Log.i("AnchorWatch", "Anchor watch started at lat: ${location.latitude}, lon: ${location.longitude} with radius: ${radiusMeters}m")
} ?: run {
Log.e("AnchorWatch", "Could not start anchor watch: Last known location is null.")
// Handle error, e.g., show a toast to the user
}
}
/**
* Stops the anchor watch.
*/
fun stopAnchorWatch() {
_anchorWatchState.update { AnchorWatchState(isActive = false) }
Log.i("AnchorWatch", "Anchor watch stopped.")
anchorAlarmManager.stopAlarm()
isAlarmTriggered = false
}
/**
* Updates the watch circle radius.
*/
fun updateWatchCircleRadius(radiusMeters: Double) {
_anchorWatchState.update { it.copy(watchCircleRadiusMeters = radiusMeters) }
Log.d("AnchorWatch", "Watch circle radius updated to ${radiusMeters}m.")
}
companion object {
const val ACTION_START_FOREGROUND_SERVICE = "ACTION_START_FOREGROUND_SERVICE"
const val ACTION_STOP_FOREGROUND_SERVICE = "ACTION_STOP_FOREGROUND_SERVICE"
const val ACTION_START_ANCHOR_WATCH = "ACTION_START_ANCHOR_WATCH"
const val ACTION_STOP_ANCHOR_WATCH = "ACTION_STOP_ANCHOR_WATCH"
const val ACTION_UPDATE_WATCH_RADIUS = "ACTION_UPDATE_WATCH_RADIUS"
const val ACTION_TOGGLE_TIDAL_VISIBILITY = "ACTION_TOGGLE_TIDAL_VISIBILITY"
const val EXTRA_WATCH_RADIUS = "extra_watch_radius"
const val EXTRA_TIDAL_VISIBILITY = "extra_tidal_visibility"
// NMEA Gateway configuration (example values - these should ideally be configurable by the user)
private const val NMEA_GATEWAY_IP = "192.168.1.1" // Placeholder IP address
private const val NMEA_GATEWAY_PORT = 10110 // Default NMEA port
// Publicly accessible flows
val locationFlow: SharedFlow<GpsData>
get() = _locationFlow
val anchorWatchState: StateFlow<AnchorWatchState>
get() = _anchorWatchState
val tidalCurrentState: StateFlow<TidalCurrentState>
get() = _tidalCurrentState
val barometerStatus: StateFlow<BarometerStatus>
get() = _barometerStatus
// NMEA Data Flows
val nmeaGpsPositionFlow: SharedFlow<GpsPosition>
get() = _nmeaGpsPositionFlow
val nmeaWindDataFlow: SharedFlow<WindData>
get() = _nmeaWindDataFlow
val nmeaDepthDataFlow: SharedFlow<DepthData>
get() = _nmeaDepthDataFlow
val nmeaHeadingDataFlow: SharedFlow<HeadingData>
get() = _nmeaHeadingDataFlow
private val _locationFlow = MutableSharedFlow<GpsData>(replay = 1)
private val _anchorWatchState = MutableStateFlow(AnchorWatchState())
private val _tidalCurrentState = MutableStateFlow(TidalCurrentState())
private val _barometerStatus = MutableStateFlow(BarometerStatus())
// Private NMEA Data Flows
private val _nmeaGpsPositionFlow = MutableSharedFlow<GpsPosition>(
replay = 0, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
)
private val _nmeaWindDataFlow = MutableSharedFlow<WindData>(
replay = 0, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
)
private val _nmeaDepthDataFlow = MutableSharedFlow<DepthData>(
replay = 0, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
)
private val _nmeaHeadingDataFlow = MutableSharedFlow<HeadingData>(
replay = 0, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
)
private val _currentPowerMode = MutableStateFlow(PowerMode.FULL)
val currentPowerMode: StateFlow<PowerMode>
get() = _currentPowerMode
}
}
|