diff --git a/goodwe_controller/app/carmode.py b/goodwe_controller/app/carmode.py index 4eaba57..fe8165e 100644 --- a/goodwe_controller/app/carmode.py +++ b/goodwe_controller/app/carmode.py @@ -840,6 +840,11 @@ class SolarPlan: self.a, self.set_at, self.last_dir = CH.MIN_A, now, -1 +class _CloseHeld(Exception): + """EV-60: Solar's cut gate holds the close (raised inside `_advance`'s + want-a call, caught in `SolarWriter._advance`; never leaves it).""" + + class SolarWriter(ManualWriter): """What Solar does to the charger, one `step()` per cycle. It shares ManualWriter's write, phase sequence and other-writer watch. @@ -1102,23 +1107,33 @@ class SolarWriter(ManualWriter): it). The sequence's first cut close waits at 0 A until RETRY_S after the last refused cut try (counts nothing, ends nothing); after a cut close of the sequence went, the 5 s spacing of `_advance` rules.""" - q = self.seq - if q["stage"] == "settle" and now - q["t1"] >= SETTLE_S \ - and callable(want_a): - hi = CH.ENTITY_MAX_A if clamp is None else float(clamp) - a = min(max(float(want_a(q)), CH.MIN_A), hi) - if self._cut_try(a): - if self.cut_fails >= MAX_TRIES: - self.seq, self.last_a, self.reapply = None, None, True - self.rev_applied = None - self.plan.end_switch(False) - self._cap_end = True - return - if not q.get("cut_went") and self.cut_fail_at is not None \ - and now - self.cut_fail_at < RETRY_S: - return - q["cut_went"] = True - await super()._advance(drv, r, cur, now, tracker, want_a, clamp, rev) + self._cap_end = False # set again below, read by `step` only + if callable(want_a): + # The gate runs where `_advance` asks for the close value, just + # before it writes: also on the "car kept drawing" path, which + # turns zero into settle and closes in the same call. + inner, hi = want_a, (CH.ENTITY_MAX_A if clamp is None + else float(clamp)) + + def want_a(q): + v = inner(q) + if self._cut_try(min(max(float(v), CH.MIN_A), hi)): + if self.cut_fails >= MAX_TRIES: + self.seq, self.last_a, self.reapply = None, None, True + self.rev_applied = None + self.plan.end_switch(False) + self._cap_end = True + raise _CloseHeld + if not q.get("cut_went") and self.cut_fail_at is not None \ + and now - self.cut_fail_at < RETRY_S: + raise _CloseHeld + q["cut_went"] = True + return v + try: + await super()._advance(drv, r, cur, now, tracker, want_a, clamp, + rev) + except _CloseHeld: + pass def _failed(self, now) -> None: if self.sent_cut: diff --git a/goodwe_controller/test_ev60.py b/goodwe_controller/test_ev60.py index b268b22..e60dd6e 100644 --- a/goodwe_controller/test_ev60.py +++ b/goodwe_controller/test_ev60.py @@ -3,12 +3,19 @@ quarter key (the phase change's 0 A and the capped close included), and a Solar press during the close retries gives the value MAX_TRIES tries. `python3 -B test_ev60.py` -Rows T1-T24 (with T1b, T2b, T2c, T3b, T3c, T8b, T21b) and N1-N14 of -reports/EV-60-test-plan.md. Here: T1-T11, T14 (writer level), T16-T22, T24 -(the shutdown rows against `Base60`), and the source checks of T15, T23 and -N7, N10, N11. Carried by the other suites, green: T12 (test_ev54*), T13 -(test_ev59 Manual rows, test_ev56, test_ev28, test_ev15, test_ev04), T15 -(test_ev56 T12, "T20 T12 [R]"), T23. +Rows T1-T24 (with T1b, T2b, T2c, T3b, T3c, T5b, T8b, T21b) and N1-N14 of +reports/EV-60-test-plan.md, plus the rows of brief ruling "scope (a)" (the +"car kept drawing" close, here "S-a" and "S-b"). Every id has a check in this +file whose label starts with the id: +T1-T11 (T11a-c), T12 (EV-54 `_put` as base, against `Base60`), T13 (the +Manual source pin, same check as N7), T14, T15 (Solar's non-cut refused close, +independent of test_ev56's golden), T16-T22 (T22 has its own check), T24, +S-a/S-b, the stale-flag row "T25-flag", and N1-N14 (N4, N6, N11, N12, N13 are +generic checks over every run or source checks). +Also carried by other suites, named by check id (all run by CI): T12: +test_ev54 and test_ev54_drop; T13: test_ev59 "T1", "T2", "T24a"; T15: +test_ev56 "T12: 3 close tries 5 s apart" (it asserts `fails == MAX_TRIES`); +T23 is the CI suite run itself and has no check here. Writer level: `SolarWriter` on a stub driver, a stub tracker and a stub reading; time is the 1 s step counter, never wall time. R = the answer False, X = it raises. `Base60` is 2e09622's Solar (the three methods this ticket @@ -179,7 +186,7 @@ def state(w, drv): def world(kind, until, *, start=0, setup=None, pre=None, post=None, - take=None, cls=None, cur=8.0, sun=None, avail=None): + take=None, cls=None, cur=8.0, sun=None, avail=None, power=None): """One writer-level run from `start` to `until` (both included). `take` (a, drv, w) -> True: the charger takes the write; the others are refused (R) or raise (X) as `kind`'s characters say, one per refused write, the @@ -211,6 +218,7 @@ def world(kind, until, *, start=0, setup=None, pre=None, post=None, r.w, r.drv = w, drv if setup: setup(w, drv) + r.pre0, r.key0, r.held, r.held_max = w.cut_fails, w.cut_key, 0, 0 for t in range(start, until + 1): drv.now = float(t) CLOCK["t"] = t @@ -218,15 +226,25 @@ def world(kind, until, *, start=0, setup=None, pre=None, post=None, pre(w, drv, t) w.cut_quarter(int(t // 900)) rd = Rd(drv.cur, drv.ph, power=(0.0 if drv.cur == 0 else 1840.0)) + if power is not None: + rd.power_w = power(t, drv, w) if avail is not None and not avail(t): rd.available = False had = w.seq is not None + n_before = len(drv.calls) s = sun(t) if sun else CM.SunInputs(target_w=2000.0) LOOP.run_until_complete(w.step( drv, rd, mode=CM.SOLAR, sun=s, clamp=16.0, now=float(t), tracker=trk, writing=True, status="charging")) if had and w.seq is None and r.ended is None: r.ended = t + # N13: steps in a row a sequence sits in settle with nothing written + if (w.seq is not None and w.seq.get("stage") == "settle" + and len(drv.calls) == n_before): + r.held += 1 + r.held_max = max(r.held_max, r.held) + else: + r.held = 0 r.snap[t] = state(w, drv) if post: post(w, drv, t) @@ -438,6 +456,7 @@ for k in "R": r = route_a(k, until=1500, plan_a=6.0, take=lambda a, d, w: d.now >= 900) z = [t for t in r.zeros if t >= 900] + R5A = r check(f"T5a: at key 1 cut_fails is 0 and Solar controls again: 0 A at 900 " f"({z[:1]}), 6 A close, plan.n 3, seq None, cut_warned False " f"({r.snap[900]['cf']}, {r.w.plan.n}, {r.w.cut_warned})", @@ -736,13 +755,14 @@ check(f"T9: the close value moves 8, 7, 6 A at 10, 15, 20: fails 3, gave_up, " f"REF at 20, seq None ({S9})", S9 == ([(10, 8.0), (15, 7.0), (20, 6.0)], 3, True, [20], None)) -setup, pre = pre_roll(0, 0) r = t2b("R", extra_pre=lambda w, d, t: w.user_changed() if t == 162 else None) cl = [t for t, a in r.nz if a == 6.0] -check(f"T17: a Solar press between close 1 and close 2 resets neither " - f"cut_fails nor the sequence's tries: closes {cl}, cut_fails " - f"{r.w.cut_fails}, end as T6, no give-up", +check(f"T17: T2b with a Solar press at 162, between close 1 (160) and close 2 " + f"(165): the press resets neither cut_fails (3 in all, not 1 or 0) nor " + f"the sequence's tries (no 3rd close, the end at 170): closes {cl}, " + f"cut_fails {r.w.cut_fails}, end as T6, no give-up", near(cl, [160, 165], 1) and r.w.cut_fails == 3 and not r.w.gave_up + and r.snap[161]["cf"] == 2 and r.snap[163]["cf"] == 2 and r.ended and abs(r.ended - 170) <= 1 and r.snap[r.ended]["re"] and not r.snap[r.ended]["sw"]) @@ -886,7 +906,126 @@ check(f"T24: shutdown during T1's wait: no exception, release as base " f"({d24.calls})", res24 in (False, None) and len(d24.calls) in (0, 3)) # =========================================================================== # -print("\n6. THE NEVERS OVER EVERY RUN, SOURCE CHECKS (N1-N14, T15, T23)") +print("\n5b. THE CAR KEEPS DRAWING (S-a, S-b), T12, T15, T22, THE STALE FLAG") +# =========================================================================== # + + +def drawing(preset, fail_at, cls=None): + """A phase change begins at 814 with no cap (0 A taken); the car keeps + drawing (power 1840 W), the cap arrives at 815; at the 20 s timeout (834) + `_advance` turns zero into settle and closes in the same call: a cut try.""" + def setup(w, d): + w.cut_key, w.cut_fails, w.cut_fail_at = 0, preset, float(fail_at) + + def pre(w, d, t): + if t == 814: + w.plan._set(8.0, 814.0, 3) + + def post(w, d, t): + if t == 815: + set_cap(w, 6.0) + return world("R", 880, start=810, setup=setup, pre=pre, post=post, + cls=cls, power=lambda t, d, w: 1840.0) + + +r = drawing(3, 790) +check(f"S-a: the car keeps drawing, cut_fails 3: at the timeout (834) no " + f"close is written ({r.drv.calls}), the sequence ends at 0 A (seq None, " + f"switch False, reapply), cut_fails 3, no give-up, no select, no REF", + r.drv.calls == [(814.0, 0.0)] and r.ended is not None + and abs(r.ended - 834) <= 1 and r.w.seq is None and r.w.reapply + and not r.w.plan.switch and r.w.cut_fails == 3 and not r.w.gave_up + and not r.drv.phases_log and not r.refs and not r.errs) +r = drawing(1, 790) +cl = [t for t, a in r.nz] +check(f"S-b: the car keeps drawing, last refused cut try at 790: the close " + f"waits at 0 A (no write 834-849, seq in settle, cut_fails 1, no " + f"give-up), goes at 850 (cut_fail_at + RETRY_S), then 855 ({cl}); " + f"cut_fails 3; the end 5 s later", + near(cl, [850, 855], 1) and all(r.snap[t]["seq"] + and r.snap[t]["seq"][0] == "settle" + and r.snap[t]["cf"] == 1 + and not r.snap[t]["gu"] + for t in range(835, 850)) + and r.w.cut_fails == 3 and r.ended and abs(r.ended - 860) <= 1 + and not r.w.gave_up and not r.refs and not r.errs) +r = drawing(1, 790, cls=Base60) +check("S-a/S-b self-check: Base60 writes the close at the timeout " + f"({[t for t, a in r.nz]}, cut_fails {r.w.cut_fails})", + [t for t, a in r.nz][:1] == [834] and r.w.cut_fails >= 2) + +# T12: EV-54's `_put` cut path is as base (3 tries per key, the next key again) +for k in "RX": + def put_world(cls, k=k): + return world(k, 1500, cls=cls, take=lambda a, d, w: False, + setup=lambda w, d: (set_cap(w, 6.0), + setattr(w.plan, "a", 6.0))) + a, b = put_world(None), put_world(Base60) + check(f"T12 [{k}]: `_put`'s cut path as base: same calls as Base60 " + f"({a.tries}), 3 tries in key 0 and 3 in key 1", + a.drv.calls == b.drv.calls and per_key(a.tries) == {0: 3, 1: 3} + and not a.w.gave_up) + +# T15: Solar's non-cut refused close (test_ev56 T12's shape), independent of +# that suite's golden: 3 closes 5 s apart, then the end, `fails` MAX_TRIES +for k in "RX": + r = sp3(k) + ts_ = [t for t, _a in r.nz] + check(f"T15 [{k}]: 3 close tries 5 s apart ({ts_}), then seq None, the " + f"change ended (switch False), the car at 0 A, fails == MAX_TRIES " + f"({r.w.fails}), gave_up, one REF_S ({r.refs}), one select only " + f"({len(r.drv.phases_log)})", + len(ts_) == 3 and [b - a for a, b in zip(ts_, ts_[1:])] == [5, 5] + and r.w.seq is None and not r.w.plan.switch and r.drv.cur == 0.0 + and r.w.fails == CM.MAX_TRIES and r.w.gave_up and len(r.refs) == 1 + and len(r.drv.phases_log) == 1) + +# T22: the spacing rule, on its own: a sequence's first cut close is at least +# RETRY_S after `cut_fail_at`, later ones 5 s apart; `_begin_solar`'s cut-try +# 0 A is at least RETRY_S after it too +rb, rc = t2b("R"), t2c("R") +setup, pre = pre_roll(870, 874) +rd_ = world("R", 990, start=870, setup=setup, pre=pre, post=cap_in_settle()) +r1 = t1b("R") +c_b = [t for t, a in rb.nz if a == 6.0] +c_c = [t for t, a in rc.nz if a == 6.0] +c_d = [t for t, a in rd_.nz if a == 6.0] +check(f"T22: first cut close >= RETRY_S after cut_fail_at: T2b {c_b[0]} vs " + f"100, T2c {c_c[0]} vs 100, T21b {c_d[0]} vs 870; the next ones 5 s " + f"apart ({c_b[1] - c_b[0]}, {c_c[1] - c_c[0]}, {c_d[1] - c_d[0]}); " + f"T1b's 0 A at {r1.zeros[1]} vs 0", + c_b[0] - 100 >= CM.RETRY_S and c_c[0] - 100 >= CM.RETRY_S + and c_d[0] - 870 >= CM.RETRY_S and c_b[1] - c_b[0] == 5 + and c_c[1] - c_c[0] == 5 and c_d[1] - c_d[0] == 5 + and r1.zeros[1] - r1.zeros[0] >= CM.RETRY_S) + +# T25-flag: a stale `_cap_end` cannot make a later sequence's end skip +# `step`'s end-of-sequence handling (reset at each `_advance` entry) +w25 = solar_writer(8.0) +w25._cap_end = True # as a direct `_advance` caller leaves it +d25 = Drv(8.0, lambda a, n: True) +d25.now = 20.0 +w25.seq = {"stage": "settle", "t0": 0.0, "n": 3, "tries": 0, "prev": 8.0, + "t1": 0.0} +LOOP.run_until_complete(w25._advance(d25, Rd(0.0, 1, power=0.0), 0.0, 20.0, + Trk(), lambda q: 8.0, 16.0, 1)) +check("T25-flag: `_advance` resets `_cap_end` at entry (a taken close leaves " + "it False)", w25._cap_end is False and w25.seq is None) +r = world("R", 60, start=1, setup=lambda w, d: ( + w.plan._set(8.0, 0.0, 3), setattr(w, "_cap_end", True), + setattr(w.plan, "target_w", 1500.0)), + take=lambda a, d, w: True) +check(f"T25-flag: with a stale flag set before a normal change (the close is " + f"6 A, below the plan's 8 A), the change still ends through `step`'s " + f"end handling, which sets plan.a to the close value: plan.switch " + f"{r.w.plan.switch}, plan.n {r.w.plan.n}, plan.a {r.w.plan.a}, last " + f"write {r.drv.calls[-1]}, flag {r.w._cap_end}", + not r.w.plan.switch and r.w.plan.n == 3 and r.w.plan.a == 6.0 + and r.w.seq is None and r.w._cap_end is False + and r.drv.calls[-1][1] == 6.0) + +# =========================================================================== # +print("\n6. THE NEVERS OVER EVERY RUN, SOURCE CHECKS (N1-N14, T13, T23)") # =========================================================================== # worst = max((max(per_key(r.tries).values(), default=0) - r.cap_ for r in ALL), default=0) @@ -906,7 +1045,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]) -check("N7: ManualWriter's source is 2e09622's, byte for byte", +check("T13/N7: ManualWriter's source is 2e09622's, byte for byte", hashlib.sha256(inspect.getsource(CM.ManualWriter).encode()) .hexdigest() == MANUAL_SHA) @@ -923,6 +1062,75 @@ check("N10 (write_phases and the notices untouched): one write_phases( and " and src.count("asyncio.sleep") == 1 and src.count("create_task") == 0 and src.count("_LOG.warning(") == 10) +# N4: no cut try within RETRY_S of the last refused one, except the 5 s of +# closes in one sequence (also across a key roll: T21b), over every run +bad4 = [(r.kind, r.tries) for r in ALL + if [g for g in (b - a for a, b in zip(r.tries, r.tries[1:])) + if g < CM.RETRY_S and abs(g - 5) > 1]] +check(f"N4: consecutive refused cut tries are >= RETRY_S apart or 5 s apart " + f"(closes of one sequence), over {len(ALL)} runs ({bad4[:1]})", + not bad4) + +# N6: a non-cut give-up after exactly MAX_TRIES tries since the last press +# (or the episode's start), one REF_S per give-up +n6 = [] +for press in (None, 13, 18, 11): + for k in "RX": + r = sp3(k, press=press) + since = [t for t, _a in r.nz if press is None or t >= press] + last = r.refs[-1] if r.refs else None + n6.append((press, k, len(since[:since.index(last) + 1]) + if last in since else None, len(r.refs))) +check(f"N6: give-up after exactly MAX_TRIES tries since the press, one REF_S " + f"per give-up ({n6})", + all(n == CM.MAX_TRIES and refs == 1 for _p, _k, n, refs in n6)) + +# N11: the edits to existing tests are the ruled ones only (source checks) +s56, s59 = tsrc("test_ev56.py"), tsrc("test_ev59.py") +other = ("905c0b8ba89bb307", "343ea01aacfa1019", "ff6306ce56b35c64", + "f1fb141275839562", "369069be9187ab3c", "2cd78dee7ee93aaa", + "f0adb058a5bbefba", "6d7bcb747d1dc5a3", "2be1a760b699b28c", + "a5888e4557b4ee8f", "1ad97e089468d71b", "7baf8981ab382e05", + "54701b140ec6f30b", "0cfd3ccd334f378d", "1d85595302dc032a", + "5fc65dbdc730e539", "78ffe9cf72c5e214") +check("N11: test_ev56's goldens: every one but 'T20 T12 [R]' is base's, that " + "one is re-taken (25a205989dc44204, 2fd24ddbb18f14cc gone), 'T5b' still " + "base's 5 tries; T19b and T12 carry the ruled expectations", + all(f'"{d}"' in s56 for d in other) + and '"T20 T12 [R]": "25a205989dc44204"' in s56 + and '"T20 T12 [R]": "2fd24ddbb18f14cc"' not in s56 + and '"T5b": [(111.0, 6.0), (116.0, 6.0), (121.0, 6.0), (181.0, 6.0),' + in s56 + and '("a", c4c, [0, 60, 120], 3)' in s56 + and '("b", c4b, [0, 60, 120], 3)' in s56 + and 'e12[2]["fails"] == CM.MAX_TRIES' in s56 + and "S_PRESS == ([12, 17, 22, 82], 3, True, [82], 13)" in s59 + and "S_NONE == ([12, 17, 22], 3, True, [22], None)" in s59 + and "S14C == ([(10, 8.0), (15, 7.0), (20, 6.0)], 3, True, [20], None)" + in s59) + +# N12: one CHARGER_REFUSED (Q3W) per reaching of the cap in a key, over every +# run (a key counts as reached with the earlier tries it started with) +bad12 = [] +for r in ALL: + n_ = per_key(r.tries) + reached = [k_ for k_ in set(n_) | {r.key0} + if n_.get(k_, 0) + (r.pre0 if k_ == r.key0 else 0) >= r.cap_] + if len(r.q3w) > len(reached): + bad12.append((r.kind, r.q3w, n_)) +check(f"N12: never more Q3W lines than keys that reached the cap, over " + f"{len(ALL)} runs ({bad12[:1]})", not bad12) + +# N13: Solar is never stuck: a sequence sits in settle with no write at most +# RETRY_S + 5 s (the Q5 wait), and a gated change begins in the first step of +# the next key once the charger takes writes (T5a) +worst13 = max(r.held_max for r in ALL) +check(f"N13: longest settle without a write {worst13} s <= RETRY_S + 5; the " + f"gated change begins at {[t for t in R5A.zeros if t >= 900][:1]} " + f"(first step of key 1)", + worst13 <= CM.RETRY_S + 5 + and [t for t in R5A.zeros if t >= 900][:1] == [900]) + print() if fails: print(f"FAIL: {len(fails)} check(s)")