|
| 1 | +package com.evsuite.chargepilot |
| 2 | + |
| 3 | +import android.content.ComponentName |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.ServiceConnection |
| 7 | +import android.os.Bundle |
| 8 | +import android.os.IBinder |
| 9 | +import android.view.View |
| 10 | +import androidx.appcompat.app.AppCompatActivity |
| 11 | +import com.evsuite.chargepilot.databinding.ActivityArrivalForecastBinding |
| 12 | +import com.evsuite.hardware.FirmwareInfo |
| 13 | +import com.evsuite.hardware.saic.SaicNavGuidance |
| 14 | +import com.evsuite.hardware.telemetry.ArrivalSocForecast |
| 15 | +import com.evsuite.hardware.telemetry.EnergySnapshot |
| 16 | +import com.evsuite.hardware.telemetry.EnergyTripHistoryStore |
| 17 | +import com.evsuite.hardware.telemetry.Provenanced |
| 18 | +import com.evsuite.hardware.telemetry.SocRate |
| 19 | +import com.evsuite.hardware.telemetry.SocRateEstimator |
| 20 | +import java.io.File |
| 21 | +import java.util.Locale |
| 22 | +import java.util.concurrent.Executors |
| 23 | +import java.util.concurrent.ScheduledFuture |
| 24 | +import java.util.concurrent.TimeUnit |
| 25 | + |
| 26 | +/** |
| 27 | + * What the charge will read on arrival, if the head unit knows where the driver is going. |
| 28 | + * |
| 29 | + * The obvious way to build this — the energy model, a distance, a pack capacity — cannot work |
| 30 | + * on this vehicle: CP-003 proved battery power is declared and never published, so nothing can |
| 31 | + * integrate kWh. State of charge per kilometre needs no energy unit and both its inputs read, |
| 32 | + * which is what this screen shows. |
| 33 | + * |
| 34 | + * **It reads the navigation, it never registers with it.** `SaicNavGuidance.readNow` answers |
| 35 | + * from synchronous getters; the listener registration that CP-040 used to discover them stays |
| 36 | + * in the unstable channel, so a stable build never joins the adapter's callback fan-out. |
| 37 | + * |
| 38 | + * Everything happens on [worker]: the vehicle binder calls, the trip file, and the arithmetic. |
| 39 | + * The dashboard's own sampling must not wait on any of it. |
| 40 | + */ |
| 41 | +class ArrivalForecastActivity : AppCompatActivity() { |
| 42 | + |
| 43 | + private lateinit var binding: ActivityArrivalForecastBinding |
| 44 | + private lateinit var provenance: ProvenanceText |
| 45 | + |
| 46 | + private val worker = Executors.newSingleThreadScheduledExecutor { runnable -> |
| 47 | + Thread(runnable, "chargepilot-arrival") |
| 48 | + } |
| 49 | + |
| 50 | + @Volatile |
| 51 | + private var snapshot: EnergySnapshot? = null |
| 52 | + |
| 53 | + /** Read once: the trip file does not change while this screen is open. */ |
| 54 | + @Volatile |
| 55 | + private var rate: SocRate? = null |
| 56 | + |
| 57 | + @Volatile |
| 58 | + private var ratePending = true |
| 59 | + |
| 60 | + private var recorder: TripRecordingService? = null |
| 61 | + private var bound = false |
| 62 | + private var refresh: ScheduledFuture<*>? = null |
| 63 | + |
| 64 | + private val connection = object : ServiceConnection { |
| 65 | + override fun onServiceConnected(name: ComponentName?, service: IBinder?) { |
| 66 | + val value = (service as? TripRecordingService.LocalBinder)?.service ?: return |
| 67 | + recorder = value |
| 68 | + snapshot = value.latest |
| 69 | + value.setListener(this@ArrivalForecastActivity) { latest -> snapshot = latest } |
| 70 | + } |
| 71 | + |
| 72 | + override fun onServiceDisconnected(name: ComponentName?) { |
| 73 | + recorder = null |
| 74 | + snapshot = null |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 79 | + super.onCreate(savedInstanceState) |
| 80 | + binding = ActivityArrivalForecastBinding.inflate(layoutInflater) |
| 81 | + setContentView(binding.root) |
| 82 | + provenance = ProvenanceText(this) |
| 83 | + binding.backAction.setOnClickListener { finish() } |
| 84 | + worker.execute { |
| 85 | + SaicNavGuidance.connect(applicationContext) |
| 86 | + loadRate() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + override fun onStart() { |
| 91 | + super.onStart() |
| 92 | + bound = bindService( |
| 93 | + Intent(this, TripRecordingService::class.java), |
| 94 | + connection, |
| 95 | + Context.BIND_AUTO_CREATE, |
| 96 | + ) |
| 97 | + refresh = worker.scheduleWithFixedDelay(::recompute, 0L, REFRESH_SECONDS, TimeUnit.SECONDS) |
| 98 | + } |
| 99 | + |
| 100 | + override fun onStop() { |
| 101 | + refresh?.cancel(false) |
| 102 | + refresh = null |
| 103 | + recorder?.clearListener(this) |
| 104 | + recorder = null |
| 105 | + if (bound) unbindService(connection) |
| 106 | + bound = false |
| 107 | + snapshot = null |
| 108 | + super.onStop() |
| 109 | + } |
| 110 | + |
| 111 | + override fun onDestroy() { |
| 112 | + worker.shutdownNow() |
| 113 | + super.onDestroy() |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * The rate this driver has actually spent, when enough trips can be vouched for. |
| 118 | + * |
| 119 | + * Trips recorded before the speed conversion was fixed are 3.6 times too long and are |
| 120 | + * refused by the estimator, so a fresh install has no history and falls back to the |
| 121 | + * vehicle's own range — which is the point of having two sources. |
| 122 | + */ |
| 123 | + private fun loadRate() { |
| 124 | + val generation = FirmwareInfo.getGeneration() |
| 125 | + val trips = runCatching { |
| 126 | + EnergyTripHistoryStore(File(filesDir, HISTORY_FILE)).readSummaries() |
| 127 | + }.getOrDefault(emptyList()) |
| 128 | + rate = SocRateEstimator.fromTrips(trips, generation) |
| 129 | + ratePending = false |
| 130 | + } |
| 131 | + |
| 132 | + private fun recompute() { |
| 133 | + val latest = snapshot |
| 134 | + val generation = FirmwareInfo.getGeneration() |
| 135 | + val guidance = runCatching { SaicNavGuidance.readNow() }.getOrNull() |
| 136 | + val remainingKm = guidance?.remainingDistanceKm(generation) |
| 137 | + val socPercent = latest?.socPercent?.toDouble() |
| 138 | + val effective = rate ?: SocRateEstimator.fromVehicleRange( |
| 139 | + socPercent, |
| 140 | + latest?.rangeKm?.toDouble(), |
| 141 | + ) |
| 142 | + val arrival = ArrivalSocForecast.of(socPercent, remainingKm, effective) |
| 143 | + val reach = ArrivalSocForecast.rangeAtRateKm(socPercent, effective) |
| 144 | + val state = ViewState( |
| 145 | + arrival = arrival, |
| 146 | + reachKm = reach, |
| 147 | + remainingKm = remainingKm, |
| 148 | + minutes = guidance?.remainingMinutes, |
| 149 | + road = guidance?.road, |
| 150 | + rate = effective, |
| 151 | + routeKnown = remainingKm != null, |
| 152 | + ratePending = ratePending, |
| 153 | + ) |
| 154 | + runOnUiThread { if (!isFinishing && !isDestroyed) render(state) } |
| 155 | + } |
| 156 | + |
| 157 | + private fun render(state: ViewState) { |
| 158 | + val arrival = provenance.render(state.arrival, "%.0f %%") |
| 159 | + binding.arrivalValue.text = arrival |
| 160 | + binding.arrivalValue.contentDescription = |
| 161 | + provenance.describe(getString(R.string.arrival_label), state.arrival, arrival) |
| 162 | + |
| 163 | + binding.arrivalStatus.text = when { |
| 164 | + !state.routeKnown -> getString(R.string.arrival_no_route) |
| 165 | + state.ratePending -> getString(R.string.arrival_rate_loading) |
| 166 | + !state.arrival.isAvailable -> getString(R.string.arrival_refused) |
| 167 | + else -> getString( |
| 168 | + R.string.arrival_route, |
| 169 | + String.format(Locale.getDefault(), "%.1f", state.remainingKm ?: 0.0), |
| 170 | + state.minutes ?: 0, |
| 171 | + state.road.orEmpty(), |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + binding.arrivalDetail.visibility = if (state.rate == null) View.GONE else View.VISIBLE |
| 176 | + state.rate?.let { rate -> |
| 177 | + binding.arrivalDetail.text = getString( |
| 178 | + R.string.arrival_detail, |
| 179 | + getString( |
| 180 | + when (rate.source) { |
| 181 | + SocRate.Source.TRIP_HISTORY -> R.string.arrival_source_trips |
| 182 | + SocRate.Source.VEHICLE_RANGE -> R.string.arrival_source_range |
| 183 | + } |
| 184 | + ), |
| 185 | + rate.sampleCount, |
| 186 | + String.format(Locale.getDefault(), "%.3f", rate.percentPerKm), |
| 187 | + provenance.render(state.reachKm, "%.0f km"), |
| 188 | + ) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private data class ViewState( |
| 193 | + val arrival: Provenanced<Double>, |
| 194 | + val reachKm: Provenanced<Double>, |
| 195 | + val remainingKm: Double?, |
| 196 | + val minutes: Int?, |
| 197 | + val road: String?, |
| 198 | + val rate: SocRate?, |
| 199 | + val routeKnown: Boolean, |
| 200 | + val ratePending: Boolean, |
| 201 | + ) |
| 202 | + |
| 203 | + private companion object { |
| 204 | + const val HISTORY_FILE = "trips.json" |
| 205 | + |
| 206 | + /** Slow on purpose: a route shortens by metres a second and this is not a speedometer. */ |
| 207 | + const val REFRESH_SECONDS = 5L |
| 208 | + } |
| 209 | +} |
0 commit comments