90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
import time
|
|
import threading
|
|
from enum import Enum
|
|
from pydantic import BaseModel, Field
|
|
from typing import Optional, List
|
|
|
|
class AnnouncementStates(Enum):
|
|
IDLE: str = "Ready"
|
|
TRANSLATING: str = "Translating"
|
|
GENERATING_VOICE: str = "Generating voice synthesis"
|
|
ROUTING: str = "Routing to endpoints"
|
|
ACTIVE: str = "Broadcasting announcement"
|
|
COMPLETE: str = "Complete"
|
|
ERROR: str = "Error"
|
|
|
|
class AnnouncementDetails(BaseModel):
|
|
text: str
|
|
languages: List[str]
|
|
group: str
|
|
endpoints: List[str]
|
|
start_time: float
|
|
|
|
class AnnouncementProgress(BaseModel):
|
|
current_state: str = AnnouncementStates.IDLE
|
|
error: Optional[str] = None
|
|
details: AnnouncementDetails
|
|
progress: float = Field(default=0.0, ge=0.0, le=1.0)
|
|
description: str = Field(default="Ready")
|
|
|
|
class AnnouncementSystem:
|
|
def __init__(self):
|
|
self.current_process = AnnouncementProgress(
|
|
details=AnnouncementDetails(
|
|
text="",
|
|
languages=[],
|
|
endpoints=[],
|
|
group="",
|
|
start_time=0.0
|
|
)
|
|
)
|
|
self._thread = None
|
|
|
|
|
|
def start_announcement(self, text, group):
|
|
if self.current_process.current_state not in [AnnouncementStates.IDLE, AnnouncementStates.COMPLETE]:
|
|
raise Exception("Announcement already in progress")
|
|
|
|
self.current_process.details.text = text
|
|
self.current_process.details.group = group
|
|
self._thread = threading.Thread(target=self._run_process)
|
|
self._thread.start()
|
|
|
|
def _run_process(self):
|
|
self.current_process.details.start_time = time.time()
|
|
try:
|
|
self._update_state(AnnouncementStates.TRANSLATING)
|
|
time.sleep(2) # Simulate translation
|
|
|
|
self._update_state(AnnouncementStates.GENERATING_VOICE)
|
|
time.sleep(1.5) # Voice synthesis
|
|
|
|
self._update_state(AnnouncementStates.ROUTING)
|
|
time.sleep(len(self.current_process.details.endpoints) * 0.5)
|
|
|
|
self._update_state(AnnouncementStates.ACTIVE)
|
|
time.sleep(3) # Simulate broadcast
|
|
|
|
self._update_state(AnnouncementStates.COMPLETE)
|
|
|
|
except Exception as e:
|
|
self.current_process.error = str(e)
|
|
self._update_state(AnnouncementStates.ERROR)
|
|
|
|
def _update_state(self, new_state):
|
|
self.current_process.current_state = new_state
|
|
|
|
# Progress based on state transitions
|
|
state_progress = {
|
|
AnnouncementStates.TRANSLATING: 0.25,
|
|
AnnouncementStates.GENERATING_VOICE: 0.5,
|
|
AnnouncementStates.ROUTING: 0.75,
|
|
AnnouncementStates.ACTIVE: 1.0,
|
|
AnnouncementStates.COMPLETE: 1.0,
|
|
AnnouncementStates.ERROR: 1.0
|
|
}
|
|
self.current_process.progress = state_progress[new_state]
|
|
|
|
# Singleton instance
|
|
announcement_system = AnnouncementSystem()
|