init project files
This commit is contained in:
parent
96f783ad55
commit
1434f35f8b
134
app.py
Normal file
134
app.py
Normal file
@ -0,0 +1,134 @@
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, render_template, jsonify, request, send_from_directory, redirect, url_for
|
||||
|
||||
from mkconnect.mouldking.MouldKing import MouldKing
|
||||
from mkconnect.tracer.TracerConsole import TracerConsole
|
||||
from mkconnect.advertiser.AdvertiserBTSocket import AdvertiserBTSocket
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['CONFIG_DIR'] = 'configs'
|
||||
os.makedirs(app.config['CONFIG_DIR'], exist_ok=True)
|
||||
|
||||
# Logging setup
|
||||
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
tracer = TracerConsole()
|
||||
advertiser = AdvertiserBTSocket()
|
||||
|
||||
current_hub = None
|
||||
current_config = None
|
||||
|
||||
def load_configs():
|
||||
configs = []
|
||||
for file in os.listdir(app.config['CONFIG_DIR']):
|
||||
if file.endswith('.json'):
|
||||
with open(os.path.join(app.config['CONFIG_DIR'], file)) as f:
|
||||
cfg = json.load(f)
|
||||
cfg['filename'] = file
|
||||
configs.append(cfg)
|
||||
return configs
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html', configs=load_configs())
|
||||
|
||||
@app.route('/load_config/<filename>')
|
||||
def load_config(filename):
|
||||
global current_config
|
||||
path = os.path.join(app.config['CONFIG_DIR'], filename)
|
||||
if not os.path.exists(path):
|
||||
return "Config not found", 404
|
||||
with open(path) as f:
|
||||
current_config = json.load(f)
|
||||
current_config['filename'] = filename
|
||||
return redirect(url_for('control_page'))
|
||||
|
||||
@app.route('/control')
|
||||
def control_page():
|
||||
if not current_config:
|
||||
return redirect(url_for('index'))
|
||||
return render_template('control.html', config=current_config)
|
||||
|
||||
@app.route('/api/connect', methods=['POST'])
|
||||
def connect():
|
||||
global current_hub
|
||||
if not current_config:
|
||||
return jsonify({"error": "Keine Config geladen"}), 400
|
||||
try:
|
||||
hub_id = current_config['hub_id']
|
||||
current_hub = MouldKing(hub_id=hub_id, advertiser=advertiser, tracer=tracer)
|
||||
current_hub.connect()
|
||||
logging.info(f"Verbunden mit Hub {hub_id} für {current_config['name']}")
|
||||
return jsonify({"status": "connected"})
|
||||
except Exception as e:
|
||||
logging.error(f"Connect-Fehler: {str(e)}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/control', methods=['POST'])
|
||||
def control():
|
||||
if not current_hub:
|
||||
return jsonify({"error": "Nicht verbunden"}), 400
|
||||
data = request.json
|
||||
port = data['port']
|
||||
value = float(data['value'])
|
||||
|
||||
ch = next((c for c in current_config['channels'] if c['port'] == port), None)
|
||||
if not ch:
|
||||
return jsonify({"error": "Port nicht in Config"}), 400
|
||||
|
||||
if ch.get('negative_only', False) and value > 0:
|
||||
value = -abs(value) # Force negative
|
||||
|
||||
if ch.get('invert', False):
|
||||
value = -value
|
||||
|
||||
if ch['type'] != 'motor':
|
||||
value = ch.get('on_value', 1.0) if value != 0 else ch.get('off_value', 0.0)
|
||||
|
||||
try:
|
||||
current_hub.set_motor(channel=port, power=value) # Passe an reale Methode an
|
||||
logging.info(f"Control: {port} @ {value}")
|
||||
return jsonify({"status": "ok"})
|
||||
except Exception as e:
|
||||
logging.error(f"Control-Fehler: {str(e)}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/stop_all', methods=['POST'])
|
||||
def stop_all():
|
||||
if current_hub:
|
||||
current_hub.stop_all()
|
||||
logging.info("Alle gestoppt")
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
@app.route('/admin')
|
||||
def admin():
|
||||
return render_template('admin.html', configs=load_configs())
|
||||
|
||||
@app.route('/admin/edit/<filename>', methods=['GET', 'POST'])
|
||||
def edit_config(filename):
|
||||
path = os.path.join(app.config['CONFIG_DIR'], filename)
|
||||
if request.method == 'POST':
|
||||
data = request.form['config']
|
||||
with open(path, 'w') as f:
|
||||
json.dump(json.loads(data), f, indent=4)
|
||||
return redirect(url_for('admin'))
|
||||
if not os.path.exists(path):
|
||||
return "Not found", 404
|
||||
with open(path) as f:
|
||||
config = json.load(f)
|
||||
return render_template('edit.html', config=json.dumps(config, indent=4), filename=filename)
|
||||
|
||||
@app.route('/admin/logs')
|
||||
def view_logs():
|
||||
with open('app.log') as f:
|
||||
logs = f.read()
|
||||
return render_template('logs.html', logs=logs)
|
||||
|
||||
@app.route('/configs/<path:filename>')
|
||||
def serve_config_file(filename):
|
||||
return send_from_directory(app.config['CONFIG_DIR'], filename)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5050, debug=True)
|
||||
BIN
configs/.DS_Store
vendored
Normal file
BIN
configs/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
configs/dampflok1.jpg
Normal file
BIN
configs/dampflok1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 196 KiB |
12
configs/dampflok1.json
Normal file
12
configs/dampflok1.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Dampflok BR 01",
|
||||
"image": "dampflok1.jpg",
|
||||
"hub_id": 0,
|
||||
"hub_type": "4channel",
|
||||
"channels": [
|
||||
{"port": "A", "type": "motor", "name": "Fahrtrichtung", "invert": false, "negative_only": false},
|
||||
{"port": "B", "type": "motor", "name": "Unterstützung", "invert": false, "negative_only": false},
|
||||
{"port": "C", "type": "light", "name": "Licht vorne", "on_value": 1.0, "off_value": 0.0, "negative_only": false},
|
||||
{"port": "D", "type": "fogger", "name": "Dampf", "on_value": -1.0, "off_value": 0.0, "negative_only": true}
|
||||
]
|
||||
}
|
||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
||||
flask==3.0.3
|
||||
# Deine mkconnect-lib – je nach Installationsweg
|
||||
git+https://git.avalon-skynet.work/oberon/mkconnect-lib.git
|
||||
# oder falls lokal gebaut: ./path/to/mkconnect-lib
|
||||
7
static/css/custom.css
Normal file
7
static/css/custom.css
Normal file
@ -0,0 +1,7 @@
|
||||
/* custom.css */
|
||||
body {
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
.card {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
2
static/js/app.js
Normal file
2
static/js/app.js
Normal file
@ -0,0 +1,2 @@
|
||||
// app.js – gemeinsames JavaScript
|
||||
console.log("MK Control JS geladen");
|
||||
0
templates/admin.html
Normal file
0
templates/admin.html
Normal file
84
templates/base.html
Normal file
84
templates/base.html
Normal file
@ -0,0 +1,84 @@
|
||||
<!doctype html>
|
||||
<html lang="de" data-bs-theme="light">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{% block title %}Mould King Control{% endblock %}</title>
|
||||
|
||||
<!-- Bootstrap 5.3 CSS CDN -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
|
||||
crossorigin="anonymous">
|
||||
|
||||
<!-- Eigenes CSS (optional – später erweitern) -->
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/custom.css') }}">
|
||||
|
||||
{% block head_extra %}{% endblock %}
|
||||
</head>
|
||||
<body class="d-flex flex-column min-vh-100">
|
||||
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="{{ url_for('index') }}">MK Control</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
|
||||
data-bs-target="#navbarNav" aria-controls="navbarNav"
|
||||
aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if request.path == url_for('index') %}active{% endif %}"
|
||||
href="{{ url_for('index') }}">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if 'control' in request.path %}active{% endif %}"
|
||||
href="{{ url_for('index') }}">Steuerung</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if 'admin' in request.path %}active{% endif %}"
|
||||
href="{{ url_for('admin') }}">Admin</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Hauptinhalt -->
|
||||
<main class="flex-grow-1 py-4">
|
||||
<div class="container">
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- Footer -->
|
||||
<footer class="bg-dark text-white text-center py-3 mt-auto">
|
||||
<div class="container">
|
||||
<p class="mb-0">Mould King Bluetooth Control © 2025–2026 | Powered by Flask & Bootstrap</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<!-- Bootstrap JS Bundle (inkl. Popper) -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
<!-- Eigenes JS -->
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
|
||||
{% block scripts %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
0
templates/control.html
Normal file
0
templates/control.html
Normal file
0
templates/edit.html
Normal file
0
templates/edit.html
Normal file
0
templates/index.html
Normal file
0
templates/index.html
Normal file
0
templates/logs.html
Normal file
0
templates/logs.html
Normal file
Loading…
x
Reference in New Issue
Block a user