Skip to content

Commit

Permalink
feat: vehicle status reading
Browse files Browse the repository at this point in the history
Add vehicle status reading, and ability to close the session when key is off
  • Loading branch information
tzebrowski authored Jan 23, 2025
1 parent 36a2657 commit 7f7ed6f
Show file tree
Hide file tree
Showing 37 changed files with 763 additions and 349 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/obd/graphs/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.obd.graphs.*
import org.obd.graphs.bl.datalogger.dataLogger
import org.obd.graphs.bl.drag.dragRacingMetricsProcessor
import org.obd.graphs.bl.extra.vehicleStatusMetricsProcessor
import org.obd.graphs.bl.generator.MetricsGenerator
import org.obd.graphs.bl.trip.tripManager
import org.obd.graphs.profile.profile
Expand Down Expand Up @@ -228,6 +229,7 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {
dataLogger
.observe(dragRacingMetricsProcessor)
.observe(tripManager)
.observe(vehicleStatusMetricsProcessor)

if (BuildConfig.DEBUG){
dataLogger.observe(MetricsGenerator(BuildConfig.DEBUG))
Expand Down
37 changes: 34 additions & 3 deletions app/src/main/java/org/obd/graphs/activity/Receivers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.core.view.isVisible
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.obd.graphs.*
import org.obd.graphs.bl.datalogger.*
import org.obd.graphs.bl.extra.*
import org.obd.graphs.preferences.PREFS_CONNECTION_TYPE_CHANGED_EVENT
import org.obd.graphs.preferences.Prefs
import org.obd.graphs.preferences.isEnabled
Expand All @@ -43,7 +44,6 @@ import org.obd.graphs.ui.common.toast


internal val powerReceiver = PowerBroadcastReceiver()

const val NOTIFICATION_GRAPH_VIEW_TOGGLE = "view.graph.toggle"
const val NOTIFICATION_DASH_VIEW_TOGGLE = "view.dash.toggle"
const val NOTIFICATION_GAUGE_VIEW_TOGGLE = "view.gauge.toggle"
Expand All @@ -54,6 +54,7 @@ const val DASH_VIEW_ID = "pref.dash.view.enabled"
const val GIULIA_VIEW_ID = "pref.giulia.view.enabled"
const val RESET_TOOLBAR_ANIMATION: String = "toolbar.reset.animation"

private const val EVENT_VEHICLE_STATUS_CHANGED = "event.vehicle.status.CHANGED"

internal fun MainActivity.receive(intent: Intent?) {

Expand Down Expand Up @@ -227,10 +228,32 @@ internal fun MainActivity.receive(intent: Intent?) {
toast(R.string.main_activity_toast_connection_error)
handleStop()
}
}
}

EVENT_VEHICLE_STATUS_VEHICLE_RUNNING -> {
updateVehicleStatus("Running")
}

EVENT_VEHICLE_STATUS_VEHICLE_IDLING -> {
updateVehicleStatus("Idling")
}

EVENT_VEHICLE_STATUS_IGNITION_OFF -> {
updateVehicleStatus("Key off")
if (dataLoggerPreferences.instance.vehicleStatusDisconnectWhenOff){
Log.i(LOG_TAG,"Received vehicle status OFF event. Closing the session.")
dataLogger.stop()
}
}

EVENT_VEHICLE_STATUS_IGNITION_ON -> {
updateVehicleStatus("Key on")
}

EVENT_VEHICLE_STATUS_CHANGED->{
updateVehicleStatus("")
}
}
}

