|
|
|
@@ -70,6 +70,7 @@ TOOK_S = 10.0 # a write is "taken" when the read-back follows by t
|
|
|
|
|
TOOK_TOL_A = 0.5 # ... to within this
|
|
|
|
|
STUCK_S = 60.0 # EV-04: power and read-back unchanged after a write
|
|
|
|
|
STUCK_MIN_A = 1.0 # ... that moved the setpoint by at least this
|
|
|
|
|
LIFE_KEEP_S = 40.0 # EV-13: samples kept to date a refused write's life
|
|
|
|
|
OWN_WRITE_MUTE_S = 15.0 # the tracker's two-writer detector ignores our own
|
|
|
|
|
# Volts used to bound a car figure by what the read-back allows (a physical
|
|
|
|
|
# ceiling for the alarm path, never a setpoint).
|
|
|
|
@@ -120,6 +121,7 @@ REQUIRED = ("current", "readback", "state", "power")
|
|
|
|
|
_SETPOINT_RE = re.compile(r"^number\.(?P<x>.+)_" + SETPOINT_SUFFIX + "$")
|
|
|
|
|
|
|
|
|
|
_ROLE_ORDER = tuple(ROLES)
|
|
|
|
|
_LIFE_ROLES = ("power", "energy", "l1", "l2", "l3") # EV-13
|
|
|
|
|
#: power unit -> W. The probe and the reading both use this one table.
|
|
|
|
|
_W_PER = {"mW": 0.001, "W": 1.0, "kW": 1000.0, "MW": 1000000.0}
|
|
|
|
|
POWER_UNITS = tuple(_W_PER)
|
|
|
|
@@ -161,6 +163,9 @@ class ChargerReading:
|
|
|
|
|
error: str | None = None # None: no error (or not read)
|
|
|
|
|
display: str | None = None
|
|
|
|
|
why: str = "" # for the log only, "" when available
|
|
|
|
|
# EV-13: when the sample was TAKEN, on the tracker's clock. None: the
|
|
|
|
|
# caller's `now` (in the cycle that is the read's start, `now_mono`).
|
|
|
|
|
taken_at: float | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _state_obj(obj):
|
|
|
|
@@ -420,6 +425,9 @@ class AlfenHaDriver(ChargerDriver):
|
|
|
|
|
self._clock = time.monotonic
|
|
|
|
|
self.clamp_faults = [] # (asked, clamp), newest last, for the log
|
|
|
|
|
self.sent = [] # every number value actually sent
|
|
|
|
|
# EV-13: what the latest write saw while it waited for the read-back,
|
|
|
|
|
# [(time, ChargerReading)]: the cycle's reads do not run inside it.
|
|
|
|
|
self.polls = []
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_options(cls, opts: dict) -> "AlfenHaDriver":
|
|
|
|
@@ -498,6 +506,7 @@ class AlfenHaDriver(ChargerDriver):
|
|
|
|
|
read-back followed within `TOOK_S`. A value above `clamp_a()` is a
|
|
|
|
|
SOFTWARE FAULT: nothing is sent, it is logged (the clamp watchdog's
|
|
|
|
|
shape) and the answer is False."""
|
|
|
|
|
self.polls = []
|
|
|
|
|
try:
|
|
|
|
|
a = float(a)
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
@@ -522,7 +531,8 @@ class AlfenHaDriver(ChargerDriver):
|
|
|
|
|
"""Did the read-back follow the write, within `TOOK_S`?"""
|
|
|
|
|
t0 = self._clock()
|
|
|
|
|
while True:
|
|
|
|
|
st = await self._states(("readback", "current"))
|
|
|
|
|
st = await self._states(("readback", "current") + _LIFE_ROLES)
|
|
|
|
|
self._poll(st)
|
|
|
|
|
seen = _first(_amps(st["readback"]), _amps(st["current"]))
|
|
|
|
|
if seen is not None and abs(seen - a) <= TOOK_TOL_A:
|
|
|
|
|
return True
|
|
|
|
@@ -530,6 +540,18 @@ class AlfenHaDriver(ChargerDriver):
|
|
|
|
|
return False
|
|
|
|
|
await self._sleep(1.0)
|
|
|
|
|
|
|
|
|
|
def _poll(self, st: dict) -> None:
|
|
|
|
|
"""EV-13: keep what this wait saw of energy, power and phase currents
|
|
|
|
|
(never raises: a write does not depend on it)."""
|
|
|
|
|
try:
|
|
|
|
|
cur = tuple(a for a in (_amps(st.get(k)) for k in ("l1", "l2", "l3"))
|
|
|
|
|
if a is not None)
|
|
|
|
|
self.polls.append((self._clock(), ChargerReading(
|
|
|
|
|
available=True, power_w=_watts(st.get("power")),
|
|
|
|
|
energy_kwh=_kwh(st.get("energy")), current_a=cur)))
|
|
|
|
|
except Exception: # noqa: BLE001
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def write_phases(self, n: int) -> bool:
|
|
|
|
|
"""Select 1 or 3 phases. ONLY THROUGH `carmode`'s safe sequence: the
|
|
|
|
|
setting moves the contactors, so it is touched with the car paused."""
|
|
|
|
@@ -631,6 +653,26 @@ CHARGING, WAITING, NONE_, FULL, UNREACH, SETUP, FAULT = (
|
|
|
|
|
"charging", "waiting", "none", "full", "unreach", "setup", "fault")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _life_vals(r) -> tuple:
|
|
|
|
|
"""EV-13: what shows a charger is alive: energy, power, three phase
|
|
|
|
|
currents. None is "not read"."""
|
|
|
|
|
cur = tuple(r.current_a or ())[:3]
|
|
|
|
|
return (r.energy_kwh, r.power_w) + cur + (None,) * (3 - len(cur))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _alive(vals: tuple, pre: list) -> bool:
|
|
|
|
|
"""A value that is present now, was present before the write, and differs
|
|
|
|
|
from every value before it. Present but unchanged, or not there before, is
|
|
|
|
|
no life."""
|
|
|
|
|
for i, v in enumerate(vals):
|
|
|
|
|
if v is None:
|
|
|
|
|
continue
|
|
|
|
|
old = [p[i] for p in pre if p[i] is not None]
|
|
|
|
|
if old and all(v != o for o in old):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class CarTracker:
|
|
|
|
|
"""What the cycle learns about the car from the readings. Pure, clocked by
|
|
|
|
@@ -663,6 +705,9 @@ class CarTracker:
|
|
|
|
|
sig: tuple | None = None
|
|
|
|
|
stuck_now: bool = False
|
|
|
|
|
obs_at: float | None = None # monotonic second of the latest reading
|
|
|
|
|
# EV-13: a REFUSED write waits to see if the charger shows life (below)
|
|
|
|
|
_pend: dict | None = None
|
|
|
|
|
_ring: list = field(default_factory=list) # (taken-at, life values)
|
|
|
|
|
_hist: list = field(default_factory=list) # (t, power W, energy Wh)
|
|
|
|
|
# today's energy
|
|
|
|
|
day: str | None = None
|
|
|
|
@@ -683,7 +728,10 @@ 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)
|
|
|
|
|
self._writer(r, now_utc, now)
|
|
|
|
|
self._energy(r, day)
|
|
|
|
@@ -727,19 +775,78 @@ class CarTracker:
|
|
|
|
|
`OWN_WRITE_MUTE_S` from now, not from before the write."""
|
|
|
|
|
self.mute_until = now + OWN_WRITE_MUTE_S
|
|
|
|
|
|
|
|
|
|
def note_write(self, now: float, was_a, to_a) -> None:
|
|
|
|
|
"""The add-on wrote the current. Its own change is not "another
|
|
|
|
|
writer" for `OWN_WRITE_MUTE_S`, and a write that moved the setpoint by
|
|
|
|
|
`STUCK_MIN_A` or more arms the stuck check: power and read-back that
|
|
|
|
|
have not changed `STUCK_S` later are a stuck reading, not a car."""
|
|
|
|
|
def note_write(self, now: float, was_a, to_a, ok: bool = True,
|
|
|
|
|
polls=()) -> None:
|
|
|
|
|
"""The add-on wrote the current at `now`. Its own change is not
|
|
|
|
|
"another writer" for `OWN_WRITE_MUTE_S`, and a write that moved the
|
|
|
|
|
setpoint by `STUCK_MIN_A` or more arms the stuck check: power and
|
|
|
|
|
read-back that have not changed `STUCK_S` later are a stuck reading,
|
|
|
|
|
not a car.
|
|
|
|
|
|
|
|
|
|
EV-13: a write the charger REFUSED (`ok` False) arms it only when the
|
|
|
|
|
charger shows no life: the energy, a phase current or the power
|
|
|
|
|
changed in a sample taken after `now` and within `TOOK_S` of it,
|
|
|
|
|
against the last values before. A refused write on a charger that is
|
|
|
|
|
plainly alive is not a stuck reading. `polls` are the samples the
|
|
|
|
|
write itself saw while it waited, `[(time, ChargerReading)]`."""
|
|
|
|
|
self.mute_until = now + OWN_WRITE_MUTE_S
|
|
|
|
|
try:
|
|
|
|
|
moved = was_a is None or abs(float(to_a) - float(was_a)) \
|
|
|
|
|
>= STUCK_MIN_A
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
moved = False
|
|
|
|
|
if moved and self.armed_at is None:
|
|
|
|
|
if self._pend is not None and now > self._pend["t"] + TOOK_S:
|
|
|
|
|
self._pend_arm() # the old window ran out
|
|
|
|
|
if not moved or self.armed_at is not None:
|
|
|
|
|
return
|
|
|
|
|
if ok:
|
|
|
|
|
self._pend = None
|
|
|
|
|
self.armed_at, self.armed_sig = now, self.sig
|
|
|
|
|
return
|
|
|
|
|
if self._pend is not None:
|
|
|
|
|
return # an earlier refusal's window
|
|
|
|
|
before = [x for x in self._ring if x[0] <= now]
|
|
|
|
|
pre = [max(before, key=lambda x: x[0])[1]] if before else []
|
|
|
|
|
self._pend = {"t": now, "pre": pre, "sig": self.sig}
|
|
|
|
|
later = [(t, _life_vals(r)) for t, r in polls]
|
|
|
|
|
later += [(t, v) for t, v in self._ring if t > now]
|
|
|
|
|
for t, v in sorted(later, key=lambda x: x[0]):
|
|
|
|
|
self._pend_feed(t, v, t)
|
|
|
|
|
|
|
|
|
|
def _life(self, r, now: float) -> None:
|
|
|
|
|
"""EV-13: file this sample (by when it was TAKEN) and let a refused
|
|
|
|
|
write's open window look at it."""
|
|
|
|
|
t = now if r.taken_at is None else r.taken_at
|
|
|
|
|
v = _life_vals(r)
|
|
|
|
|
self._ring.append((t, v))
|
|
|
|
|
top = max(x[0] for x in self._ring)
|
|
|
|
|
self._ring = [x for x in self._ring if top - x[0] <= LIFE_KEEP_S]
|
|
|
|
|
self._pend_feed(t, v, now)
|
|
|
|
|
|
|
|
|
|
def _pend_feed(self, t: float, vals: tuple, arrival: float) -> None:
|
|
|
|
|
p = self._pend
|
|
|
|
|
if p is None:
|
|
|
|
|
return
|
|
|
|
|
if t <= p["t"]:
|
|
|
|
|
p["pre"] = (p["pre"] + [vals])[-8:] # taken before the send
|
|
|
|
|
elif t <= p["t"] + TOOK_S and _alive(vals, p["pre"]):
|
|
|
|
|
self._pend = None # life: nothing to arm
|
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
if p is not None and self.armed_at is None:
|
|
|
|
|
self.armed_at, self.armed_sig = p["t"], p["sig"]
|
|
|
|
|
|
|
|
|
|
def _sig(self, r, now: float) -> None:
|
|
|
|
|
sig = (r.power_w, r.allowed_a)
|
|
|
|
|