72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
"""
|
||
moOde Radio Controller
|
||
Flask-Webapp zur Steuerung des moOde Audio Players
|
||
"""
|
||
|
||
import os
|
||
import requests
|
||
from flask import Flask, jsonify, render_template, request
|
||
|
||
# templates/ und static/ immer relativ zur app.py finden,
|
||
# egal von welchem Verzeichnis aus gestartet wird
|
||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||
app = Flask(__name__, template_folder=os.path.join(BASE_DIR, "templates"))
|
||
|
||
# moOde Player IP-Adresse – anpassen!
|
||
MOODE_URL = "http://moode.local"
|
||
|
||
|
||
def moode_cmd(cmd: str, **kwargs) -> dict:
|
||
"""Sendet einen Befehl an die moOde-API."""
|
||
try:
|
||
params = {"cmd": cmd, **kwargs}
|
||
r = requests.get(f"{MOODE_URL}/engine/index.php", params=params, timeout=5)
|
||
r.raise_for_status()
|
||
try:
|
||
return {"ok": True, "data": r.json()}
|
||
except Exception:
|
||
return {"ok": True, "data": r.text}
|
||
except requests.exceptions.ConnectionError:
|
||
return {"ok": False, "error": "moOde nicht erreichbar. IP-Adresse prüfen."}
|
||
except requests.exceptions.Timeout:
|
||
return {"ok": False, "error": "Verbindungs-Timeout."}
|
||
except Exception as e:
|
||
return {"ok": False, "error": str(e)}
|
||
|
||
|
||
@app.route("/")
|
||
def index():
|
||
return render_template("index.html", moode_url=MOODE_URL)
|
||
|
||
|
||
@app.route("/api/play", methods=["POST"])
|
||
def play():
|
||
result = moode_cmd("play")
|
||
return jsonify(result)
|
||
|
||
|
||
@app.route("/api/stop", methods=["POST"])
|
||
def stop():
|
||
result = moode_cmd("stop")
|
||
return jsonify(result)
|
||
|
||
|
||
@app.route("/api/volume", methods=["POST"])
|
||
def volume():
|
||
level = request.json.get("level")
|
||
if level is None or not (0 <= int(level) <= 100):
|
||
return jsonify({"ok": False, "error": "Ungültige Lautstärke (0–100)."})
|
||
result = moode_cmd("vol.set", level=int(level))
|
||
return jsonify(result)
|
||
|
||
|
||
@app.route("/api/status")
|
||
def status():
|
||
result = moode_cmd("get_currentsong")
|
||
return jsonify(result)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print("moOde Controller gestartet → http://localhost:5555")
|
||
app.run(debug=True, host="0.0.0.0", port=5555)
|