update api urls
This commit is contained in:
parent
27d080e09a
commit
55f85f8684
105
app.py
105
app.py
@ -30,6 +30,8 @@ advertiser = AdvertiserBTSocket() # ← ohne tracer, wie korrigiert
|
|||||||
# Globale Zustände (für Einzelbenutzer / Entwicklung – später Session/DB)
|
# Globale Zustände (für Einzelbenutzer / Entwicklung – später Session/DB)
|
||||||
current_config = None
|
current_config = None
|
||||||
current_hub: MouldKing | None = None
|
current_hub: MouldKing | None = None
|
||||||
|
current_module = None # Module6_0 oder Module4_0
|
||||||
|
current_device = None # Device0/1/2 je nach hub_id
|
||||||
|
|
||||||
def load_configs():
|
def load_configs():
|
||||||
configs = []
|
configs = []
|
||||||
@ -160,70 +162,97 @@ def serve_config_file(filename):
|
|||||||
|
|
||||||
@app.route('/api/connect', methods=['POST'])
|
@app.route('/api/connect', methods=['POST'])
|
||||||
def api_connect():
|
def api_connect():
|
||||||
global current_hub
|
global current_hub, current_module, current_device
|
||||||
if current_config is None:
|
if current_config is None:
|
||||||
return jsonify({"success": False, "message": "Keine Konfiguration geladen"}), 400
|
return jsonify({"success": False, "message": "Keine Konfiguration geladen"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hub_id = int(current_config.get('hub_id', 0))
|
hub_id = int(current_config.get('hub_id', 0))
|
||||||
|
hub_type = current_config.get('hub_type', '6channel') # '4channel' oder '6channel'
|
||||||
|
|
||||||
# Falls schon eine Instanz existiert → erstmal sauber trennen
|
# Alte Verbindung sauber trennen
|
||||||
if current_hub is not None:
|
if current_device is not None:
|
||||||
try:
|
try:
|
||||||
current_hub.disconnect() # ← falls diese Methode existiert
|
current_device.Disconnect()
|
||||||
except AttributeError:
|
except:
|
||||||
|
pass
|
||||||
|
if current_module is not None:
|
||||||
|
# ggf. Stop vor Disconnect
|
||||||
|
try:
|
||||||
|
current_device.Stop()
|
||||||
|
except:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Fehler beim Trennen der alten Instanz: {e}")
|
|
||||||
|
|
||||||
# Instanz ohne Argumente erstellen
|
# MouldKing neu initialisieren
|
||||||
current_hub = MouldKing() # ← KEINE Argumente!
|
mk = MouldKing()
|
||||||
|
mk.SetAdvertiser(advertiser)
|
||||||
# Hub-ID und andere Einstellungen ggf. nachträglich setzen
|
|
||||||
# (das muss deine Bibliothek unterstützen – teste!)
|
|
||||||
try:
|
try:
|
||||||
current_hub.set_hub_id(hub_id) # ← falls diese Methode existiert
|
mk.SetTracer(tracer)
|
||||||
except AttributeError:
|
except:
|
||||||
logger.warning("Keine set_hub_id-Methode → ID wird evtl. anders gesetzt")
|
pass
|
||||||
|
|
||||||
current_hub.connect() # ← das sollte den Hub in BT-Modus schalten
|
# Modul holen
|
||||||
|
if hub_type.lower() in ['6channel', '6']:
|
||||||
|
current_module = mk.Module6_0()
|
||||||
|
else:
|
||||||
|
current_module = mk.Module4_0()
|
||||||
|
|
||||||
logger.info(f"Erfolgreich verbunden mit Hub-ID {hub_id}")
|
# Device auswählen (hub_id = 0,1,2 → Device0,1,2)
|
||||||
return jsonify({"success": True, "message": f"Hub {hub_id} verbunden"})
|
device_attr = f'Device{hub_id}'
|
||||||
|
if not hasattr(current_module, device_attr):
|
||||||
|
raise ValueError(f"Hub-ID {hub_id} nicht unterstützt (max 0-2)")
|
||||||
|
|
||||||
|
current_device = getattr(current_module, device_attr)
|
||||||
|
|
||||||
|
# Verbinden
|
||||||
|
current_device.Connect()
|
||||||
|
|
||||||
|
current_hub = mk # falls später noch gebraucht
|
||||||
|
|
||||||
|
logger.info(f"Verbunden: {hub_type} Hub-ID {hub_id} → {device_attr}")
|
||||||
|
return jsonify({"success": True, "message": f"Hub {hub_id} ({hub_type}) verbunden"})
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Connect-Fehler")
|
logger.exception("Connect-Fehler")
|
||||||
return jsonify({
|
return jsonify({"success": False, "message": f"Verbindungsfehler: {str(e)}"}), 500
|
||||||
"success": False,
|
|
||||||
"message": f"Verbindungsfehler: {str(e)}"
|
|
||||||
}), 500
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/control', methods=['POST'])
|
@app.route('/api/control', methods=['POST'])
|
||||||
def api_control():
|
def api_control():
|
||||||
global current_hub
|
global current_device
|
||||||
if current_hub is None:
|
if current_device is None:
|
||||||
return jsonify({"success": False, "message": "Nicht verbunden"}), 400
|
return jsonify({"success": False, "message": "Nicht verbunden"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
port = data['port']
|
port = data['port'] # 'A', 'B', ...
|
||||||
value = float(data['value'])
|
value = float(data['value'])
|
||||||
|
|
||||||
# Config-spezifische Anpassungen
|
# Port → channelId mappen (A=0, B=1, ...)
|
||||||
channel_config = next((c for c in current_config['channels'] if c['port'] == port), None)
|
channel_map = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5}
|
||||||
if channel_config:
|
if isinstance(port, str):
|
||||||
if channel_config.get('invert', False):
|
port_upper = port.upper()
|
||||||
|
if port_upper in channel_map:
|
||||||
|
channel_id = channel_map[port_upper]
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Ungültiger Port: {port}")
|
||||||
|
else:
|
||||||
|
channel_id = int(port)
|
||||||
|
|
||||||
|
# Config-spezifische Anpassungen (invert, negative_only, on/off-Werte)
|
||||||
|
ch_cfg = next((c for c in current_config['channels'] if c['port'].upper() == port.upper()), None)
|
||||||
|
if ch_cfg:
|
||||||
|
if ch_cfg.get('invert', False):
|
||||||
value = -value
|
value = -value
|
||||||
if channel_config.get('negative_only', False) and value >= 0:
|
if ch_cfg.get('negative_only', False) and value >= 0:
|
||||||
value = -abs(value)
|
value = -abs(value)
|
||||||
if channel_config['type'] != 'motor':
|
if ch_cfg['type'] != 'motor':
|
||||||
value = channel_config.get('on_value', 1.0) if value != 0 else channel_config.get('off_value', 0.0)
|
value = ch_cfg.get('on_value', 1.0) if value != 0 else ch_cfg.get('off_value', 0.0)
|
||||||
|
|
||||||
# Hier kommt der echte Aufruf
|
# Echter Aufruf!
|
||||||
current_hub.set_motor(channel=port, power=value) # ← ← ← Kernaufruf
|
current_device.SetChannel(channel_id, value)
|
||||||
|
|
||||||
logger.info(f"Steuerung: {port} → {value:.2f}")
|
logger.info(f"SetChannel({channel_id}, {value:.2f}) → Port {port}")
|
||||||
return jsonify({"success": True})
|
return jsonify({"success": True})
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -233,12 +262,12 @@ def api_control():
|
|||||||
|
|
||||||
@app.route('/api/stop_all', methods=['POST'])
|
@app.route('/api/stop_all', methods=['POST'])
|
||||||
def api_stop_all():
|
def api_stop_all():
|
||||||
global current_hub
|
global current_device
|
||||||
if current_hub is None:
|
if current_device is None:
|
||||||
return jsonify({"success": False, "message": "Nicht verbunden"}), 400
|
return jsonify({"success": False, "message": "Nicht verbunden"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_hub.stop_all() # ← oder alle Kanäle manuell auf 0 setzen
|
current_device.Stop() # ← Stoppt alle Kanäle des Devices
|
||||||
logger.info("Alle Kanäle gestoppt")
|
logger.info("Alle Kanäle gestoppt")
|
||||||
return jsonify({"success": True})
|
return jsonify({"success": True})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user