feat: add getLogs method to BridgeModule for fetching event logs

This commit is contained in:
fiatcode 2026-04-30 13:26:23 +07:00
parent cd34c8bbf3
commit 3ae8bf00c1
No known key found for this signature in database

View file

@ -2,11 +2,16 @@ package com.traccar.traccar_client
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Handler
import android.os.Looper
import com.traccar.traccar_client.service.LocationTrackingService import com.traccar.traccar_client.service.LocationTrackingService
import com.traccar.traccar_client.storage.AppDatabase
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Dispatchers
class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler { class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
@ -14,6 +19,7 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
private lateinit var eventChannel: EventChannel private lateinit var eventChannel: EventChannel
private var context: Context? = null private var context: Context? = null
private lateinit var prefs: SharedPreferences private lateinit var prefs: SharedPreferences
private val mainHandler = Handler(Looper.getMainLooper())
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
context = binding.applicationContext context = binding.applicationContext
@ -66,6 +72,10 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
reportLocationNow() reportLocationNow()
result.success(true) result.success(true)
} }
"getLogs" -> {
val limit = (call.arguments as? Number)?.toInt() ?: 100
fetchLogs(limit, result)
}
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
@ -118,4 +128,30 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
} }
context?.startService(intent) context?.startService(intent)
} }
private fun fetchLogs(limit: Int, result: MethodChannel.Result) {
Thread {
try {
val logs = runBlocking(Dispatchers.IO) {
val db = context?.let { AppDatabase.getInstance(it) }
if (db != null) {
val events = db.eventLogDao().getRecentEventsList(limit)
events.map { event ->
mapOf(
"id" to event.id,
"timestamp" to event.timestamp,
"eventType" to event.eventType,
"message" to event.message
)
}
} else {
emptyList()
}
}
mainHandler.post { result.success(logs) }
} catch (e: Exception) {
mainHandler.post { result.success(emptyList<Map<String, Any>>()) }
}
}.start()
}
} }