From 1709fdf1ac96e4480c5569172762ac6c923e8ada Mon Sep 17 00:00:00 2001 From: TCFDONMEZ Date: Wed, 30 Sep 2026 12:08:54 +0300 Subject: [PATCH] Track rolling 7 and 30 day consumption --- FatihsMG4-unstable-2.2.24.apk.sha256 | 1 - FatihsMG4-unstable-2.2.25.apk.sha256 | 1 + app/build.gradle | 4 +- .../com/evsuite/abrp/ConsumptionTracker.java | 52 ++++++++++--------- app/src/main/res/layout/pane_consumption.xml | 6 +-- 5 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 FatihsMG4-unstable-2.2.24.apk.sha256 create mode 100644 FatihsMG4-unstable-2.2.25.apk.sha256 diff --git a/FatihsMG4-unstable-2.2.24.apk.sha256 b/FatihsMG4-unstable-2.2.24.apk.sha256 deleted file mode 100644 index e24cf4a..0000000 --- a/FatihsMG4-unstable-2.2.24.apk.sha256 +++ /dev/null @@ -1 +0,0 @@ -c2c11f712ebc3052d30725b536a7efee06ad9de9e1700e852e10264fe083f8eb FatihsMG4-unstable-2.2.24.apk diff --git a/FatihsMG4-unstable-2.2.25.apk.sha256 b/FatihsMG4-unstable-2.2.25.apk.sha256 new file mode 100644 index 0000000..59d3080 --- /dev/null +++ b/FatihsMG4-unstable-2.2.25.apk.sha256 @@ -0,0 +1 @@ +e16113c3dd3d83492b0807836e01520323e5cc7e5a33a4e2c13b904e0152c2a8 FatihsMG4-unstable-2.2.25.apk diff --git a/app/build.gradle b/app/build.gradle index 72833f0..7a5aeb9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,8 +34,8 @@ android { applicationId "com.evsuite.abrp" minSdk 27 targetSdk 36 - versionCode 41 - versionName "2.2.24" + versionCode 42 + versionName "2.2.25" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/evsuite/abrp/ConsumptionTracker.java b/app/src/main/java/com/evsuite/abrp/ConsumptionTracker.java index 133b83b..a4c3bfa 100644 --- a/app/src/main/java/com/evsuite/abrp/ConsumptionTracker.java +++ b/app/src/main/java/com/evsuite/abrp/ConsumptionTracker.java @@ -5,10 +5,7 @@ import android.content.SharedPreferences; import com.evsuite.hardware.telemetry.EnergySnapshot; -import java.time.Instant; import java.time.LocalDate; -import java.time.ZoneId; -import java.time.temporal.WeekFields; /** Persistent, read-only trip/lifetime integration patterned after DriveHub_Dort. */ final class ConsumptionTracker { @@ -29,23 +26,18 @@ final class ConsumptionTracker { } private final SharedPreferences prefs; - private Totals lifetime, week, month, tripA, tripB; - private String weekId, monthId; + private Totals lifetime, tripA, tripB; private long lastMs; private Float lastSpeed, lastPower, lastSoc; private ConsumptionTracker(Context context) { prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE); - lifetime = load("life"); week = load("week"); month = load("month"); - tripA = load("a"); tripB = load("b"); - weekId = prefs.getString("week_id", ""); - monthId = prefs.getString("month_id", ""); - rollPeriods(System.currentTimeMillis()); + lifetime = load("life"); tripA = load("a"); tripB = load("b"); + migrateCalendarCounters(); } synchronized void sample(EnergySnapshot s) { long now = s.getTimestampMs(); - rollPeriods(now); if (lastMs > 0 && now > lastMs) { double hours = (now - lastMs) / 3_600_000d; // Never bridge a process pause/sleep into invented driving data. @@ -59,8 +51,10 @@ final class ConsumptionTracker { double dsoc = (lastSoc == null || s.getSocPercent() == null || Boolean.TRUE.equals(s.getChargePortConnected())) ? 0d : lastSoc - s.getSocPercent(); lifetime = add(lifetime, km, kwh, hours, dsoc); - week = add(week, km, kwh, hours, dsoc); - month = add(month, km, kwh, hours, dsoc); + String dayKey = "day_" + LocalDate.now(); + SharedPreferences.Editor daily = prefs.edit(); + save(daily, dayKey, add(load(dayKey), km, kwh, hours, dsoc)); + daily.commit(); tripA = add(tripA, km, kwh, hours, dsoc); tripB = add(tripB, km, kwh, hours, dsoc); persist(); @@ -72,7 +66,7 @@ final class ConsumptionTracker { synchronized Totals totals(Period period) { switch (period) { - case WEEK: return week; case MONTH: return month; case TRIP_A: return tripA; + case WEEK: return rollingDays(7); case MONTH: return rollingDays(30); case TRIP_A: return tripA; case TRIP_B: return tripB; default: return lifetime; } } @@ -86,21 +80,29 @@ final class ConsumptionTracker { synchronized void persist() { SharedPreferences.Editor e = prefs.edit(); - save(e, "life", lifetime); save(e, "week", week); save(e, "month", month); + save(e, "life", lifetime); save(e, "a", tripA); save(e, "b", tripB); - e.putString("week_id", weekId).putString("month_id", monthId); e.commit(); } - private void rollPeriods(long nowMs) { - LocalDate date = Instant.ofEpochMilli(nowMs).atZone(ZoneId.systemDefault()).toLocalDate(); - WeekFields iso = WeekFields.ISO; - String currentWeek = date.get(iso.weekBasedYear()) + "-W" + date.get(iso.weekOfWeekBasedYear()); - String currentMonth = date.getYear() + "-" + date.getMonthValue(); - boolean changed = false; - if (!currentWeek.equals(weekId)) { weekId = currentWeek; week = zero(); changed = true; } - if (!currentMonth.equals(monthId)) { monthId = currentMonth; month = zero(); changed = true; } - if (changed) persist(); + private Totals rollingDays(int days) { + Totals result = zero(); + LocalDate today = LocalDate.now(); + for (int i = 0; i < days; i++) { + Totals day = load("day_" + today.minusDays(i)); + result = add(result, day.km, day.kwh, day.hours, day.soc); + } + return result; + } + + /** Preserve anything gathered by 2.2.24 by treating its just-created counter as today. */ + private void migrateCalendarCounters() { + if (prefs.getBoolean("rolling_days_migrated", false)) return; + Totals old = load("week"); + SharedPreferences.Editor e = prefs.edit(); + if (old.km != 0 || old.kwh != 0 || old.hours != 0 || old.soc != 0) + save(e, "day_" + LocalDate.now(), old); + e.putBoolean("rolling_days_migrated", true).commit(); } private Totals load(String key) { return new Totals(bits(key,"km"), bits(key,"kwh"), bits(key,"h"), bits(key,"soc")); } diff --git a/app/src/main/res/layout/pane_consumption.xml b/app/src/main/res/layout/pane_consumption.xml index 22ae588..b926218 100644 --- a/app/src/main/res/layout/pane_consumption.xml +++ b/app/src/main/res/layout/pane_consumption.xml @@ -6,8 +6,8 @@ - - + + @@ -22,6 +22,6 @@ - +