|
| 1 | +package com.evsuite.chargepilot |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.appcompat.app.AppCompatActivity |
| 5 | +import com.evsuite.chargepilot.databinding.ActivityChargeHistoryBinding |
| 6 | +import com.evsuite.hardware.telemetry.ChargeEnergy |
| 7 | +import com.evsuite.hardware.telemetry.ChargeEnergyReport |
| 8 | +import java.text.DateFormat |
| 9 | +import java.util.Date |
| 10 | +import java.util.concurrent.Executors |
| 11 | +import kotlin.math.abs |
| 12 | + |
| 13 | +/** |
| 14 | + * Every charge the battery ledger holds, and what the watched ones measured. |
| 15 | + * |
| 16 | + * The battery page has room for one line about charging; this screen is where the list lives. |
| 17 | + * Nothing new is sampled or inferred: rows come from [ChargeEnergyReport], the same report the |
| 18 | + * battery page and the diagnostic bundle read, so the three can never disagree. There is no |
| 19 | + * cost here on purpose — a tariff is a declaration, and every figure on this screen is measured. |
| 20 | + * |
| 21 | + * A charge the head unit slept through keeps its points and its span and shows no energy or |
| 22 | + * power: the integral did not move while nobody was integrating, and a zero would be a lie. |
| 23 | + */ |
| 24 | +class ChargeHistoryActivity : AppCompatActivity() { |
| 25 | + |
| 26 | + private lateinit var binding: ActivityChargeHistoryBinding |
| 27 | + private val disk = Executors.newSingleThreadExecutor { runnable -> |
| 28 | + Thread(runnable, "chargepilot-charge-history") |
| 29 | + } |
| 30 | + |
| 31 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 32 | + super.onCreate(savedInstanceState) |
| 33 | + binding = ActivityChargeHistoryBinding.inflate(layoutInflater) |
| 34 | + setContentView(binding.root) |
| 35 | + binding.backAction.setOnClickListener { finish() } |
| 36 | + disk.execute { |
| 37 | + val settings = VehicleSettings.read(this) |
| 38 | + val report = LocalBatteryLearning.refresh(filesDir, settings.usableCapacityKwhWhenNew) |
| 39 | + .charge |
| 40 | + runOnUiThread { |
| 41 | + if (!isFinishing && !isDestroyed) render(report) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + override fun onDestroy() { |
| 47 | + disk.shutdownNow() |
| 48 | + super.onDestroy() |
| 49 | + } |
| 50 | + |
| 51 | + private fun render(report: ChargeEnergyReport) { |
| 52 | + val charges = report.pluggedCharges |
| 53 | + if (charges.isEmpty()) { |
| 54 | + binding.analysisFacts.text = getString(R.string.charge_history_empty) |
| 55 | + binding.curveFacts.text = getString(R.string.charge_history_curve_empty) |
| 56 | + binding.chargeList.text = "" |
| 57 | + return |
| 58 | + } |
| 59 | + binding.analysisFacts.text = analysis(report, charges).joinToString("\n\n") |
| 60 | + val bands = report.powerBySocBand() |
| 61 | + binding.curveFacts.text = if (bands.isEmpty()) { |
| 62 | + getString(R.string.charge_history_curve_empty) |
| 63 | + } else { |
| 64 | + bands.joinToString("\n") { |
| 65 | + getString( |
| 66 | + R.string.charge_history_band, |
| 67 | + it.fromPercent, it.toPercent, it.meanPowerKw, it.hours, |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + binding.chargeList.text = charges.asReversed() |
| 72 | + .take(MAX_ROWS) |
| 73 | + .joinToString("\n\n") { row(it) } |
| 74 | + } |
| 75 | + |
| 76 | + private fun analysis(report: ChargeEnergyReport, charges: List<ChargeEnergy>): List<String> { |
| 77 | + val lines = ArrayList<String>() |
| 78 | + lines += getString( |
| 79 | + R.string.charge_history_summary, |
| 80 | + charges.size, |
| 81 | + charges.count { it.watched }, |
| 82 | + charges.sumOf { it.session.gainedPercent }, |
| 83 | + ) |
| 84 | + val median = charges.sortedBy { it.session.gainedPercent }[charges.size / 2].session |
| 85 | + lines += getString( |
| 86 | + R.string.charge_history_typical, |
| 87 | + median.gainedPercent, |
| 88 | + median.meanPercentPerHour, |
| 89 | + charges.maxOf { it.session.peakPercentPerHour }, |
| 90 | + ) |
| 91 | + val watched = charges.filter { it.watched && it.packDeltaKwh != null } |
| 92 | + if (watched.isNotEmpty()) { |
| 93 | + val energy = watched.sumOf { abs(it.packDeltaKwh!!) } |
| 94 | + val hours = watched.sumOf { it.session.durationHours } |
| 95 | + lines += getString( |
| 96 | + R.string.charge_history_energy, |
| 97 | + energy, |
| 98 | + if (hours > 0.0) energy / hours else 0.0, |
| 99 | + watched.mapNotNull { it.peakPowerKw }.maxOrNull() ?: 0.0, |
| 100 | + ) |
| 101 | + } |
| 102 | + lines += getString( |
| 103 | + R.string.charge_history_verdicts, |
| 104 | + report.packSign.name, |
| 105 | + report.counterBehaviour.name, |
| 106 | + report.chargingStatuses.joinToString(",").ifEmpty { "—" }, |
| 107 | + ) |
| 108 | + lines += getString(R.string.charge_history_temperature) |
| 109 | + return lines |
| 110 | + } |
| 111 | + |
| 112 | + private fun row(charge: ChargeEnergy): String { |
| 113 | + val session = charge.session |
| 114 | + val format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT) |
| 115 | + val lines = ArrayList<String>() |
| 116 | + lines += getString( |
| 117 | + R.string.charge_history_row, |
| 118 | + format.format(Date(session.startedAtMs)), |
| 119 | + format.format(Date(session.endedAtMs)), |
| 120 | + session.startSocPercent, |
| 121 | + session.endSocPercent, |
| 122 | + session.gainedPercent, |
| 123 | + session.durationHours, |
| 124 | + session.meanPercentPerHour, |
| 125 | + ) |
| 126 | + val energy = charge.packDeltaKwh?.takeIf { charge.watched } |
| 127 | + val power = charge.meanPowerKw |
| 128 | + lines += if (energy != null && power != null) { |
| 129 | + getString( |
| 130 | + R.string.charge_history_row_watched, |
| 131 | + abs(energy), |
| 132 | + power, |
| 133 | + charge.peakPowerKw ?: power, |
| 134 | + ) |
| 135 | + } else { |
| 136 | + getString(R.string.charge_history_row_unwatched) |
| 137 | + } |
| 138 | + val low = charge.minOutsideTempCelsius |
| 139 | + val high = charge.maxOutsideTempCelsius |
| 140 | + if (low != null && high != null) { |
| 141 | + lines += getString(R.string.charge_history_row_temperature, low, high) |
| 142 | + } |
| 143 | + return lines.joinToString("\n") |
| 144 | + } |
| 145 | + |
| 146 | + private companion object { |
| 147 | + /** The ledger is bounded already; a list longer than this is scrolled, not read. */ |
| 148 | + const val MAX_ROWS = 50 |
| 149 | + } |
| 150 | +} |
0 commit comments