Skip to main content

State Manager Agent

The State Manager Agent provides centralized state management for traffic entities across the platform.

πŸ“‹ Overview​

PropertyValue
Modulesrc.agents.state_management.state_manager_agent
ClassStateManagerAgent
AuthorUIP Team
Version1.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 TypePossible States
Cameraactive, inactive, maintenance, offline
Observationpending, validated, published, archived
Accidentdetected, confirmed, responding, resolved
Congestionforming, 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​

EventDescription
state.createdNew entity state created
state.updatedState transition occurred
state.deletedEntity 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']}")

See the complete agents reference for all available agents.