private fun MainActivity.handleStop() {

Expand Down Expand Up @@ -299,6 +322,14 @@ internal fun MainActivity.registerReceiver() {
it.addAction(DATA_LOGGER_WIFI_NOT_CONNECTED)
it.addAction(REQUEST_LOCATION_PERMISSIONS)
it.addAction(RESET_TOOLBAR_ANIMATION)

it.addAction(EVENT_VEHICLE_STATUS_VEHICLE_RUNNING)
it.addAction(EVENT_VEHICLE_STATUS_VEHICLE_IDLING)
it.addAction(EVENT_VEHICLE_STATUS_IGNITION_OFF)
it.addAction(EVENT_VEHICLE_STATUS_VEHICLE_ACCELERATING)
it.addAction(EVENT_VEHICLE_STATUS_VEHICLE_DECELERATING)
it.addAction(EVENT_VEHICLE_STATUS_IGNITION_ON)
it.addAction(EVENT_VEHICLE_STATUS_CHANGED)
}

registerReceiver(this, powerReceiver){
Expand Down
28 changes: 21 additions & 7 deletions app/src/main/java/org/obd/graphs/activity/StatusPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@ package org.obd.graphs.activity
import android.graphics.Color
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import org.obd.graphs.R
import org.obd.graphs.bl.datalogger.dataLoggerPreferences
import org.obd.graphs.profile.profile
import org.obd.graphs.sendBroadcastEvent
import org.obd.graphs.ui.common.COLOR_PHILIPPINE_GREEN
import org.obd.graphs.ui.common.COLOR_RAINBOW_INDIGO
import org.obd.graphs.ui.common.TOGGLE_TOOLBAR_ACTION
import org.obd.graphs.ui.common.highLightText
import org.obd.graphs.ui.common.*


internal fun MainActivity.updateVehicleStatus(status: String) {

updateTextField(
R.id.vehicle_status,
resources.getString(R.string.status_panel_vehicle_status),
status,
COLOR_CARDINAL,
1.0f
){
it.isVisible = dataLoggerPreferences.instance.vehicleStatusPanelEnabled
}
}

internal fun MainActivity.updateAdapterConnectionType() {
updateTextField(
R.id.connection_status,
resources.getString(R.string.adapter_connection_type),
resources.getString(R.string.status_panel_adapter_connection_type),
dataLoggerPreferences.instance.connectionType,
COLOR_PHILIPPINE_GREEN,
1.0f
Expand All @@ -44,6 +55,7 @@ internal fun MainActivity.updateAdapterConnectionType() {
internal fun MainActivity.setupStatusPanel() {
updateAdapterConnectionType()
updateVehicleProfile()
updateVehicleStatus("")

(findViewById<TextView>(R.id.connection_status)).let {
it.setOnClickListener {
Expand All @@ -67,7 +79,7 @@ internal fun MainActivity.setupStatusPanel() {
internal fun MainActivity.updateVehicleProfile() {
updateTextField(
R.id.vehicle_profile,
resources.getString(R.string.vehicle_profile),
resources.getString(R.string.status_panel_vehicle_profile),
profile.getCurrentProfileName(),
COLOR_RAINBOW_INDIGO,
1.0f
Expand All @@ -79,9 +91,11 @@ private fun MainActivity.updateTextField(
text1: String,
text2: String,
color: Int,
text2Size: Float
text2Size: Float,
func: (p: TextView) -> Unit = {}
) {
(findViewById<TextView>(viewId)).let {
func(it)
it.text = "$text1 $text2"
it.highLightText(text1, 0.7f, Color.WHITE)
it.highLightText(text2, text2Size, color)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2019-2024, Tomasz Żebrowski
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.obd.graphs.preferences

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import androidx.preference.CheckBoxPreference
import androidx.preference.Preference.OnPreferenceChangeListener
import org.obd.graphs.sendBroadcastEvent
import org.obd.graphs.ui.common.COLOR_CARDINAL
import org.obd.graphs.ui.common.colorize


class BroadcastEventCheckBoxPreference(
context: Context,
private val attrs: AttributeSet?
) :
CheckBoxPreference(context, attrs) {

private val experimental = getAttribute("experimental").toBooleanStrictOrNull() ?: false
private val broadcastEvent = getAttribute("broadcastEvent")

init {
onPreferenceChangeListener = OnPreferenceChangeListener { _, _ ->
if (broadcastEvent != null && broadcastEvent.isNotEmpty()) {
sendBroadcastEvent(broadcastEvent)
}
true
}
}

override fun getSummary(): CharSequence? =
if (experimental){
super.getSummary().toString().colorize(COLOR_CARDINAL, Typeface.NORMAL, 0, 33, 1.0f)
} else {
super.getSummary()
}


private fun getAttribute(attrName: String): String = if (attrs == null) {
""
} else {
val givenValue: String? = (0 until attrs.attributeCount)
.filter { index -> attrs.getAttributeName(index) == attrName }
.map { index -> attrs.getAttributeValue(index) }.firstOrNull()
givenValue ?: ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ class AACheckBoxPreference(
.map { index -> attrs.getAttributeValue(index) }.firstOrNull()
givenValue ?: ""
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.model.GradientColor
import org.obd.graphs.bl.collector.Metric
import org.obd.graphs.R
import org.obd.graphs.valueToNumber
import org.obd.graphs.preferences.Prefs
import org.obd.graphs.ui.common.COLOR_PHILIPPINE_GREEN
import org.obd.graphs.ui.common.highLightText
Expand Down Expand Up @@ -82,7 +83,7 @@ class DashboardViewAdapter(
val obdCommand = metric.source.command as ObdCommand
holder.buildChart(obdCommand.pid)

val segmentNum: Int = holder.segments.indexOf(if (metric.value == null) 0.0 else metric.source.valueToDouble())
val segmentNum: Int = holder.segments.indexOf( (metric.source.valueToNumber() ?: 0).toDouble())
(segmentNum > 0).apply {
//reset
(0 until holder.chart.data.dataSetCount).reversed().forEach { e ->
Expand Down Expand Up @@ -132,7 +133,7 @@ class DashboardViewAdapter(

holder.chart.invalidate()
holder.label.text = obdCommand.pid.description
val units = (metric.source.command as ObdCommand).pid.units
val units = (metric.source.command as ObdCommand).pid.units?:""
val value = metric.source.valueToString() + " " + units
holder.value.text = value
holder.value.highLightText(units, 0.3f, COLOR_PHILIPPINE_GREEN)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/obd/graphs/ui/gauge/Gauge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import org.obd.graphs.R
import org.obd.graphs.ValueScaler
import org.obd.graphs.ValueConverter
import org.obd.graphs.ui.common.isTablet
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.math.abs
Expand Down Expand Up @@ -64,7 +64,7 @@ class Gauge(context: Context, attrs: AttributeSet?) : View(context, attrs) {
var gaugeDrawScale = false
private val numbersPaint = Paint()
private val initialized = AtomicBoolean(false)
private val valueScaler = ValueScaler()
private val valueConverter = ValueConverter()

private var strokeCap: String = Paint.Cap.BUTT.name
set(newValue) {
Expand Down Expand Up @@ -264,7 +264,7 @@ class Gauge(context: Context, attrs: AttributeSet?) : View(context, attrs) {
}
}

private fun calculateRescaleValue(): Float = (valueScaler.scaleToNewRange(
private fun calculateRescaleValue(): Float = (valueConverter.scaleToNewRange(
measuredWidth.toFloat() * measuredHeight.toFloat(),
0.0f,
Resources.getSystem().displayMetrics.widthPixels * Resources.getSystem().displayMetrics.heightPixels.toFloat(),
Expand Down
Loading

0 comments on commit 7f7ed6f

Please sign in to comment.