diff --git a/goodwe_controller/app/carmode.py b/goodwe_controller/app/carmode.py index d49f41d..5cdeceb 100644 --- a/goodwe_controller/app/carmode.py +++ b/goodwe_controller/app/carmode.py @@ -310,6 +310,9 @@ class ManualWriter: before our own write of this step, so the current is not judged and the count stands.""" known, diff = [], None + if cur is None: + judge = False # an unknown read: the count neither starts + # nor clears (phases are still judged) if judge: known = self._own_values() if known and cur is not None: @@ -492,6 +495,7 @@ class ManualWriter: async def _advance(self, drv, r, cur, now, tracker, want_a, clamp, rev): q = self.seq + pressed = q.pop("pressed", False) # covers the first reading only if q["stage"] in ("zero", "select") and cur is not None \ and cur >= CH.MIN_A: # Something else raised the current while the car was being @@ -500,9 +504,11 @@ class ManualWriter: # changes something. self.seq = None self.last_a = self.attempted_a = self.prior_a = None - if q.get("pressed"): - # EV-22 (DA1): the user's change came during this change: the - # charger is taken as it is, never held over that value. + if pressed: + # EV-22 (DA1): the user's change came during this change and + # this is the first reading judged after it: the charger is + # taken as it is, never held over that value. A later raise + # is held as ever. self._took_as_is() return self.hold = self.other = True @@ -968,12 +974,13 @@ class SolarWriter(ManualWriter): self.refused_cut = None # a cut value the charger did not take self._cap_end = False # EV-60: a capped close ended the change # EV-22 (D4): the phases Solar itself chose. `_own_n`: the last it - # TOOK (or the start state); `_sel_n` and `_win_end`: its latest - # select try and the SETTLE_S after that try returned, in which a - # read-back is not judged. + # TOOK (or the start state); `_sel_n` and `_win_t1`: its latest + # select try and when that try returned; SETTLE_S after it (and the + # one step that closes there, `_win_last`) a read-back is not judged. self._own_n = None self._sel_n = None - self._win_end = None + self._win_t1 = None + self._win_last = False def owe_start(self) -> None: """C1: the add-on starts in Off while an exit 0 A of its own was still @@ -1349,7 +1356,8 @@ class SolarWriter(ManualWriter): unless it is kept.""" self._own_n, self._ph_diff = None, False if window: - self._sel_n = self._win_end = None + self._sel_n = self._win_t1 = None + self._win_last = False def _select_try(self, n, ok) -> None: """EV-22 (DA3): our own select try returned. SETTLE_S from now a @@ -1357,23 +1365,31 @@ class SolarWriter(ManualWriter): taken select is our phases at once, one the charger did not confirm becomes ours if a read inside the window shows it.""" self._sel_n = n - self._win_end = float(self.last_done) + SETTLE_S + self._win_t1 = float(self.last_done) + self._win_last = False if ok: self._own_n, self._ph_diff = n, False def _judge_phases(self, r, now) -> None: """EV-22 (D4): the read-back phases against the phases Solar itself chose (never the plan's adopted `n`). Unknown phases and reads inside - a select's window start nothing and lift nothing.""" + a select's window start nothing and lift nothing. The window is the + sequence's settle time on the same clock and with the same + comparison as the close (`now - t1 < SETTLE_S`), and the one step + that closes at its end is still inside it, whatever fraction of a + second `last_done` lies after the step's `now`.""" p = getattr(r, "phases_allowed", None) - if p not in (1, 3): - return - if self._win_end is not None: - if now <= self._win_end: - if p == self._sel_n: + if self._win_t1 is not None: + inside = now - self._win_t1 < SETTLE_S + if not inside and not self._win_last: + self._win_last = inside = True # the closing step + if inside: + if p in (1, 3) and p == self._sel_n: self._own_n, self._ph_diff = p, False # the take return - self._sel_n = self._win_end = None + self._sel_n = self._win_t1 = None + if p not in (1, 3): + return if self._own_n is None: self._own_n, self._ph_diff = p, False # the start state return diff --git a/goodwe_controller/test_ev22.py b/goodwe_controller/test_ev22.py index 722b6ef..a4909c3 100644 --- a/goodwe_controller/test_ev22.py +++ b/goodwe_controller/test_ev22.py @@ -1047,4 +1047,164 @@ try: finally: CM.PHASE_GAP_S = keep_gap +# =========================================================================== # +print("\nREVIEW ROUND 1 (rows A-D, R1, the window's closing step)") +# =========================================================================== # + + +class NoneSim(SimCharger): + """The read-back and the setpoint answer with a non-number while + `badcur` (the charger is available, `cur` is None).""" + badcur = False + + def _build(self): + out = super()._build() + if self.badcur: + for k in list(out): + if k.endswith("max_current_socket_1") and \ + ("number." in k or "main_active" in k): + out[k] = dict(out[k], state="n/a") + return out + + +def badcur(on): + def f(p, c): + p.ch.badcur = on + return f + + +class LateSim(SimCharger): + """Ignores current writes while t0 <= sim t < t1 and remembers the last; + `late` (an event) shows it: a write the charger took late.""" + owed = None + t0, t1 = 0.0, 0.0 + + def request_current(self, a): + if self.t0 <= self.t < self.t1: + self.owed = a + keep, self.refuse = self.refuse, "ignore" + try: + return super().request_current(a) + finally: + self.refuse = keep + return super().request_current(a) + + +def late(p, c): + if p.ch.owed is not None: + p.ch.set_current(p.ch.owed) + + +T0Z = int(([x[2] for x in r13["c"].car_solar.log + if x[0] == "current" and x[1] == 0.0 and x[2] > 400] + or [580.0])[0]) +# A (Major 1, Solar): the press covers the first reading only; a later raise +# in the same zero stage is held at once. +n, rec = LOG.mark(), Rec() +r = solar(800, sun=R39, sim=slow_sim(), rec=rec, + events={T0Z + 2: act("car_mode", "solar"), T0Z + 8: setc(16)}) +check(f"T44 (A): Solar, a press at {T0Z + 2} in the zero stage, another " + f"writer's 16 A at {T0Z + 8}: the raise check holds at once " + f"({rec.first('hold', lambda v: v, T0Z + 8)}), nothing written over it " + f"({after(r, T0Z + 8)})", + (rec.first("hold", lambda v: v, T0Z + 8) or 99999) <= T0Z + 9 + and after(r, T0Z + 8) == []) +# B (Major 1, Manual) +rec = Rec() +r = man(300, {102: act("car_current", 14), 106: setc(9)}, rec=rec, + sim=slow_sim()) +check(f"T43 (B): Manual, the user sets 14 A at 102 in the zero stage, another " + f"writer's 9 A at 106: held at once " + f"({rec.first('mhold', lambda v: v, 106)}), nothing written over it " + f"({after(r, 107)})", + (rec.first("mhold", lambda v: v, 106) or 99999) <= 107 + and after(r, 107) == [] and r["sim"].setpoint_a == 9.0) +# C (Major 2, Solar): an unknown read-back leaves the count alone. +n, rec = LOG.mark(), Rec() +r = solar(700, sun=R39, sim=NoneSim(setpoint_a=0), rec=rec, + events={TS + 3: setc(16), TS + 9: badcur(True), + TS + 12: badcur(False)}) +check(f"T9 (C): another writer's 16 A at {TS + 3}, an unknown read-back " + f"{TS + 9}-{TS + 11}: the count stands " + f"({[rec.d[t]['ms'] for t in (TS + 8, TS + 10, TS + 12)]}), nothing " + f"is written over the 16 A ({after(r, TS + 3)}), the hold comes", + rec.every("ms", lambda v: v is not None, TS + 5, TS + 14) + and after(r, TS + 3) == [] + and rec.first("hold", lambda v: v) is not None) +# D (Major 2, Manual) +rec = Rec() +r = man(300, {104: setc(9), 109: badcur(True), 113: badcur(False)}, rec=rec, + sim=NoneSim(setpoint_a=0)) +check(f"T1 (D): Manual, 9 A at 104, an unknown read-back 109-112 over the " + f"close at 111: no write over the 9 A ({after(r, 104)}), the count " + f"stands ({[rec.d[t]['mm'] for t in (108, 110, 112)]})", + after(r, 104) == [] and rec.every("mm", lambda v: v is not None, 106, 112)) +# R1: the sequence's own values count as own after a press (formal note). +lm = LateSim(setpoint_a=0) +lm.t0, lm.t1 = 110.0, 120.0 +rec = Rec() +r = man(260, {113: act("car_current", 12), 120: late}, rec=rec, sim=lm) +check(f"T43 (R1, Manual): the close refused until 120, a press at 113 clears " + f"`attempted_a`, the charger then shows the late close: own value, " + f"never a mismatch or a hold " + f"({rec.first('mm', lo=100)}, {rec.first('mhold', lambda v: v)})", + rec.first("mm", lo=100) is None + and rec.first("mhold", lambda v: v) is None) +ls = LateSim(setpoint_a=0) +ls.t0, ls.t1 = TS + 10.5, TS + 14.0 # the close at 592 (sim 593) ignored +rec = Rec() +r = solar(700, sun=R39, sim=ls, rec=rec, + events={TS + 12: act("car_mode", "solar"), TS + 14: late}) +check(f"T44 (R1, Solar): the close refused from 592, a press at {TS + 12}, " + f"the charger then shows the late close: own value, never a mismatch " + f"or a hold ({rec.first('ms', lo=TS)}, " + f"{rec.first('hold', lambda v: v)})", + rec.first("ms", lo=TS) is None + and rec.first("hold", lambda v: v) is None) + + +# Finding 4: `last_done` a fraction of a second after the step's `now`: the +# step that closes is still inside the window. +def frac_run(taken_late): + w = swriter(0.0, ph=3) + w.plan.a, w.plan.n = 12.0, 1 + drv = Drv(0.0, ph=3) + w.seq = {"stage": "settle", "t0": 119.0, "t1": 121.4, "n": 1, "tries": 0, + "prev": None, "from": 3, "own": [0.0], "sel": True} + w.last_done = 121.4 + w._own_n = 3 + w._select_try(1, not taken_late) + ms = {} + + def pre(w_, d, t): + if taken_late: + d.ph = 1 if t >= 132 else 3 # the take is first seen at 132 + else: + d.ph = 3 if t >= 124 else 1 # another writer's 3 phases + ms[t] = w_.mismatch_since + trk = Trk() + for t in range(122, 138): + drv.now = float(t) + pre(w, drv, t) + rd = Rd(drv.cur, drv.ph, power=0.0 if drv.cur == 0 else 1840.0) + LOOP.run_until_complete(w.step( + drv, rd, mode=CM.SOLAR, sun=CM.SunInputs(target_w=2000.0), + clamp=16.0, now=float(t), tracker=trk, writing=True, + status="charging")) + ms[t] = w.mismatch_since + return w, drv, ms + + +w, drv, ms = frac_run(False) +cl = [(int(t), a) for t, a in drv.calls if a > 0] +check(f"T29 (fraction): last_done 121.4, the close step at 132 (10.6 s later) " + f"is inside the window: the close is written at 132 ({cl}), no " + f"mismatch at 132 ({ms[132]}), the mismatch from 133 ({ms[133]})", + cl and cl[0][0] == 132 and ms[132] is None and ms[133] == 133.0) +w, drv, ms = frac_run(True) +check(f"T48 (fraction): a take first seen on the closing step (132, last_done " + f"121.4) is Solar's own: own phases {w._own_n}, no mismatch " + f"({ms[132]}, {ms[134]})", + w._own_n == 1 and ms[132] is None and ms[134] is None) + report() diff --git a/goodwe_controller/test_ev22_fuzz.py b/goodwe_controller/test_ev22_fuzz.py index 9fe1a79..657aa34 100644 --- a/goodwe_controller/test_ev22_fuzz.py +++ b/goodwe_controller/test_ev22_fuzz.py @@ -295,7 +295,7 @@ def fuzz_site(seed): if sim.own_ph is None and getattr(w, "_own_n", None) is not None: sim.own_ph = w._own_n HIST[t] = (w.mismatch_since, w.hold, getattr(w, "_own_n", None), - getattr(w, "_win_end", None), + getattr(w, "_win_t1", None), None if w.seq is None else w.seq["stage"], p.ch.phases_allowed, p.ch.own_ph, p.ch.ph_other_at, w.sent_a, p.ch.setpoint_a) diff --git a/goodwe_controller/test_ev60.py b/goodwe_controller/test_ev60.py index c5c8038..0850701 100644 --- a/goodwe_controller/test_ev60.py +++ b/goodwe_controller/test_ev60.py @@ -26,8 +26,6 @@ Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import asyncio -import hashlib -import inspect import logging import os import sys @@ -1074,7 +1072,7 @@ check("N5: no select while the car drew (0 A not taken), over every run", check("N9: no exception out of a step or release in any run (every run " "completed), and no ERROR record on a cut try (G1)", not [e for r in CUT_ONLY for e in r.errs]) -# T13/N7: EV-60 review-time pin; retired by EV-22, which changes ManualWriter by design (C7, DA1) +# EV-60 review-time pin; retired by EV-22, which changes ManualWriter by design (C7, DA1) def tsrc(name):