From 21e16e27f047053582d8b0c722feedf3c8badebe Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sun, 16 Jan 2022 17:57:14 -0600 Subject: Refactored report types into commonMain, added support for event report types in controller and API --- .../android/details/reports/UnitReportsFragment.kt | 42 ++++++------ .../details/reports/UnitReportsViewModel.kt | 20 +++++- .../TrackerMap/client/apis/ReportsApi.kt | 10 ++- .../TrackerMap/client/models/EventInformation.kt | 79 +++++++++++++++++++++- .../TrackerMap/controllers/ReportController.kt | 14 ++-- 5 files changed, 136 insertions(+), 29 deletions(-) diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt index 3b3570b..4c87b31 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt @@ -205,26 +205,28 @@ class UnitReportsFragment : Fragment() { datetimeText.text = Formatter.formatDate(it) } event.event.type?.let { - eventText.text = getString(when (it) { - "deviceOnline" -> R.string.event_device_online - "deviceUnknown" -> R.string.event_device_unknown - "deviceOffline" -> R.string.event_device_offline - "deviceInactive" -> R.string.event_device_inactive - "deviceMoving" -> R.string.event_device_moving - "deviceStopped" -> R.string.event_device_stopped - "deviceOverspeed" -> R.string.event_device_overspeed - "deviceFuelDrop" -> R.string.event_device_fuel_drop - "commandResult" -> R.string.event_command_result - "geofenceEnter" -> R.string.event_geofence_enter - "geofenceExit" -> R.string.event_geofence_exit - "alarm" -> R.string.event_alarm - "ignitionOn" -> R.string.event_ignition_on - "ignitionOff" -> R.string.event_ignition_off - "maintenance" -> R.string.event_maintenance - "textMessage" -> R.string.event_text_message - "driverChanged" -> R.string.event_driver_changed - else -> R.string.event_unknown - }) + eventText.text = getString( + when (EventInformation.stringToReportType(it)) { + EventInformation.Type.DEVICE_ONLINE -> R.string.event_device_online + EventInformation.Type.DEVICE_UNKNOWN -> R.string.event_device_unknown + EventInformation.Type.DEVICE_OFFLINE -> R.string.event_device_offline + EventInformation.Type.DEVICE_INACTIVE -> R.string.event_device_inactive + EventInformation.Type.DEVICE_MOVING -> R.string.event_device_moving + EventInformation.Type.DEVICE_STOPPED -> R.string.event_device_stopped + EventInformation.Type.DEVICE_OVERSPEED -> R.string.event_device_overspeed + EventInformation.Type.DEVICE_FUEL_DROP -> R.string.event_device_fuel_drop + EventInformation.Type.COMMAND_RESULT -> R.string.event_command_result + EventInformation.Type.GEOFENCE_ENTER -> R.string.event_geofence_enter + EventInformation.Type.GEOFENCE_EXIT -> R.string.event_geofence_exit + EventInformation.Type.ALARM -> R.string.event_alarm + EventInformation.Type.IGNITION_ON -> R.string.event_ignition_on + EventInformation.Type.IGNITION_OFF -> R.string.event_ignition_off + EventInformation.Type.MAINTENANCE -> R.string.event_maintenance + EventInformation.Type.TEXT_MESSAGE -> R.string.event_text_message + EventInformation.Type.DRIVER_CHANGED -> R.string.event_driver_changed + EventInformation.Type.UNKNOWN -> R.string.event_unknown + else -> R.string.event_unknown + }) } event.geofence?.let { geofenceText.text = it.name diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt index 865f096..62ac4f2 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.* import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.models.EventInformation import mx.trackermap.TrackerMap.controllers.ReportController import mx.trackermap.TrackerMap.utils.ReportDates import org.koin.core.component.KoinComponent @@ -84,7 +85,24 @@ class UnitReportsViewModel( deviceId = id, reportType = _reportType.value, reportPeriod = _reportPeriod.value, - xlsx = xlsx + xlsx = xlsx, + eventTypes = arrayOf( + EventInformation.Type.DEVICE_INACTIVE, + EventInformation.Type.DEVICE_MOVING, + EventInformation.Type.DEVICE_STOPPED, + EventInformation.Type.DEVICE_OVERSPEED, + EventInformation.Type.DEVICE_FUEL_DROP, + EventInformation.Type.COMMAND_RESULT, + EventInformation.Type.GEOFENCE_ENTER, + EventInformation.Type.GEOFENCE_EXIT, + EventInformation.Type.ALARM, + EventInformation.Type.IGNITION_ON, + EventInformation.Type.IGNITION_OFF, + EventInformation.Type.MAINTENANCE, + EventInformation.Type.TEXT_MESSAGE, + EventInformation.Type.DRIVER_CHANGED, + EventInformation.Type.UNKNOWN + ) ) } } diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt index b4d6f74..3765207 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/ReportsApi.kt @@ -36,12 +36,14 @@ class ReportsApi(basePath: String = "https://demo.traccar.org/api") : ApiClient( from: String, to: String, deviceId: Int, + type: String = "%", xlsx: Boolean = false ): Any { val localVariableQuery: MultiValueMap = mapOf( "deviceId" to toMultiValue(listOf(deviceId), "multi"), "from" to listOf(from), - "to" to listOf(to) + "to" to listOf(to), + "type" to listOf(type) ) val localVariableHeaders = mutableMapOf() if (xlsx) { @@ -79,18 +81,20 @@ class ReportsApi(basePath: String = "https://demo.traccar.org/api") : ApiClient( suspend fun reportsEventsGet( from: String, to: String, + type: String = "%", deviceId: Int, ): Array { - return reportsEventsGetBase(from, to, deviceId, false) as Array + return reportsEventsGetBase(from, to, deviceId, type, false) as Array } @Suppress("UNCHECKED_CAST") suspend fun reportsEventsGetXlsx( from: String, to: String, + type: String = "%", deviceId: Int, ): ByteArray { - return reportsEventsGetBase(from, to, deviceId, true) as ByteArray + return reportsEventsGetBase(from, to, deviceId, type, true) as ByteArray } /** diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt index c4f91fe..befd8f1 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt @@ -4,4 +4,81 @@ data class EventInformation( val event: Event, val position: Position?, val geofence: Geofence? -) +) { + enum class Type { + ALL, + DEVICE_ONLINE, + DEVICE_UNKNOWN, + DEVICE_OFFLINE, + DEVICE_INACTIVE, + DEVICE_MOVING, + DEVICE_STOPPED, + DEVICE_OVERSPEED, + DEVICE_FUEL_DROP, + COMMAND_RESULT, + GEOFENCE_ENTER, + GEOFENCE_EXIT, + ALARM, + IGNITION_ON, + IGNITION_OFF, + MAINTENANCE, + TEXT_MESSAGE, + DRIVER_CHANGED, + UNKNOWN + } + + companion object { + fun reportTypeToString(t: Type) = + when (t) { + Type.ALL -> "%" + Type.DEVICE_ONLINE -> "deviceOnline" + Type.DEVICE_UNKNOWN -> "deviceUnknown" + Type.DEVICE_OFFLINE -> "deviceOffline" + Type.DEVICE_INACTIVE -> "deviceInactive" + Type.DEVICE_MOVING -> "deviceMoving" + Type.DEVICE_STOPPED -> "deviceStopped" + Type.DEVICE_OVERSPEED -> "deviceOverspeed" + Type.DEVICE_FUEL_DROP -> "deviceFuelDrop" + Type.COMMAND_RESULT -> "commandResult" + Type.GEOFENCE_ENTER -> "geofenceEnter" + Type.GEOFENCE_EXIT -> "geofenceExit" + Type.ALARM -> "alarm" + Type.IGNITION_ON -> "ignitionOn" + Type.IGNITION_OFF -> "ignitionOff" + Type.MAINTENANCE -> "maintenance" + Type.TEXT_MESSAGE -> "textMessage" + Type.DRIVER_CHANGED -> "driverChanged" + Type.UNKNOWN -> "unknown" + } + + fun reportTypesToString(t: Array) = + if (t.isEmpty() || t.contains(Type.ALL)) { + reportTypeToString(Type.ALL) + } else { + t.joinToString(",", transform = this::reportTypeToString) + } + + fun stringToReportType(s: String) = + when (s) { + "deviceOnline" -> Type.DEVICE_ONLINE + "deviceUnknown" -> Type.DEVICE_UNKNOWN + "deviceOffline" -> Type.DEVICE_OFFLINE + "deviceInactive" -> Type.DEVICE_INACTIVE + "deviceMoving" -> Type.DEVICE_MOVING + "deviceStopped" -> Type.DEVICE_STOPPED + "deviceOverspeed" -> Type.DEVICE_OVERSPEED + "deviceFuelDrop" -> Type.DEVICE_FUEL_DROP + "commandResult" -> Type.COMMAND_RESULT + "geofenceEnter" -> Type.GEOFENCE_ENTER + "geofenceExit" -> Type.GEOFENCE_EXIT + "alarm" -> Type.ALARM + "ignitionOn" -> Type.IGNITION_ON + "ignitionOff" -> Type.IGNITION_OFF + "maintenance" -> Type.MAINTENANCE + "textMessage" -> Type.TEXT_MESSAGE + "driverChanged" -> Type.DRIVER_CHANGED + "unknown" -> Type.UNKNOWN + else -> Type.UNKNOWN + } + } +} diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt index 2151331..8a7f527 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt @@ -36,7 +36,8 @@ class ReportController( deviceId: Int, reportType: ReportType?, reportPeriod: ReportDates.ReportPeriod?, - xlsx: Boolean = false + xlsx: Boolean = false, + eventTypes: Array = arrayOf() ) { if (reportType == null || reportPeriod == null) { return @@ -54,7 +55,7 @@ class ReportController( GlobalScope.launch { when (reportType) { ReportType.POSITIONS -> fetchPositions(deviceId, previousDate, currentDate, xlsx) - ReportType.EVENTS -> fetchEvents(deviceId, previousDate, currentDate, xlsx) + ReportType.EVENTS -> fetchEvents(deviceId, previousDate, currentDate, eventTypes, xlsx) ReportType.STOPS -> fetchStops(deviceId, previousDate, currentDate, xlsx) } } @@ -84,13 +85,16 @@ class ReportController( deviceId: Int, from: String, to: String, + types: Array, xlsx: Boolean ) { Log.d("UnitReportsVM", "Fetching events") if (!xlsx) { val positionsResult = reportsApi.reportsRouteGet(from, to, deviceId) - val eventsResult = reportsApi.reportsEventsGet(from, to, deviceId) + val eventsResult = reportsApi.reportsEventsGet( + from, to, EventInformation.reportTypesToString(types), deviceId + ) val geofencesResult = geofencesApi.geofencesGet(all = true) val result = mutableListOf() @@ -105,7 +109,9 @@ class ReportController( Log.d("UnitReportsVM", "Events report: $result") reportFlow.value = Report.EventsReport(result.toTypedArray()) } else { - val result = reportsApi.reportsEventsGetXlsx(from, to, deviceId) + val result = reportsApi.reportsEventsGetXlsx( + from, to, EventInformation.reportTypesToString(types), deviceId + ) Log.d("UnitReportsVM", "Events report: $result") reportFlow.value = Report.XlsxReport(result) -- cgit v1.2.3