State Manager Agent
The State Manager Agent provides centralized state management for traffic entities across the platform.
π Overviewβ
| Property | Value |
|---|---|
| Module | src.agents.state_management.state_manager_agent |
| Class | StateManagerAgent |
| Author | UIP Team |
| Version | 1.0.0 |
π― Purposeβ
The State Manager Agent handles:
- Centralized state storage for all traffic entities
- State transitions with validation
- Event sourcing for state history
- State synchronization across services
π State Modelβ
Entity Statesβ
| Entity Type | Possible States |
|---|---|
| Camera | active, inactive, maintenance, offline |
| Observation | pending, validated, published, archived |
| Accident | detected, confirmed, responding, resolved |
| Congestion | forming, active, dissipating, cleared |
State Transitionsβ
Camera State Machine:
ββββββββββββββββββββββββββββ
βΌ β
ββββββββββ activate βββββββ΄βββββ
βinactiveββββββββββββββββΆβ active β
ββββββ¬ββββ ββββββ¬ββββββ
β β
β maintenance offlineβ
βΌ βΌ
ββββββββββββββ ββββββββββββ
βmaintenance β β offline β
ββββββββββββββ ββββββββββββ
π§ Architectureβ
βββββββββββββββββββββββββββββββββββββββββββββββ
β State Manager Agent β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββ βββββββββββ βββββββββββββββ β
β β State β β Event β β State β β
β β Store β β Source β β Validator β β
β ββββββ¬βββββ ββββββ¬βββββ ββββββββ¬βββββββ β
β β β β β
β ββββββββββββββΌβββββββββββββββ β
β βΌ β
β βββββββββββββββββ β
β β State Machine β β
β βββββββββ¬ββββββββ β
β β β
β βββββββββββββΌβββββββ βββββ β
β βΌ βΌ βΌ β
β βββββββββββ βββββββββββ βββββββββββ β
β β Redis β β MongoDB β β Event β β
β β Cache β β Store β β Bus β β
β βββββββββββ βββββββββββ βββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββ
π Usageβ
Get Stateβ
from src.agents.state_management.state_manager_agent import StateManagerAgent
state_manager = StateManagerAgent()
# Get current state
state = state_manager.get_state("camera:CAM_001")
print(f"Current state: {state['status']}")
Update Stateβ
# Update state with transition
state_manager.transition(
entity_id="camera:CAM_001",
from_state="inactive",
to_state="active",
metadata={"activated_by": "system"}
)
State Historyβ
# Get state history
history = state_manager.get_history(
entity_id="camera:CAM_001",
limit=10
)
for event in history:
print(f"{event['timestamp']}: {event['from_state']} -> {event['to_state']}")
βοΈ Configurationβ
# config/state_manager_config.yaml
state_manager:
enabled: true
# Storage backends
storage:
primary: redis
persistent: mongodb
# State definitions
entities:
camera:
states:
- active
- inactive
- maintenance
- offline
transitions:
inactive:
- active
- maintenance
active:
- inactive
- offline
maintenance:
- active
- inactive
offline:
- active
accident:
states:
- detected
- confirmed
- responding
- resolved
transitions:
detected:
- confirmed
- resolved
confirmed:
- responding
- resolved
responding:
- resolved
# Event sourcing
event_sourcing:
enabled: true
retention_days: 30
π State Eventsβ
Event Typesβ
| Event | Description |
|---|---|
state.created | New entity state created |
state.updated | State transition occurred |
state.deleted | Entity state removed |
Event Publishingβ
# Subscribe to state changes
state_manager.subscribe("camera:*", on_camera_state_change)
# Handler function
def on_camera_state_change(event):
print(f"Camera {event['entity_id']} changed to {event['new_state']}")
π Related Documentationβ
- Accident State Manager - Accident state handling
- Congestion State Manager - Congestion state handling
- Temporal State Tracker - Time-based state tracking
See the complete agents reference for all available agents.