feat(events): deliver state changes to the configurable operator event center

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:27 +03:00
parent d4ed96f3cd
commit 1b67d5f7df
35 changed files with 2058 additions and 8 deletions
+40 -1
View File
@@ -25,6 +25,11 @@ from .recovery import listener_addresses
from .sensor_transient import cache as sensor_transient
from .trust import SCHEMA, CoreTrust, PairingError, node_request, parse_invitation, pem, public_id
from .board_event_transitions import BoardPresence, LINK_TIMEOUT, publish_control
from .node_operator_events import accept_operator_events
from .uplink_notifications import publish_uplink_applied
class FleetRegistry:
"""One writer lock and durable transactions; transport never owns vehicles."""
@@ -57,6 +62,10 @@ class FleetRegistry:
self.rover_telemetry_preferences = RoverTelemetryPreferences(root)
from .uplinks import Uplinks
self.uplinks = Uplinks(guard=self.rover_control.network_change)
from k1link.notifications import NotificationHistory
self.notifications = NotificationHistory(root.parent / 'notifications')
self.board_presence = BoardPresence(self.notifications)
self.power.notifications = self.notifications
from .rover_channel import RoverChannel
self.rover_channel = RoverChannel(self.rover_control)
from .rover_configuration import RoverConfigurations
@@ -118,7 +127,10 @@ class FleetRegistry:
# Publish only after commit. Inventory fsync never holds the control
# channel lock; a failed transaction cannot change its authority.
self.rover_channel.publish(row)
if previous and previous["binding"]["binding_id"] != row["binding"]["binding_id"]:
self.board_presence.links.pop(previous["binding"]["binding_id"], None)
if row["enrollment"] == "revoked":
self.board_presence.links.pop(row["binding"]["binding_id"], None)
self.network_profiles.revoke_node(row["node_id"])
self.events.notify()
@@ -301,7 +313,7 @@ class FleetRegistry:
row["enrollment"] == "paired"
and last_seen is not None
and last_seen >= self.started_at
and time.time() - last_seen < 20
and time.time() - last_seen < LINK_TIMEOUT
)
return {
"id": row["id"],
@@ -438,6 +450,11 @@ class FleetRegistry:
self.routers.prune()
self.uplinks.prune()
active = [row for row in self.rows() if row["enrollment"] in ("pending", "paired")]
for row in active:
if row["enrollment"] == "paired":
with suppress(OSError, sqlite3.Error):
self.board_presence.expire(row)
publish_control(self.notifications, row, self.rover_control)
for address in {address for row in active for address in listener_addresses(row)}:
try:
self.listen(address)
@@ -524,6 +541,11 @@ class FleetRegistry:
configuration_response = self.rover_configurations.heartbeat(node_id, value)
network_response = self.network_profiles.heartbeat(row, value)
uplink_response = self.uplinks.heartbeat(row, value)
try:
self.notifications.uplink(row, self.uplinks.events(row))
publish_uplink_applied(self.notifications, row, self.uplinks)
except (OSError, sqlite3.Error):
logging.getLogger(__name__).warning('Notification history unavailable')
archive_ack = None
archive_started = time.monotonic()
try:
@@ -539,6 +561,12 @@ class FleetRegistry:
archive_elapsed = time.monotonic() - archive_started
enrollment_response = self.device_enrollment.heartbeat(row, value)
router_response = self.routers.heartbeat(row, value)
try:
from .router_notifications import publish_router_results, publish_router_incidents
publish_router_results(self.notifications, row, self.routers.pending)
publish_router_incidents(self.notifications, row, self.routers.latest.get(node_id))
except (OSError, sqlite3.Error):
logging.getLogger(__name__).warning('Router notification history unavailable')
host = value.get("host")
if (
not isinstance(host, dict)
@@ -569,13 +597,22 @@ class FleetRegistry:
if archive_elapsed > .025 or save_elapsed > .025:
logging.getLogger(__name__).warning(
"fleet heartbeat timing archive_ms=%.1f save_ms=%.1f", archive_elapsed*1000, save_elapsed*1000)
with suppress(OSError, sqlite3.Error):
self.board_presence.received(row)
monitor_ack = None
try:
monitor_ack = self.monitor.submit(node_id, value.get("monitor"))
except (ValueError, TypeError, KeyError, sqlite3.Error):
# Telemetry persistence failure must not block device control.
pass
event_ack = {}
if value.get('operator_events_v1'):
try:
event_ack = accept_operator_events(self.notifications, row, value['operator_events_v1'])
except (OSError, sqlite3.Error):
logging.getLogger(__name__).warning('Operator events awaiting storage')
return 200, {
**event_ack,
"vesc_configurations_ack": archive_ack,
"monitor_ack": monitor_ack,
"ok": True,
@@ -611,4 +648,6 @@ class FleetRegistry:
"at": now, "binding_id": row["binding"]["binding_id"],
"agent_instance_id": binding.agent_instance_id,
}
with suppress(OSError, sqlite3.Error):
self.board_presence.received(row)
return 200, {"ok": True, "heartbeat_encoding": "gzip"}