power_ab.py: --budka -- the meter read through a Chytra Budka (cbprom), charge from its Wh counter

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-14 01:54:27 +01:00
1 parent 14550b5d66
commit f2dd7e8082
1 file changed
+46 -6
+46 -6
View File
@@ -47,8 +47,41 @@ EXPORTER = "http://localhost:9877/metrics"
SETTLE_DEFAULT = 60
def meter_sample(url, meter=None):
CBPROM = "http://localhost:9878/metrics"
_cb_volts = None # the voltage that turns the cbprom Wh counter into mAh, fixed per run
def cbprom_sample(url, meter, device):
"""The same reading from a Chytra Budka relaying the meter (cbprom.py bridge).
The box publishes V/I/P/Wh/temp only -- the capacity counter is decoded but
not sent -- so the charge comes from the energy counter (10 mWh steps) over
one voltage held for the whole run: ~2 mAh at 5 V, twice the UC96's own step.
"""
global _cb_volts
with urllib.request.urlopen(url, timeout=10) as r:
body = r.read().decode()
want = (f'device="{device}"', f'meter="{meter}"')
out = {}
for line in body.splitlines():
if line.startswith("cb_meter_") and all(w in line for w in want):
out[line.split("{", 1)[0]] = float(line.rsplit(" ", 1)[1])
if "cb_meter_energy_wh" not in out or "cb_meter_voltage_volts" not in out:
raise SystemExit(f"cbprom nema meter {meter} z budky {device}")
age = time.time() - out.get("cb_meter_last_seen_timestamp_seconds", 0.0)
if age > 30:
raise SystemExit(f"posledni ramec pred {age:.0f} s -- serie je zamrzla, ne stabilni")
if _cb_volts is None:
_cb_volts = out["cb_meter_voltage_volts"]
return (out["cb_meter_energy_wh"] * 1000.0 / _cb_volts, out["cb_meter_voltage_volts"],
out.get("cb_meter_current_amps", 0.0),
out.get("cb_meter_last_seen_timestamp_seconds", 0.0))
def meter_sample(url, meter=None, device=None):
"""One reading of the exporter: (capacity_mAh, volts, amps, unix_ts)."""
if device:
return cbprom_sample(url, meter, device)
with urllib.request.urlopen(url, timeout=10) as r:
body = r.read().decode()
out = {}
@@ -243,14 +276,21 @@ def main():
ap.add_argument("--poll", type=float, default=10.0,
help="jak casto vzorkovat citac kapacity v okne (s, vychozi 10)")
ap.add_argument("--meter", help="MAC merace bez dvojtecek, kdyz exporter vidi vic meraku")
ap.add_argument("--url", default=EXPORTER)
ap.add_argument("--url", default=None, help=f"exporter (vychozi {EXPORTER}, s --budka {CBPROM})")
ap.add_argument("--budka", metavar="DEVICE",
help="merak cte Chytra Budka (id, napr. fbfe18): data z cbprom, vyzaduje --meter")
ap.add_argument("--json", help="ulozit vysledek jako JSON")
args = ap.parse_args()
if not args.cell:
raise SystemExit("aspon jedna --cell")
if args.budka and not args.meter:
raise SystemExit("--budka chce i --meter (budka cte vic meraku)")
if args.url is None:
args.url = CBPROM if args.budka else EXPORTER
err_ma = 60.0 / args.minutes # +-1 mAh quantisation over the window
# +-1 counter step over the window; through a budka the step is 10 mWh ~ 2 mAh
err_ma = (2.0 if args.budka else 1.0) * 60.0 / args.minutes
print(f"okno {args.minutes:g} min => kvantizace merice je +-{err_ma:.2f} mA na bunku")
if err_ma > 1.5:
print(" POZOR: to je vic, nez kolik ceka vetsina uspor. Delsi --minutes, nebo "
@@ -309,7 +349,7 @@ def main():
"state": state, "tx_air_delta": None, "rx_air_delta": None,
"samples": 0, "invalid": "neaplikovano"})
continue
cap0, v0, _, _ = meter_sample(args.url, args.meter)
cap0, v0, _, _ = meter_sample(args.url, args.meter, args.budka)
t0 = time.time()
print(f" start: cap={cap0:.0f} mAh, U={v0:.2f} V, stav={state}")
@@ -321,7 +361,7 @@ def main():
next_beat = t0 + 60
while time.time() < deadline:
time.sleep(min(args.poll, max(0.0, deadline - time.time())))
cap, v1, i1, _ = meter_sample(args.url, args.meter)
cap, v1, i1, _ = meter_sample(args.url, args.meter, args.budka)
now = time.time()
series.append((now - t0, cap))
if now >= next_beat:
@@ -330,7 +370,7 @@ def main():
print(f" ... {el:.0f}/{args.minutes:g} min, +{cap-cap0:.0f} mAh, "
f"zatim {(cap-cap0)/((now-t0)/3600.0):.1f} mA", flush=True)
cap1, v1, i1, _ = meter_sample(args.url, args.meter)
cap1, v1, i1, _ = meter_sample(args.url, args.meter, args.budka)
series.append((time.time() - t0, cap1))
dt_h = (time.time() - t0) / 3600.0
tx1, rx1 = con.air() if con else (None, None)