standalone.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. Standalone-Anwendung für Trixy.
  4. Kombiniert Server- und Client-Funktionalität für den
  5. Betrieb ohne separate Server-Instanz.
  6. """
  7. from pathlib import Path
  8. from trixy_core.application import IApplication
  9. from trixy_core.config.datasets.standalone import StandaloneConfig
  10. from trixy_core.plugins.plugin_manager import PluginManager
  11. from trixy_core.events.event_data.basic import SystemShutdown
  12. from trixy_core.audio.config import init_audio_settings_from_config
  13. from trixy_core.input.keyboard_input import create_keyboard_input, KeyboardInputService, SimpleKeyboardInput
  14. from trixy_core.nlp.intent_dispatcher import IntentDispatcherService
  15. from trixy_core.utils.debug import pinfo, pdebug, perror
  16. class StandaloneApplication(IApplication):
  17. """
  18. Standalone-Anwendung für Trixy.
  19. Bietet alle Funktionen von Server und Client in
  20. einer einzelnen Anwendung.
  21. """
  22. def __init__(
  23. self,
  24. config_path: str | Path = "config/standalone_config.json",
  25. debug: bool = False,
  26. keyboard_input: bool = False,
  27. ) -> None:
  28. """
  29. Initialisiert die Standalone-Anwendung.
  30. Args:
  31. config_path: Pfad zur Konfigurationsdatei
  32. debug: Debug-Modus aktivieren
  33. keyboard_input: Keyboard-Eingabe aktivieren
  34. """
  35. super().__init__(debug)
  36. self._config_path = Path(config_path)
  37. self._standalone_config: StandaloneConfig | None = None
  38. self._plugin_manager: PluginManager | None = None
  39. # Im Debug-Modus immer Keyboard-Shortcuts aktivieren (Ctrl+P/N/S)
  40. self._keyboard_input_enabled = keyboard_input or debug
  41. self._keyboard_service: KeyboardInputService | SimpleKeyboardInput | None = None
  42. @property
  43. def standalone_config(self) -> StandaloneConfig:
  44. """Standalone-Konfiguration."""
  45. if self._standalone_config is None:
  46. raise RuntimeError("Standalone nicht initialisiert")
  47. return self._standalone_config
  48. @property
  49. def plugins(self) -> PluginManager:
  50. """Plugin-Manager."""
  51. if self._plugin_manager is None:
  52. raise RuntimeError("Standalone nicht initialisiert")
  53. return self._plugin_manager
  54. @property
  55. def room(self) -> str:
  56. """Raum-Kennung."""
  57. if self._standalone_config:
  58. return self._standalone_config.room
  59. return "standalone"
  60. @property
  61. def alias(self) -> str:
  62. """Anzeigename."""
  63. if self._standalone_config:
  64. return self._standalone_config.alias
  65. return "Trixy"
  66. async def initialize(self) -> None:
  67. """Initialisiert die Standalone-Anwendung."""
  68. pinfo("Initialisiere Standalone...")
  69. # Konfiguration laden
  70. self._standalone_config = self.config_manager.load(
  71. self._config_path,
  72. StandaloneConfig,
  73. name="standalone",
  74. auto_reload=True
  75. )
  76. # Audio-Einstellungen initialisieren (für Plugin-Zugriff)
  77. audio_settings = init_audio_settings_from_config(self._standalone_config)
  78. pdebug(f"Audio-Einstellungen: TTS={audio_settings.tts_sample_rate}Hz, Musik={audio_settings.music_sample_rate}Hz")
  79. # Plugin-Manager initialisieren
  80. self._plugin_manager = PluginManager(
  81. self,
  82. directory=self._standalone_config.plugins.plugin_directory
  83. )
  84. # IntentDispatcher-Service registrieren (verarbeitet intent_received Events)
  85. self._intent_dispatcher = IntentDispatcherService(self)
  86. self.services.register_instance(self._intent_dispatcher)
  87. self.events.register_object(self._intent_dispatcher)
  88. pdebug("Standalone initialisiert")
  89. async def start(self) -> None:
  90. """Startet die Standalone-Anwendung."""
  91. pinfo(f"Starte Standalone als '{self.alias}'...")
  92. # Services starten
  93. await self.services.start_all()
  94. # Plugins laden
  95. if self._standalone_config and self._standalone_config.plugins.enabled:
  96. await self._plugin_manager.load_all()
  97. # Keyboard-Input starten (falls aktiviert)
  98. # Im Standalone-Modus gibt es keine Server-Verbindung,
  99. # daher wird speech_recognized Event direkt emittiert
  100. if self._keyboard_input_enabled:
  101. self._keyboard_service = create_keyboard_input(
  102. self,
  103. connection=None, # Standalone hat keine Server-Verbindung
  104. prompt=f"{self.alias}> ",
  105. )
  106. await self._keyboard_service.start()
  107. pinfo("Standalone gestartet")
  108. async def stop(self) -> None:
  109. """Stoppt die Standalone-Anwendung."""
  110. pinfo("Stoppe Standalone...")
  111. # Keyboard-Input stoppen
  112. if self._keyboard_service:
  113. await self._keyboard_service.stop()
  114. self._keyboard_service = None
  115. # Shutdown-Event auslösen
  116. await self.events.trigger(
  117. "system_shutdown",
  118. SystemShutdown(reason="Standalone shutdown", source="standalone")
  119. )
  120. # Plugins entladen
  121. if self._plugin_manager:
  122. await self._plugin_manager.unload_all()
  123. # Services stoppen
  124. await self.services.stop_all()
  125. # ConfigManager aufräumen
  126. self.config_manager.stop_watching()
  127. pinfo("Standalone gestoppt")