Track battery percentage history; skip poll write when value unchanged

This commit is contained in:
2026-04-13 21:15:19 -05:00
parent 279a1f3f3e
commit d7ba64a2f3
6 changed files with 230 additions and 7 deletions
+25 -3
View File
@@ -2,7 +2,9 @@ from flask import Flask, render_template, redirect, url_for, request, flash, abo
from sqlalchemy import create_engine, func
from sqlalchemy.orm import scoped_session, sessionmaker
from models import Base, Battery, Device, CapacityTest, ChargeLog
from datetime import datetime
from models import Base, Battery, BatteryPctLog, Device, CapacityTest, ChargeLog
def create_app(config_object="config"):
@@ -146,10 +148,15 @@ def create_app(config_object="config"):
.filter_by(battery_id=battery_id)
.order_by(ChargeLog.charged_date.desc(), ChargeLog.id.desc())
.all())
pct_logs = (db.query(BatteryPctLog)
.filter_by(battery_id=battery_id)
.order_by(BatteryPctLog.recorded_at.desc())
.all())
return render_template("battery_detail.html", battery=battery,
storage_locations=storage_locations,
capacity_tests=capacity_tests,
charge_logs=charge_logs)
charge_logs=charge_logs,
pct_logs=pct_logs)
# ------------------------------------------------------------------ #
# Battery — edit notes
@@ -176,7 +183,16 @@ def create_app(config_object="config"):
battery.charge_cycles = _int("charge_cycles")
battery.purchase_date = f.get("purchase_date", "").strip() or None
battery.storage_location = f.get("storage_location", "").strip() or None
battery.battery_percentage = _int("battery_percentage")
new_pct = _int("battery_percentage")
if new_pct != battery.battery_percentage:
battery.battery_percentage = new_pct
if new_pct is not None:
db.add(BatteryPctLog(
battery_id=battery.id,
percentage=new_pct,
recorded_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
source="manual",
))
db.commit()
flash("Details updated.", "success")
@@ -251,6 +267,12 @@ def create_app(config_object="config"):
if increment:
battery.charge_cycles = (battery.charge_cycles or 0) + 1
battery.battery_percentage = 100
db.add(BatteryPctLog(
battery_id=battery_id,
percentage=100,
recorded_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
source="charge",
))
db.add(ChargeLog(battery_id=battery_id, charged_date=date_val,
increment_cycles=increment, notes=notes))
db.commit()