diff --git a/goodwe_controller/app/ctl_car.py b/goodwe_controller/app/ctl_car.py index 391fae2..eeb660c 100644 --- a/goodwe_controller/app/ctl_car.py +++ b/goodwe_controller/app/ctl_car.py @@ -61,8 +61,8 @@ class CarMixin: self.car_over = False # ... held: the card and row say so self._car_over_since = None self._car_over_gone = None - self._car_why_hold = None # EV-16: the held Car Why (display) - self._car_over_ep = 0 # EV-16: bumped by control at each start/end of car_over; the hold is valid for one value + self._car_why_rec = None # EV-16: [shown over, since, episode] + self._car_over_ep = 0 # EV-16: bumped when car_over ends self._car_batt_prev = self._car_batt_prev2 = None self._car_hx_prev = None # EV-19: last cycle's (held, car W) self._car_need_real = self._car_need_excl = False # EV-10 latches @@ -394,16 +394,40 @@ class CarMixin: if self._car_over_since is None: self._car_over_since = mono if mono - self._car_over_since >= OVER_HOLD_S: - if not self.car_over: - self._car_over_ep += 1 # EV-16: a new episode self.car_over = True else: if self._car_over_gone is None: self._car_over_gone = mono if mono - self._car_over_gone >= OVER_OFF_HOLD_S: self.car_over, self._car_over_since = False, None - self._car_over_ep += 1 # EV-16: ends the held Why + self._car_over_ep += 1 # EV-16: an episode ends self._car_over_gone = None + self._car_why_tick(guard, mono) + + def _car_why_tick(self, guard, mono) -> None: + """EV-16, display only: control writes `_car_why_rec`, never reads it. + The Car Why text ("are at" / "give all they can") is held: it changes + only when the other state has stood for `OVER_OFF_HOLD_S` of THIS + cycle's clock (so reads of /status do not matter). State: floor-limited + with the SoC read; a guard of None, or unread SoC, never counts as + floor. Cleared when car_over is off or the cover is off.""" + if not (self.car_over and self.car_cover): + self._car_why_rec = None + return + cand = None if guard is None else bool( + getattr(guard, "floor_limited", False) + and getattr(guard, "soc", None) is not None) + ep = self._car_over_ep + r = self._car_why_rec + if r is None or r[2] != ep: + self._car_why_rec = [bool(cand), None, ep] + elif cand is None or cand == r[0]: + r[1] = None + else: + if r[1] is None: + r[1] = mono + if mono - r[1] >= OVER_OFF_HOLD_S: + self._car_why_rec = [cand, None, ep] def _car_explains_alarm(self, real_proj, guard, target_w, w) -> bool: """EV-10: is the alarm's own test true with the car and false without @@ -502,7 +526,7 @@ class CarMixin: # The session is over: that is a real end, with no hold. self.car_over = False self._car_over_since = self._car_over_gone = None - self._car_over_ep += 1 # EV-16: ends the held Why + self._car_over_ep += 1 # EV-16: an episode ends if s not in (chargers.NONE_, None) and self._car_prev_state in ( chargers.NONE_, None): self._car_session += 1 diff --git a/goodwe_controller/app/homeview.py b/goodwe_controller/app/homeview.py index 8c317c0..4774469 100644 --- a/goodwe_controller/app/homeview.py +++ b/goodwe_controller/app/homeview.py @@ -1302,34 +1302,20 @@ def _car_why(key, c, good): # EV-16: the Why of "car over the limit" follows the guard's floor flag, which -# toggles cycle by cycle at the reserve. The text is held: it changes only when -# the other state has stood for as long as the car_over row's own hold. +# toggles cycle by cycle at the reserve. The text is held, on the control +# cycle's clock (`ctl_car.CarMixin._car_why_tick`, display only: control writes +# `_car_why_rec`, never reads it). The Why only reads it. WHY_HOLD_S = 180.0 # = ctl_car.OVER_OFF_HOLD_S (tested) def _over_why_is_floor(c, pg) -> bool: - """True: "are at {pct}"; False: "give all they can". Display only: the - state lives in `c._car_why_hold`, stamped with the episode `c._car_over_ep` - that control bumps; a hold of another episode is never used, so a read - overlapping the end of car_over cannot bring a stale text back.""" - mono = c.car_now() - cand = (pg.floor_limited and pg.soc is not None) if pg is not None \ - else None - ep = getattr(c, "_car_over_ep", 0) # owned by the control loop - h = getattr(c, "_car_why_hold", None) - if h is not None and h[2] != ep: # a hold of an ended episode - h = None - if h is None: - h = [bool(cand), None, ep] # shown, since when, episode - elif cand is None or cand == h[0]: - h[1] = None - else: - if h[1] is None: - h[1] = mono - if mono - h[1] >= WHY_HOLD_S: - h = [cand, None, ep] - c._car_why_hold = h - return h[0] + """True: "are at {pct}"; False: "give all they can". With no valid held + record (not yet ticked, or of an ended episode) the live state, written + nowhere.""" + rec = getattr(c, "_car_why_rec", None) + if rec is not None and rec[2] == getattr(c, "_car_over_ep", 0): + return rec[0] + return bool(pg is not None and pg.floor_limited and pg.soc is not None) def _car_manual_why(c, key): @@ -1342,11 +1328,9 @@ def _car_manual_why(c, key): if c.car_over: pg = getattr(c, "peak_guard", None) if not c.car_cover: - c._car_why_hold = None return _t("car.why.manual_nocover", a=a) return _t("car.why.manual_over" if _over_why_is_floor(c, pg) else "car.why.manual_full", a=a, pct=pct) - c._car_why_hold = None if c.shadow: t, n = w.would if w.would is not None else (c.car_a, c.car_n) return _t("car.why.manual_would", a=f"{float(t):.0f}", diff --git a/goodwe_controller/test_ev16.py b/goodwe_controller/test_ev16.py index 22ed817..dab89da 100644 --- a/goodwe_controller/test_ev16.py +++ b/goodwe_controller/test_ev16.py @@ -2,8 +2,9 @@ they can" while the car-over row holds and the guard's floor flag toggles. `python3 -B test_ev16.py` -Display only: the text is held in `c._car_why_hold`; control never reads it. Control owns -`c._car_over_ep` (bumped at each start and end of car_over); a hold of +Display only: the text is held by `ctl_car._car_why_tick` on the control +cycle's clock (control writes `_car_why_rec`, never reads it; /status reads do +not move it). `c._car_over_ep` is bumped when car_over ends; a record of another episode is never used. Mutant: pick the text from the live `floor_limited` again (T1, T2, T7, T8, T9 go red). @@ -94,13 +95,19 @@ def kind(t): return "over" if OVER in t else "full" if FULL in t else t +def tick(c, g, t): + """One control cycle's display tick (what car_cycle ends with).""" + c._now = t + c.peak_guard = g + c._car_why_tick(g, t) + + def run(c, secs, g_of, t0=0.0): """Read the Why once per 5 s cycle; returns [(t, kind)].""" out = [] t = t0 while t < t0 + secs: - c._now = t - c.peak_guard = g_of(t - t0) + tick(c, g_of(t - t0), t) out.append((t, kind(why(c)))) t += STEP return out @@ -212,9 +219,8 @@ check("T8 SoC toggling None / 20 % every 10 s, starting unread: one text, " c = S() seq = [] for t in range(0, 900, 5): - c._now = float(t) - c.peak_guard = None if 400 <= t < 410 else G(floor_limited=(t // 10) % 2 - == 0) + tick(c, None if 400 <= t < 410 else G(floor_limited=(t // 10) % 2 + == 0), float(t)) try: seq.append((t, kind(why(c)))) except Exception as e: # noqa: BLE001 @@ -222,8 +228,7 @@ for t in range(0, 900, 5): check("T9 guard None for a cycle: no Traceback, one text through it", not any("TRACE" in k for _, k in seq) and len({k for _, k in seq}) == 1) c = S() -c.peak_guard = None -c._now = 0.0 +tick(c, None, 0.0) check("T9 (cont.) guard None from the very first read: a text, no Traceback", kind(why(c)) in ("full", "over")) @@ -239,8 +244,7 @@ check("T10 car_over dropped after the stop", c.car_over is False) check("T10 the Why left the over wording", OVER not in why(c) and FULL not in why(c)) c.car_over = True # a new episode, later, SoC 60 % -c._now = 5000.0 -c.peak_guard = G(floor_limited=False, soc=60.0) +tick(c, G(floor_limited=False, soc=60.0), 5000.0) check("T10 the new episode shows 'give all they can' from its first cycle", kind(why(c)) == "full") @@ -253,8 +257,8 @@ for k in range(0, 200): c.w = None c.car_cycle(Proj(), G(), 14000.0, None) c.car_over = True -c._now = 9000.0 c.peak_guard = G(floor_limited=False, soc=60.0) +c._now = 9000.0 check("T10 no /status between the drop and the new episode: still 'full'", kind(why(c)) == "full") @@ -272,8 +276,7 @@ check("T11 after the unplug cycle there is no over wording", and "set when a car is connected" in homeview._car_manual_why(c, "none")) c.state = "charging" c.car_over = True -c.peak_guard = G(floor_limited=False, soc=60.0) -c._now = 400.0 +tick(c, G(floor_limited=False, soc=60.0), 400.0) check("T11 a replug starts with no held text", kind(why(c)) == "full") # the hold is also gone when the unplug itself ends car_over (no read between) c = S() @@ -290,12 +293,11 @@ check("T11 (cont.) the unplug ends the episode (control bumps it)", c = S() run(c, 100.0, lambda t: G()) c.o["ev_batt_cover"] = False -c._now = 105.0 +tick(c, G(), 105.0) check("T12 cover off: 'ignores the car' at the next read", "ignores the car" in why(c)) c.o["ev_batt_cover"] = True -c.peak_guard = G(floor_limited=False, soc=60.0) -c._now = 110.0 +tick(c, G(floor_limited=False, soc=60.0), 110.0) check("T12 cover on again, steady not limited: 'give all they can', not " "the old 'are at'", kind(why(c)) == "full") @@ -350,7 +352,7 @@ for lang, over, full in ( check(f"T17 {lang}.json wording of manual_over / manual_full unchanged", d["manual_over"] == over and d["manual_full"] == full) check("a held peak_guard row is not touched: the module still has no hold " - "in ctl_status", "_car_why_hold" not in open(os.path.join( + "in ctl_status", "_car_why_rec" not in open(os.path.join( HERE, "app", "ctl_status.py")).read()) print("\n6. EV-16 review round 1: the race, T16, T18, the N-ids") @@ -358,27 +360,26 @@ print("\n6. EV-16 review round 1: the race, T16, T18, the N-ids") # its old hold back; the next episode must not show it c = S() run(c, 100.0, lambda t: G()) -stale = c._car_why_hold +stale = c._car_why_rec check("race: the read holds 'are at' before the end", kind(why(c)) == "over") for k in range(0, 200): c._now = 100.0 + k c.w = None c.car_cycle(Proj(), G(), 14000.0, None) -c._car_why_hold = stale # the late write-back +c._car_why_rec = stale # the late write-back c.car_over = True -c._car_over_ep += 1 # what control does on a start c._now = 6000.0 c.peak_guard = G(floor_limited=False, soc=60.0) check("N4 race: a read interleaved with the clear cannot bring the old " "'are at' into the next episode", kind(why(c)) == "full") c = S() run(c, 100.0, lambda t: G()) -stale = c._car_why_hold +stale = c._car_why_rec c._car_prev_state = "charging" c.state = CH.NONE_ c._now = 105.0 c.car_cycle(Proj(), G(), 14000.0, None) -c._car_why_hold = stale # write-back after an unplug +c._car_why_rec = stale # write-back after an unplug c.state = "charging" c.car_over = True c.peak_guard = G(floor_limited=False, soc=60.0) @@ -449,6 +450,76 @@ check("N7 wording unchanged: the four texts are in the files verbatim", "piek." in nl and "De batterijen geven alles, dus deze lading bepaalt " "de piek." in nl) +print("\n7. EV-16 review round 2: the hold runs on the control clock") + + +def gap_run(c, g_of, read_at, end=2300.0): + """Control cycles every 5 s; the Why is read only at the `read_at` times.""" + out = [] + t = 0.0 + while t < end: + tick(c, g_of(t), t) + if any(a <= t < b for a, b in read_at): + out.append((t, kind(why(c)))) + t += STEP + return out + + +pages = ((0, 60), (2000, 2300)) +c = S() +r = gap_run(c, lambda t: G(floor_limited=t >= 300.0, soc=20.0), pages) +late = [(t, k) for t, k in r if t >= 2000] +check("review A: drained to the reserve while the page was closed: the " + "first read at t=2000 already shows 'are at'", + len(late) > 0 and late[0][1] == "over" and {k for _, k in late} == {"over"}) +c = S() +r = gap_run(c, lambda t: G(floor_limited=t < 300.0, + soc=20.0 if t < 300.0 else 45.0), pages) +late = [(t, k) for t, k in r if t >= 2000] +check("review B: off the reserve while the page was closed: the first " + "read at t=2000 shows 'give all they can', never a false 'are at'", + len(late) > 0 and late[0][1] == "full" and {k for _, k in late} == {"full"}) +c = S() +r = [] +for t in range(0, 400, 5): + cov = not (100 <= t < 200) + c.o["ev_batt_cover"] = cov + tick(c, G(floor_limited=t < 100, soc=20.0 if t < 100 else 45.0), + float(t)) + if t in (95, 205): + r.append(kind(why(c))) +check("review: cover off and on with no read between: back on, the state " + "now not limited shows 'give all they can' at the first read", + r == ["over", "full"]) +# lower bound of the hold: a real change is NOT shown before the hold ends +c = S() +r = [] +for t in range(0, 600, 5): + tick(c, G(floor_limited=t >= 300.0, soc=20.0), float(t)) + r.append((t, kind(why(c)))) +check("review: a real change is held for the full 180 s: still the old " + "text 170 s after it, the new one by 185 s", + dict(r)[470] == "full" and dict(r)[485] == "over") +# M7: guard None for 180 s or more under car_over +for steady, want in ((True, "over"), (False, "full")): + c = S() + seq = [] + for t in range(0, 1200, 5): + g = G(floor_limited=steady, soc=20.0 if steady else 45.0) \ + if t < 100 else None + tick(c, g, float(t)) + seq.append(kind(why(c))) + check(f"T9 (long) guard None for 1100 s after a steady " + f"{'limited' if steady else 'not limited'} state: the text stays " + f"'{want}', no flip", set(seq) == {want}) +c = S() +seq = [] +for t in range(0, 1200, 5): + tick(c, None, float(t)) + seq.append(kind(why(c))) +check("T9 (long) guard None from the start for 1200 s: one text, 'full'", + set(seq) == {"full"}) + print() print("FAIL: " + ", ".join(fails) if fails else "PASS: every check passed") sys.exit(1 if fails else 0)