feat: improve logging and event filtering
- Add HTTP debug logging to TraccarHttpClient (URL, error body) - Route HTTP client logs through app event log system - Fix syncBufferedLocations to properly report partial failures - Add DEBUG event type with separate filter chip - Rename ALL filter to NORMAL, add VERBOSE (shows DEBUG, FILTERED, SPEED_CALC) - Add color/icon for INFO, NETWORK_CHANGE, DEBUG, SPEED_CALC events - Make log message text selectable
This commit is contained in:
parent
60d051ee7b
commit
09009fee4c
3 changed files with 57 additions and 16 deletions
|
|
@ -17,8 +17,9 @@ data class TraccarConfig(
|
|||
val password: String = ""
|
||||
)
|
||||
|
||||
class TraccarHttpClient {
|
||||
|
||||
class TraccarHttpClient(
|
||||
private val onLog: ((eventType: String, message: String) -> Unit)? = null
|
||||
) {
|
||||
private val client = OkHttpClient.Builder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
|
|
@ -28,15 +29,19 @@ class TraccarHttpClient {
|
|||
suspend fun sendLocation(config: TraccarConfig, location: Location): Result<Unit> = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val url = buildLocationUrl(config, location)
|
||||
onLog?.invoke("DEBUG", "HTTP → $url")
|
||||
val request = buildRequest(config, url)
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
if (response.isSuccessful) {
|
||||
Result.success(Unit)
|
||||
} else {
|
||||
val errorBody = response.body?.string() ?: "empty"
|
||||
onLog?.invoke("ERROR", "HTTP ${response.code}: ${response.message} | $errorBody")
|
||||
Result.failure(Exception("HTTP ${response.code}: ${response.message}"))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
onLog?.invoke("ERROR", "Network error: ${e.message}")
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
|
@ -49,6 +54,7 @@ class TraccarHttpClient {
|
|||
successCount++
|
||||
}
|
||||
}
|
||||
onLog?.invoke("SYNC", "Sent $successCount/${locations.size} locations")
|
||||
Result.success(successCount)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ class LocationTrackingService : Service() {
|
|||
distanceFilterProcessor = DistanceFilterProcessor()
|
||||
heartbeatScheduler = HeartbeatScheduler(this)
|
||||
database = AppDatabase.getInstance(this)
|
||||
httpClient = TraccarHttpClient()
|
||||
httpClient = TraccarHttpClient { eventType, message -> logEvent(eventType, message) }
|
||||
connectivityReceiver = ConnectivityReceiver { online ->
|
||||
isNetworkAvailable = online
|
||||
logEvent("NETWORK_CHANGE", "Network: ${if (online) "online" else "offline"}")
|
||||
|
|
@ -434,11 +434,13 @@ class LocationTrackingService : Service() {
|
|||
)
|
||||
}
|
||||
|
||||
val result = httpClient.sendLocations(traccarConfig, locations)
|
||||
if (result.isSuccess) {
|
||||
val ids = unsyncedLocations.map { it.id }
|
||||
val successCount = httpClient.sendLocations(traccarConfig, locations).getOrNull() ?: 0
|
||||
if (successCount > 0) {
|
||||
val ids = unsyncedLocations.take(successCount).map { it.id }
|
||||
database.locationDao().markAllAsSynced(ids)
|
||||
logEvent("SYNC", "Synced ${result.getOrNull()} buffered locations")
|
||||
logEvent("SYNC", "Synced $successCount buffered locations")
|
||||
} else {
|
||||
logEvent("ERROR", "Failed to sync any of ${unsyncedLocations.size} buffered locations")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue