modularisierung von app.py und app.js - erster versuch
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
# app/bluetooth/manager.py
|
||||
"""
|
||||
Bluetooth-Verbindungsmanagement: MouldKing-Instanz, Monitor-Thread, Cleanup
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
import logging
|
||||
|
||||
from mkconnect.mouldking.MouldKing import MouldKing
|
||||
from mkconnect.tracer.TracerConsole import TracerConsole
|
||||
from mkconnect.advertiser.AdvertiserBTSocket import AdvertiserBTSocket
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Globale Bluetooth-Komponenten (einmalig initialisiert)
|
||||
tracer = TracerConsole()
|
||||
advertiser = AdvertiserBTSocket()
|
||||
|
||||
# Globale Zustände (werden von den Routen gesetzt und hier überwacht)
|
||||
current_hub = None
|
||||
current_module = None
|
||||
current_device = None
|
||||
|
||||
def init_bluetooth():
|
||||
"""
|
||||
Initialisiert die Bluetooth-Komponenten (einmalig beim App-Start).
|
||||
"""
|
||||
global tracer, advertiser
|
||||
logger.info("Bluetooth-Komponenten initialisiert (Advertiser + Tracer)")
|
||||
# Hier ggf. weitere Initialisierung (z. B. hci0 prüfen)
|
||||
|
||||
def connection_monitor():
|
||||
"""
|
||||
Background-Thread: Prüft alle 5 Sekunden, ob der Hub noch antwortet.
|
||||
Bei Fehlern wird die Verbindung sauber getrennt.
|
||||
"""
|
||||
global current_device, current_module, current_hub
|
||||
|
||||
while True:
|
||||
if current_device is not None:
|
||||
try:
|
||||
# Harter Test: Kanal 0 kurz auf 0.1 und zurück auf 0.0 setzen
|
||||
current_device.SetChannel(0, 0.1)
|
||||
time.sleep(0.05) # winzige Pause
|
||||
current_device.SetChannel(0, 0.0)
|
||||
|
||||
logger.debug("Monitor: Hub antwortet → SetChannel-Test OK")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Monitor: Hub scheint weg zu sein: {str(e)}")
|
||||
|
||||
# Sauber trennen
|
||||
try:
|
||||
current_device.Disconnect()
|
||||
except Exception as disconnect_err:
|
||||
logger.debug(f"Disconnect fehlgeschlagen (harmlos): {disconnect_err}")
|
||||
|
||||
# Globale Zustände zurücksetzen
|
||||
current_device = None
|
||||
current_module = None
|
||||
current_hub = None
|
||||
|
||||
# Optional: Frontend benachrichtigen (z. B. später über WebSocket)
|
||||
|
||||
time.sleep(5) # Prüfintervall: 5 Sekunden
|
||||
|
||||
|
||||
# Thread beim Import starten (einmalig)
|
||||
monitor_thread = threading.Thread(target=connection_monitor, daemon=True)
|
||||
monitor_thread.start()
|
||||
logger.info("Bluetooth-Monitor-Thread gestartet (5s Intervall)")
|
||||
Reference in New Issue
Block a user