| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- # -*- coding: utf-8 -*-
- """
- Standalone-Anwendung für Trixy.
- Kombiniert Server- und Client-Funktionalität für den
- Betrieb ohne separate Server-Instanz.
- """
- from pathlib import Path
- from trixy_core.application import IApplication
- from trixy_core.config.datasets.standalone import StandaloneConfig
- from trixy_core.plugins.plugin_manager import PluginManager
- from trixy_core.events.event_data.basic import SystemShutdown
- from trixy_core.audio.config import init_audio_settings_from_config
- from trixy_core.input.keyboard_input import create_keyboard_input, KeyboardInputService, SimpleKeyboardInput
- from trixy_core.nlp.intent_dispatcher import IntentDispatcherService
- from trixy_core.utils.debug import pinfo, pdebug, perror
- class StandaloneApplication(IApplication):
- """
- Standalone-Anwendung für Trixy.
- Bietet alle Funktionen von Server und Client in
- einer einzelnen Anwendung.
- """
- def __init__(
- self,
- config_path: str | Path = "config/standalone_config.json",
- debug: bool = False,
- keyboard_input: bool = False,
- ) -> None:
- """
- Initialisiert die Standalone-Anwendung.
- Args:
- config_path: Pfad zur Konfigurationsdatei
- debug: Debug-Modus aktivieren
- keyboard_input: Keyboard-Eingabe aktivieren
- """
- super().__init__(debug)
- self._config_path = Path(config_path)
- self._standalone_config: StandaloneConfig | None = None
- self._plugin_manager: PluginManager | None = None
- # Im Debug-Modus immer Keyboard-Shortcuts aktivieren (Ctrl+P/N/S)
- self._keyboard_input_enabled = keyboard_input or debug
- self._keyboard_service: KeyboardInputService | SimpleKeyboardInput | None = None
- @property
- def standalone_config(self) -> StandaloneConfig:
- """Standalone-Konfiguration."""
- if self._standalone_config is None:
- raise RuntimeError("Standalone nicht initialisiert")
- return self._standalone_config
- @property
- def plugins(self) -> PluginManager:
- """Plugin-Manager."""
- if self._plugin_manager is None:
- raise RuntimeError("Standalone nicht initialisiert")
- return self._plugin_manager
- @property
- def room(self) -> str:
- """Raum-Kennung."""
- if self._standalone_config:
- return self._standalone_config.room
- return "standalone"
- @property
- def alias(self) -> str:
- """Anzeigename."""
- if self._standalone_config:
- return self._standalone_config.alias
- return "Trixy"
- async def initialize(self) -> None:
- """Initialisiert die Standalone-Anwendung."""
- pinfo("Initialisiere Standalone...")
- # Konfiguration laden
- self._standalone_config = self.config_manager.load(
- self._config_path,
- StandaloneConfig,
- name="standalone",
- auto_reload=True
- )
- # Audio-Einstellungen initialisieren (für Plugin-Zugriff)
- audio_settings = init_audio_settings_from_config(self._standalone_config)
- pdebug(f"Audio-Einstellungen: TTS={audio_settings.tts_sample_rate}Hz, Musik={audio_settings.music_sample_rate}Hz")
- # Plugin-Manager initialisieren
- self._plugin_manager = PluginManager(
- self,
- directory=self._standalone_config.plugins.plugin_directory
- )
- # IntentDispatcher-Service registrieren (verarbeitet intent_received Events)
- self._intent_dispatcher = IntentDispatcherService(self)
- self.services.register_instance(self._intent_dispatcher)
- self.events.register_object(self._intent_dispatcher)
- pdebug("Standalone initialisiert")
- async def start(self) -> None:
- """Startet die Standalone-Anwendung."""
- pinfo(f"Starte Standalone als '{self.alias}'...")
- # Services starten
- await self.services.start_all()
- # Plugins laden
- if self._standalone_config and self._standalone_config.plugins.enabled:
- await self._plugin_manager.load_all()
- # Keyboard-Input starten (falls aktiviert)
- # Im Standalone-Modus gibt es keine Server-Verbindung,
- # daher wird speech_recognized Event direkt emittiert
- if self._keyboard_input_enabled:
- self._keyboard_service = create_keyboard_input(
- self,
- connection=None, # Standalone hat keine Server-Verbindung
- prompt=f"{self.alias}> ",
- )
- await self._keyboard_service.start()
- pinfo("Standalone gestartet")
- async def stop(self) -> None:
- """Stoppt die Standalone-Anwendung."""
- pinfo("Stoppe Standalone...")
- # Keyboard-Input stoppen
- if self._keyboard_service:
- await self._keyboard_service.stop()
- self._keyboard_service = None
- # Shutdown-Event auslösen
- await self.events.trigger(
- "system_shutdown",
- SystemShutdown(reason="Standalone shutdown", source="standalone")
- )
- # Plugins entladen
- if self._plugin_manager:
- await self._plugin_manager.unload_all()
- # Services stoppen
- await self.services.stop_all()
- # ConfigManager aufräumen
- self.config_manager.stop_watching()
- pinfo("Standalone gestoppt")
|