diff --git a/goodwe_controller/app/chargers.py b/goodwe_controller/app/chargers.py index 228b09f..a6bd23e 100644 --- a/goodwe_controller/app/chargers.py +++ b/goodwe_controller/app/chargers.py @@ -728,6 +728,8 @@ class CarTracker: if usable: self.last_ok = now self.last_good, self.last_good_utc = r, now_utc + if not r.available: + self._pend_lapse(now) if r.available: self._life(r, now) self._sig(r, now) @@ -833,6 +835,13 @@ class CarTracker: if max(t, arrival) > p["t"] + TOOK_S: self._pend_arm() + def _pend_lapse(self, now: float) -> None: + """A reading that did not answer shows no life either: once the window + has run out the refusal arms, dated the send, as it did before.""" + p = self._pend + if p is not None and now > p["t"] + TOOK_S: + self._pend_arm() + def _pend_arm(self) -> None: """No life in the window: arm as the write would have, dated its send.""" p, self._pend = self._pend, None diff --git a/goodwe_controller/test_ev13.py b/goodwe_controller/test_ev13.py index ecab25b..fe22e79 100644 --- a/goodwe_controller/test_ev13.py +++ b/goodwe_controller/test_ev13.py @@ -139,6 +139,9 @@ def play(timeline, mode="live", ok=False, was=16.0, to=10.0, pre_end=0, MODES = ("live", "ring", "polls") +N1, N2 = [], [] # (name, never stuck) / (name, seconds from the send to stuck) + + def pend(tr): """The refusal window a tracker keeps open (None: none).""" return getattr(tr, "_pend", None) @@ -147,6 +150,7 @@ def pend(tr): def life(name, changes, **kw): for m in MODES: tr, first, armed = play(steady(changes), m, **kw) + N1.append((f"{name} [{m}]", first is None and not tr.stuck_now)) check(f"{name} [{m}]: not armed, never stuck to TS+130", tr.armed_at is None and first is None and not armed and not tr.stuck_now) @@ -155,6 +159,7 @@ def life(name, changes, **kw): def armed(name, tl, at=TS, **kw): for m in MODES: tr, first, ever = play(tl, m, **kw) + N2.append((f"{name} [{m}]", None if first is None else first - TS)) check(f"{name} [{m}]: armed at the send, stuck at TS+60 (+-1), " f"not before TS+59 (first {first})", first is not None and TS + 59 <= first <= TS + 61 @@ -501,6 +506,7 @@ for beside, side in SIDES: len(res) == 3 and not any(r for _t, _a, r in res) and all(55 <= b - a <= 75 for a, b in zip(ts, ts[1:])) and w.c.car_writer.gave_up) + N1.append((nm, never_stuck(tr))) check(f"{nm}: the car shows life inside each window: never armed, " "never stuck, to 400 s", never_stuck(tr)) check(f"{nm}: after the 3rd try exactly ONE amber charger row, " @@ -529,6 +535,7 @@ for beside, side in SIDES: LOG.rec.clear() w, res, tr = run(t19(beside)) keep(f"T19-{side}", w, res) + N1.append((f"T19 [{side}]", never_stuck(tr))) check(f"T19 [{side}] the 0 A of a phase change is ignored and the car " "keeps drawing: tried MAX_TRIES times, no select, never armed or " "stuck", len(res) == carmode.MAX_TRIES @@ -560,6 +567,7 @@ for beside, side in SIDES: keep(f"T11-{side}", w, res) t_s = res[0][0] fs = first_stuck(tr) + N2.append((f"T11 [{side}]", None if fs is None else fs - t_s)) check(f"T11 [{side}] a paused car (B, 0 W) shows no life: it arms as " f"today, stuck at t_s+60 ({fs - t_s if fs else None})", w.sim.state == "B" and w.sim.power_w == 0.0 and fs is not None @@ -687,6 +695,7 @@ for beside, side in SIDES: rest = [x for x in res if x[1] == 16.0] fs = first_stuck(tr) t_r = rest[0][0] if rest else None + N2.append((f"T20 [{side}]", None if not (fs and t_r) else fs - t_r)) check(f"T20 [{side}] the 0 A is taken, the restore is refused " f"{len(rest)} times at 0 W with nothing changing: it arms as on " f"the base, dated the first refusal (stuck at +" @@ -849,6 +858,8 @@ log16 = [x for x in r16["c"].car_writer.log if x[0] == "current"] t_s = log16[0][2] if log16 else None fs = next((t for t, _a, s, _w in tr16 if s), None) al16 = P.ids(r16, "goodwe_peak_saturated") +N2.append(("T16", None if fs is None else fs - t_s)) +N1.append(("T22", all(a is None and not st for _t, a, st, _w in tr_ref))) check(f"T16 everything frozen, the write is refused: it arms at t_s={t_s} " f"and is stuck at t_s+60 +-2 (first {fs})", t_s is not None and fs is not None and 58 <= fs - t_s <= 62 @@ -963,6 +974,158 @@ check("T26 no write above the clamp (16 A), and the one select (T20) came " if any(x[1] == "phases" for x in v[1])) == ["T20-beside", "T20-inside"]) +# =========================================================================== # +print("\n7. A REFUSAL, THEN THE CHARGER GOES UNAVAILABLE") +# =========================================================================== # +GONE = CH.ChargerReading(available=False, why="no answer") + + +def gone_then_back(back_at, mode): + """Refusal at TS; no answer until `back_at`; then the frozen reading.""" + tl = [(TS + s, GONE if s < back_at else rd()) for s in range(1, 131)] + return play(tl, mode) + + +for mode in ("live", "ring"): + tr, first, _ = gone_then_back(21, mode) + check(f"unavailable [{mode}]: a refusal, no answer from TS+1 to TS+20 " + f"(past TOOK_S), then the reading back and frozen: armed dated the " + f"send, stuck at TS+60 as on the base (first {first})", + tr.armed_at == TS and first is not None and TS + 59 <= first + <= TS + 61 and pend(tr) is None) + tl = [(TS + k, GONE) for k in range(1, 31)] + tr, first, _ = play(tl, mode) + check(f"unavailable [{mode}]: a refusal and no answer at all for 30 s: " + "armed dated the send while it is still unavailable, as on the " + "base", tr.armed_at == TS and pend(tr) is None) + tr, first, _ = gone_then_back(75, mode) + check(f"unavailable [{mode}]: no answer until TS+75: the window lapsed " + f"unseen, so the first reading back is stuck at once, as on the base " + f"(first {first})", tr.armed_at == TS and first == TS + 75) + tr, first, _ = gone_then_back(5, mode) + check(f"unavailable [{mode}]: back inside the window and frozen: the " + "window runs on and arms at the send", tr.armed_at == TS + and first is not None and TS + 59 <= first <= TS + 61) + +# =========================================================================== # +print("\n8. N-IDS") +# =========================================================================== # +check(f"N1: no stuck after refusals that all showed life (T1-T5, T9 edges, " + f"T12a, T19, T22: {len(N1)} runs)", len(N1) >= 20 + and all(ok for _n, ok in N1)) +check(f"N2: every refusal with no life is stuck within 61 s of its send " + f"(T6-T11, T12b, T16, T20: {len(N2)} runs)", len(N2) >= 20 + and all(d is not None and d <= 61 for _n, d in N2)) +check("N5: no house-caused alarm later than the no-write twin or Off (T22 " + "equals its twin; T23 within a second of both)", + alarm_list(r_ref) == alarm_list(r_twin) + and abs(a23[0][0] - a23a[0][0]) <= 1 and abs(a23[0][0] - a23o[0][0]) <= 1) +from app import messages # noqa: E402 +check("N6: the constants and the row texts (EN and NL) are the base's", + (CH.STUCK_S, CH.STUCK_MIN_A, CH.OWN_WRITE_MUTE_S, CH.TOOK_S) == + (60.0, 1.0, 15.0, 10.0) + and (carmode.MAX_TRIES, carmode.RETRY_S) == (3, 60.0) + and messages.row_charger_stuck() == + "The charger reading may be stuck, so check the charger." + and messages.row_charger_stuck().nl == + "De meting van de laadpaal staat mogelijk vast, dus controleer de " + "laadpaal." + and messages.row_charger_refused() == + "The charger did not take the setting, so check the charger." + and messages.row_charger_refused().nl == + "De laadpaal nam de instelling niet over, dus controleer de laadpaal.") + +# =========================================================================== # +print("\n9. T27: THE EV-04 SECTION 9 CHECKS ARE THE BASE'S; T28: THE MUTANTS") +# =========================================================================== # +S9_SHA = "2a40480c804574207b0c81096365363c8d608eeb32285a1c18700478fc37fc41" +src04 = open(os.path.join(HERE, "test_ev04.py")).read() +a9 = src04.index('9. THE PEAK GUARD') +b9 = src04.index("# a plausible figure is bounded by what the read-back allows") +sec9 = src04[a9:b9] +check("T27 test_ev04 section 9 is byte-identical to 76f4de2", + hashlib.sha256(sec9.encode()).hexdigest() == S9_SHA) +check("T27 ...its two stuck checks are there", + "the amber row 'Charger reading may be stuck' says so" in sec9 + and "...while stuck, the car figure is not used by the alarm path" in sec9) +# the other files T27 names (ev04, ev10, ev10b, ev15, ev18, ev19, ev28, +# named_absence, bytecode) are test files of their own: CI runs every +# test_*.py, so a red one fails this PR; not re-run from here. + +MUTANTS = { + "M1": (" before = [x for x in self._ring if x[0] <= now]\n", + " return\n before = [x for x in self._ring if x[0] " + "<= now]\n"), + "M2": (" if old and all(v != o for o in old):", " if old:"), + "M3": (" t = now if r.taken_at is None else r.taken_at\n", + " t = now\n"), + "M4": (" self._pend_feed(t, v, t)\n\n def _life", + " self._pend_feed(t, v, t)\n self._pend_arm()\n\n" + " def _life"), + "M5": (' if max(t, arrival) > p["t"] + TOOK_S:\n' + ' self._pend_arm()', + ' if max(t, arrival) > p["t"] + TOOK_S:\n' + ' self._pend_arm(max(t, arrival))'), +} +M5_SIG = (" def _pend_arm(self) -> None:", + " def _pend_arm(self, at=None) -> None:") +M5_SET = (' self.armed_at, self.armed_sig = p["t"], p["sig"]', + ' self.armed_at, self.armed_sig = (p["t"] if at is None ' + 'else at), p["sig"]') +# what each mutant must turn red (substrings of the failing check names) +RED = {"M1": ["T6 ", "T7 ", "T11 ", "T16 "], "M2": ["T6 "], + "M3": ["T7 ", "T8 "], "M4": ["T2 "], "M5": ["T9 ", "T16 "]} + + +def mutant_run(name): + """A copy of the tree with the bug put back; test_ev13 (and, for the + stuck rows, test_ev04) run in it. The children skip this section.""" + import shutil + import subprocess + import tempfile + d = tempfile.mkdtemp(prefix="ev13m-") + dst = os.path.join(d, "gc") + shutil.copytree(HERE, dst, ignore=shutil.ignore_patterns( + "__pycache__", "*.pyc")) + p = os.path.join(dst, "app", "chargers.py") + txt = open(p).read() + edits = [MUTANTS[name]] + ([M5_SIG, M5_SET] if name == "M5" else []) + for old, new in edits: + assert txt.count(old) == 1, (name, old) + txt = txt.replace(old, new) + open(p, "w").write(txt) + env = dict(os.environ, EV13_MUTANT="1", PYTHONDONTWRITEBYTECODE="1") + procs = {f: subprocess.Popen( + [sys.executable, "-B", f], cwd=dst, env=env, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, text=True) + for f in ("test_ev13.py", "test_ev04.py")} + out = {f: pr.communicate()[0] for f, pr in procs.items()} + shutil.rmtree(d, ignore_errors=True) + return {f: [ln for ln in o.splitlines() if ln.strip().startswith("FAIL")] + for f, o in out.items()}, {f: procs[f].returncode for f in procs} + + +if not os.environ.get("EV13_MUTANT"): + import concurrent.futures + with concurrent.futures.ThreadPoolExecutor(5) as ex: + res = dict(zip(MUTANTS, ex.map(mutant_run, MUTANTS))) + for name in MUTANTS: + reds, rcs = res[name] + got = reds["test_ev13.py"] + check(f"T28 {name}: test_ev13 goes red ({len(got)} checks) in " + f"{RED[name]}", rcs["test_ev13.py"] != 0 + and all(any(k in ln for ln in got) for k in RED[name])) + check("T28 M1 also turns the two test_ev04 section 9 stuck checks red", + res["M1"][1]["test_ev04.py"] != 0 + and any("amber row 'Charger reading may be stuck'" in ln + for ln in res["M1"][0]["test_ev04.py"]) + and any("car figure is not used" in ln + for ln in res["M1"][0]["test_ev04.py"])) + check("T28 M2 turns test_ev04 section 9 red too; M3, M4 and M5 do not " + "(its 85 s bound is too loose to see M5's 10 s: see the report)", + res["M2"][1]["test_ev04.py"] != 0 + and all(res[m][1]["test_ev04.py"] == 0 for m in ("M3", "M4", "M5"))) + print() if fails: print(f"FAIL: {len(fails)} check(s)")