Chuyển tới nội dung chính

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.