"""Read-only evidence of settled release, tied to the stopped attachments. GET_VALUES current averages span reads. A failed immediate cleanup read is not a permanent description of the controller. Require a new whole-profile neutral window; never extend a command lease or transfer motor authority here. """ import math import re # The same electrical standstill envelope is used by release observation and # configuration writes. GET_VALUES may report sub-amp idle sensor offset. IDLE_BOUNDS = ( ("motor_current_a", 1), ("input_current_a", 1), ("duty", 0.001), ("erpm", 1), ("fault_code", 0), ) NOTICE = "Снятие тока не подтверждено. Проверьте фактическое состояние моторов." def settlement_pending(evidence): """A successful release with finite readback may still be settling. This classifies a normal input-ended cleanup only. It does not confirm release, grant authority, or excuse transport/configuration/device faults. """ devices = (evidence or {}).get("devices", {}) return bool(devices) and all( item.get("send", {}).get("sent") is True and item.get("reason") in ("neutral_readback", "non_neutral_readback") and all(type(item.get("values", {}).get(k)) in (int, float) and math.isfinite(item["values"][k]) for k in ("motor_current_a", "input_current_a", "duty", "erpm", "fault_code")) and item["values"]["fault_code"] == 0 for item in devices.values() ) class ReleaseObservation: def __init__(self, devices, started_at): self.attachments = {d.id: getattr(d, "session", None) for d in devices} self.identities = {d.id: (getattr(d, "identity", None) or {}).get("uuid") for d in devices} self.started_at = started_at self.reset() def reset(self): self.since = None self.previous = {} self.count = 0 def faulted(self, devices, now): """Retain a real firmware fault seen while waiting for settlement.""" for d in devices: try: s = d.sample code = s["values"]["fault_code"] if (d.session == self.attachments.get(d.id) == s["session_id"] and d.link and getattr(d.link, "alive", True) and self.started_at <= s["sampled_at"] <= now and now - s["sampled_at"] <= .5 and math.isfinite(code) and code != 0): return True except (KeyError, TypeError, ValueError, AttributeError): continue return False def check(self, devices, now, receiver_active): stamps = {} try: if ( not self.attachments or len(devices) != len(self.attachments) or {d.id for d in devices} != set(self.attachments) ): raise ValueError("Incomplete release observation") # A new attachment can prove CURRENT neutral state only after its # physical UUID was identified again. It never confirms old output. for d in devices: expected = self.identities[d.id] if expected and (getattr(d, "identity", None) or {}).get("uuid") != expected: raise ValueError("Controller identity changed") if any(d.session != self.attachments[d.id] for d in devices): if not all(re.fullmatch(r"[0-9a-f]{24}", self.identities[d.id] or "") and getattr(d, "identified_session", None) == d.session and d.link and getattr(d.link, "alive", True) for d in devices): raise ValueError("New attachment identity is not verified") self.attachments = {d.id: d.session for d in devices} self.started_at = now self.reset() return False for d in devices: sample = d.sample if ( not self.attachments[d.id] or d.session != self.attachments[d.id] or not d.link or not getattr(d.link, "alive", True) or not sample or sample["session_id"] != d.session ): raise ValueError("Attachment changed") at = sample["sampled_at"] if ( not math.isfinite(at) or at < self.started_at or not 0 <= now - at <= 0.5 or at <= self.previous.get(d.id, -math.inf) ): raise ValueError("No new release sample") value = sample["values"] if any( not math.isfinite(value[k]) or abs(value[k]) > bound for k, bound in IDLE_BOUNDS ): raise ValueError("Controller is not idle") if receiver_active(d.id, sample["receiver"]["level"]): raise ValueError("Receiver requests output") stamps[d.id] = at # Gaps restart the proof; old samples cannot bridge missing data. if self.previous and any(stamps[k] - self.previous[k] > 0.5 for k in stamps): self.reset() except (KeyError, TypeError, ValueError, AttributeError): self.reset() return False if self.since is None: self.since = max(stamps.values()) self.previous = stamps self.count += 1 return self.count >= 3 and min(stamps.values()) - self.since >= 0.5