From 562b93364d8af78fb4c48858957da981d6e76005 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 14 Nov 2026 02:22:24 +0100 Subject: [PATCH] power_ab.py --budka any: the freshest frame from whichever box holds the meter Co-Authored-By: Claude Opus 5.5 --- tools/power_ab.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tools/power_ab.py b/tools/power_ab.py index 65516ee6..d2228522 100755 --- a/tools/power_ab.py +++ b/tools/power_ab.py @@ -59,20 +59,30 @@ def cbprom_sample(url, meter, device): one voltage held for the whole run: ~2 mAh at 5 V, twice the UC96's own step. """ global _cb_volts - want = (f'device="{device}"', f'meter="{meter}"') - # The box drops the meter for half a minute now and then (it killed a cell - # 25 min in on 14. 11. 2026). A gap only thins the fitted series, so wait it - # out; a feed that stays stale for minutes is a dead one. + # The meter takes one connection, and every box that has it allowlisted + # competes for it: on 14. 11. 2026 it hopped between fbfe18 and 4faa24 and + # each one's series went stale in turn -- two cells lost. The counters live in + # the meter, so any box's frame is the same reading: take the freshest one + # (`--budka any`, or a comma list of boxes). A gap still only thins the fit, + # so wait it out; a feed stale for minutes is a dead one. + boxes = None if device == "any" else set(device.split(",")) age = 0.0 for attempt in range(25): with urllib.request.urlopen(url, timeout=10) as r: body = r.read().decode() - out = {} + per_box = {} 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: + if not line.startswith("cb_meter_") or f'meter="{meter}"' not in line: + continue + m = re.search(r'device="([^"]+)"', line) + if not m or (boxes is not None and m.group(1) not in boxes): + continue + per_box.setdefault(m.group(1), {})[line.split("{", 1)[0]] = float(line.rsplit(" ", 1)[1]) + usable = [o for o in per_box.values() + if "cb_meter_energy_wh" in o and "cb_meter_voltage_volts" in o] + if not usable: raise SystemExit(f"cbprom nema meter {meter} z budky {device}") + out = max(usable, key=lambda o: o.get("cb_meter_last_seen_timestamp_seconds", 0.0)) age = time.time() - out.get("cb_meter_last_seen_timestamp_seconds", 0.0) if age <= 30: break @@ -288,7 +298,7 @@ def main(): ap.add_argument("--meter", help="MAC merace bez dvojtecek, kdyz exporter vidi vic meraku") 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") + help="merak cte Chytra Budka: id (napr. fbfe18), seznam id pres carku, nebo any = nejcerstvejsi ramec z kterekoli; data z cbprom, vyzaduje --meter") ap.add_argument("--json", help="ulozit vysledek jako JSON") args = ap.parse_args()