Add weekly and monthly consumption periods

This commit is contained in:
TCFDONMEZ
2026-09-30 12:04:40 +03:00
parent 1749815599
commit 81483e7f48
7 changed files with 42 additions and 22 deletions
-1
View File
@@ -1 +0,0 @@
db95c341f6b975d9b4b9dceea79adb0b3829a0bafe8a86ea31d5b7e4012206b0 FatihsMG4-unstable-2.2.22.apk
-1
View File
@@ -1 +0,0 @@
d905ad99a0479e85460a248116ce85b50a58759f2da38146a2654e70dfc0051e FatihsMG4-unstable-2.2.23.apk
+1
View File
@@ -0,0 +1 @@
c2c11f712ebc3052d30725b536a7efee06ad9de9e1700e852e10264fe083f8eb FatihsMG4-unstable-2.2.24.apk
+2 -2
View File
@@ -34,8 +34,8 @@ android {
applicationId "com.evsuite.abrp" applicationId "com.evsuite.abrp"
minSdk 27 minSdk 27
targetSdk 36 targetSdk 36
versionCode 40 versionCode 41
versionName "2.2.23" versionName "2.2.24"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
@@ -5,9 +5,14 @@ import android.content.SharedPreferences;
import com.evsuite.hardware.telemetry.EnergySnapshot; 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. */ /** Persistent, read-only trip/lifetime integration patterned after DriveHub_Dort. */
final class ConsumptionTracker { final class ConsumptionTracker {
enum Period { START, MOTOR, LIFETIME, TRIP_A, TRIP_B } enum Period { LIFETIME, WEEK, MONTH, TRIP_A, TRIP_B }
static final class Totals { static final class Totals {
final double km, kwh, hours, soc; final double km, kwh, hours, soc;
@@ -24,20 +29,23 @@ final class ConsumptionTracker {
} }
private final SharedPreferences prefs; private final SharedPreferences prefs;
private Totals start = zero(), motor = zero(), lifetime, tripA, tripB; private Totals lifetime, week, month, tripA, tripB;
private String weekId, monthId;
private long lastMs; private long lastMs;
private Float lastSpeed, lastPower, lastSoc; private Float lastSpeed, lastPower, lastSoc;
private Boolean lastParked;
private ConsumptionTracker(Context context) { private ConsumptionTracker(Context context) {
prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE); prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
lifetime = load("life"); tripA = load("a"); tripB = load("b"); 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());
} }
synchronized void sample(EnergySnapshot s) { synchronized void sample(EnergySnapshot s) {
long now = s.getTimestampMs(); long now = s.getTimestampMs();
Boolean parked = s.getParked(); rollPeriods(now);
if (Boolean.FALSE.equals(parked) && !Boolean.FALSE.equals(lastParked)) motor = zero();
if (lastMs > 0 && now > lastMs) { if (lastMs > 0 && now > lastMs) {
double hours = (now - lastMs) / 3_600_000d; double hours = (now - lastMs) / 3_600_000d;
// Never bridge a process pause/sleep into invented driving data. // Never bridge a process pause/sleep into invented driving data.
@@ -50,21 +58,21 @@ final class ConsumptionTracker {
((lastPower == null ? power : lastPower) + power) * 0.5d * hours; ((lastPower == null ? power : lastPower) + power) * 0.5d * hours;
double dsoc = (lastSoc == null || s.getSocPercent() == null || double dsoc = (lastSoc == null || s.getSocPercent() == null ||
Boolean.TRUE.equals(s.getChargePortConnected())) ? 0d : lastSoc - s.getSocPercent(); Boolean.TRUE.equals(s.getChargePortConnected())) ? 0d : lastSoc - s.getSocPercent();
start = add(start, km, kwh, hours, dsoc);
lifetime = add(lifetime, km, kwh, hours, dsoc); lifetime = add(lifetime, km, kwh, hours, dsoc);
week = add(week, km, kwh, hours, dsoc);
month = add(month, km, kwh, hours, dsoc);
tripA = add(tripA, km, kwh, hours, dsoc); tripA = add(tripA, km, kwh, hours, dsoc);
tripB = add(tripB, km, kwh, hours, dsoc); tripB = add(tripB, km, kwh, hours, dsoc);
if (Boolean.FALSE.equals(parked)) motor = add(motor, km, kwh, hours, dsoc);
persist(); persist();
} }
} }
lastMs = now; lastSpeed = s.getSpeedKmh(); lastPower = s.getBatteryPowerKw(); lastMs = now; lastSpeed = s.getSpeedKmh(); lastPower = s.getBatteryPowerKw();
lastSoc = s.getSocPercent(); lastParked = parked; lastSoc = s.getSocPercent();
} }
synchronized Totals totals(Period period) { synchronized Totals totals(Period period) {
switch (period) { switch (period) {
case START: return start; case MOTOR: return motor; case TRIP_A: return tripA; case WEEK: return week; case MONTH: return month; case TRIP_A: return tripA;
case TRIP_B: return tripB; default: return lifetime; case TRIP_B: return tripB; default: return lifetime;
} }
} }
@@ -78,10 +86,23 @@ final class ConsumptionTracker {
synchronized void persist() { synchronized void persist() {
SharedPreferences.Editor e = prefs.edit(); SharedPreferences.Editor e = prefs.edit();
save(e, "life", lifetime); save(e, "a", tripA); save(e, "b", tripB); save(e, "life", lifetime); save(e, "week", week); save(e, "month", month);
save(e, "a", tripA); save(e, "b", tripB);
e.putString("week_id", weekId).putString("month_id", monthId);
e.commit(); 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 load(String key) { return new Totals(bits(key,"km"), bits(key,"kwh"), bits(key,"h"), bits(key,"soc")); } private Totals load(String key) { return new Totals(bits(key,"km"), bits(key,"kwh"), bits(key,"h"), bits(key,"soc")); }
private double bits(String key, String field) { return Double.longBitsToDouble(prefs.getLong(key + "_" + field, 0L)); } private double bits(String key, String field) { return Double.longBitsToDouble(prefs.getLong(key + "_" + field, 0L)); }
private static void save(SharedPreferences.Editor e, String key, Totals t) { private static void save(SharedPreferences.Editor e, String key, Totals t) {
@@ -830,8 +830,8 @@ public class MainActivity extends AppCompatActivity {
consumptionSoc = consumptionPane.findViewById(R.id.consumption_soc); consumptionSoc = consumptionPane.findViewById(R.id.consumption_soc);
consumptionReset = consumptionPane.findViewById(R.id.consumption_reset); consumptionReset = consumptionPane.findViewById(R.id.consumption_reset);
bindPeriodButton(R.id.consumption_lifetime, ConsumptionTracker.Period.LIFETIME); bindPeriodButton(R.id.consumption_lifetime, ConsumptionTracker.Period.LIFETIME);
bindPeriodButton(R.id.consumption_start, ConsumptionTracker.Period.START); bindPeriodButton(R.id.consumption_week, ConsumptionTracker.Period.WEEK);
bindPeriodButton(R.id.consumption_motor, ConsumptionTracker.Period.MOTOR); bindPeriodButton(R.id.consumption_month, ConsumptionTracker.Period.MONTH);
bindPeriodButton(R.id.consumption_trip_a, ConsumptionTracker.Period.TRIP_A); bindPeriodButton(R.id.consumption_trip_a, ConsumptionTracker.Period.TRIP_A);
bindPeriodButton(R.id.consumption_trip_b, ConsumptionTracker.Period.TRIP_B); bindPeriodButton(R.id.consumption_trip_b, ConsumptionTracker.Period.TRIP_B);
selectPeriodButton(R.id.consumption_lifetime); selectPeriodButton(R.id.consumption_lifetime);
@@ -860,8 +860,8 @@ public class MainActivity extends AppCompatActivity {
} }
private void selectPeriodButton(int selectedId) { private void selectPeriodButton(int selectedId) {
int[] ids = {R.id.consumption_lifetime, R.id.consumption_start, int[] ids = {R.id.consumption_lifetime, R.id.consumption_week,
R.id.consumption_motor, R.id.consumption_trip_a, R.id.consumption_trip_b}; R.id.consumption_month, R.id.consumption_trip_a, R.id.consumption_trip_b};
for (int id : ids) { for (int id : ids) {
MaterialButton button = consumptionPane.findViewById(id); MaterialButton button = consumptionPane.findViewById(id);
boolean selected = id == selectedId; boolean selected = id == selectedId;
+3 -3
View File
@@ -6,8 +6,8 @@
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:scrollbars="none"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:scrollbars="none">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_lifetime" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:text="Tüm zamanlar"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_lifetime" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:text="Tüm zamanlar"/>
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_start" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Başlangıçtan"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_week" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Bu hafta"/>
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_motor" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Motor sonrası"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_month" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Bu ay"/>
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_trip_a" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Trip A"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_trip_a" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Trip A"/>
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_trip_b" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Trip B"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_trip_b" style="@style/Widget.EV.Button.Outlined" android:layout_width="wrap_content" android:layout_height="56dp" android:layout_marginStart="8dp" android:text="Trip B"/>
</LinearLayout> </LinearLayout>
@@ -22,6 +22,6 @@
</GridLayout> </GridLayout>
<com.google.android.material.button.MaterialButton android:id="@+id/consumption_reset" style="@style/Widget.EV.Button.Outlined" android:layout_width="match_parent" android:layout_height="64dp" android:layout_marginTop="12dp" android:text="Seçili tripi sıfırla" android:visibility="gone"/> <com.google.android.material.button.MaterialButton android:id="@+id/consumption_reset" style="@style/Widget.EV.Button.Outlined" android:layout_width="match_parent" android:layout_height="64dp" android:layout_marginTop="12dp" android:text="Seçili tripi sıfırla" android:visibility="gone"/>
</LinearLayout> </LinearLayout>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="Tüm zamanlar, Trip A ve Trip B cihazda kalıcı saklanır; uygulama ve OTA güncellemeleriyle silinmez." android:textAppearance="@style/Text.EV.Secondary"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="Hafta, ay, tüm zamanlar ve Trip A/B cihazda kalıcı saklanır; uygulama ve OTA güncellemeleriyle silinmez." android:textAppearance="@style/Text.EV.Secondary"/>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>