power_ab.py --budka any: the freshest frame from whichever box holds the meter
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d15c057c4f
commit
562b93364d
1 file changed
+19
-9
+19
-9
@@ -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()
|
||||
|
||||
|
||||
Reference in new issue
Block a user