feat: add Room database with LocationDao and EventLogDao
This commit is contained in:
parent
8611df1c75
commit
1d6aad599c
5 changed files with 117 additions and 0 deletions
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.traccar.traccar_client.storage
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
|
||||||
|
@Database(
|
||||||
|
entities = [LocationEntity::class, EventLogEntity::class],
|
||||||
|
version = 1,
|
||||||
|
exportSchema = false
|
||||||
|
)
|
||||||
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
abstract fun locationDao(): LocationDao
|
||||||
|
abstract fun eventLogDao(): EventLogDao
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@Volatile
|
||||||
|
private var INSTANCE: AppDatabase? = null
|
||||||
|
|
||||||
|
fun getInstance(context: Context): AppDatabase {
|
||||||
|
return INSTANCE ?: synchronized(this) {
|
||||||
|
val instance = Room.databaseBuilder(
|
||||||
|
context.applicationContext,
|
||||||
|
AppDatabase::class.java,
|
||||||
|
"traccar_client_db"
|
||||||
|
).build()
|
||||||
|
INSTANCE = instance
|
||||||
|
instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.traccar.traccar_client.storage
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface EventLogDao {
|
||||||
|
@Insert
|
||||||
|
suspend fun insert(entry: EventLogEntity): Long
|
||||||
|
|
||||||
|
@Query("SELECT * FROM event_log ORDER BY timestamp DESC LIMIT 100")
|
||||||
|
fun getRecentEvents(): Flow<List<EventLogEntity>>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM event_log ORDER BY timestamp DESC LIMIT :limit")
|
||||||
|
suspend fun getRecentEventsList(limit: Int): List<EventLogEntity>
|
||||||
|
|
||||||
|
@Query("DELETE FROM event_log WHERE timestamp < :beforeTimestamp")
|
||||||
|
suspend fun deleteOlderThan(beforeTimestamp: Long)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.traccar.traccar_client.storage
|
||||||
|
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity(tableName = "event_log")
|
||||||
|
data class EventLogEntity(
|
||||||
|
@PrimaryKey(autoGenerate = true)
|
||||||
|
val id: Long = 0,
|
||||||
|
val timestamp: Long,
|
||||||
|
val eventType: String,
|
||||||
|
val message: String
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.traccar.traccar_client.storage
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface LocationDao {
|
||||||
|
@Insert
|
||||||
|
suspend fun insert(location: LocationEntity): Long
|
||||||
|
|
||||||
|
@Query("SELECT * FROM locations WHERE synced = 0 ORDER BY timestamp ASC")
|
||||||
|
suspend fun getUnsyncedLocations(): List<LocationEntity>
|
||||||
|
|
||||||
|
@Query("UPDATE locations SET synced = 1 WHERE id = :id")
|
||||||
|
suspend fun markAsSynced(id: Long)
|
||||||
|
|
||||||
|
@Query("UPDATE locations SET synced = 1 WHERE id IN (:ids)")
|
||||||
|
suspend fun markAllAsSynced(ids: List<Long>)
|
||||||
|
|
||||||
|
@Query("SELECT * FROM locations ORDER BY timestamp DESC LIMIT 1")
|
||||||
|
fun getLastLocation(): Flow<LocationEntity?>
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(*) FROM locations WHERE synced = 0")
|
||||||
|
suspend fun getUnsyncedCount(): Int
|
||||||
|
|
||||||
|
@Query("DELETE FROM locations WHERE synced = 1 AND timestamp < :beforeTimestamp")
|
||||||
|
suspend fun deleteSyncedOlderThan(beforeTimestamp: Long)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.traccar.traccar_client.storage
|
||||||
|
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity(tableName = "locations")
|
||||||
|
data class LocationEntity(
|
||||||
|
@PrimaryKey(autoGenerate = true)
|
||||||
|
val id: Long = 0,
|
||||||
|
val timestamp: Long,
|
||||||
|
val latitude: Double,
|
||||||
|
val longitude: Double,
|
||||||
|
val accuracy: Float?,
|
||||||
|
val speed: Float?,
|
||||||
|
val heading: Float?,
|
||||||
|
val altitude: Double?,
|
||||||
|
val isMoving: Boolean,
|
||||||
|
val synced: Boolean = false
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue