Showboard
Build und Push Docker Image / build-and-push (push) Successful in 14s

This commit is contained in:
2026-10-08 08:16:40 +02:00
parent 21e5069e9a
commit bf61910304
6 changed files with 403 additions and 2 deletions
+104 -1
View File
@@ -19,11 +19,12 @@ import io
import time
import logging
import functools
import hmac
from datetime import date
import openpyxl
from openpyxl.styles import Font
from flask import Flask, request, redirect, url_for, render_template, abort, session, send_file
from flask import Flask, request, redirect, url_for, render_template, abort, session, send_file, make_response
import db
@@ -54,6 +55,11 @@ app.secret_key = os.environ.get("SECRET_KEY", "bitte-in-der-.env-aendern")
app.config["MAX_CONTENT_LENGTH"] = 5 * 1024 * 1024
MASCHINEN_PASSWORT = os.environ.get("MASCHINEN_PASSWORT", "admin")
# Optionaler API-Token für /api/maschinen. Ist er nicht gesetzt, ist die
# Schnittstelle (wie der Rest der Anwendung im internen Netz) ohne Anmeldung
# lesbar. Ist er gesetzt, muss er als "Authorization: Bearer <token>" oder
# als Header "X-API-Key: <token>" mitgeschickt werden.
API_TOKEN = os.environ.get("API_TOKEN", "")
try:
db.init_pool()
@@ -651,6 +657,80 @@ def machine_beenden(maschine_id):
)
# ---------------------------------------------------------------------
# Anzeige-Dashboard je Maschine (nur lesend, z.B. für Monitore in der Halle)
# ---------------------------------------------------------------------
DASHBOARD_LOG_LIMIT = 50
def get_naechster_auftrag(cur, maschine_id):
"""Nächster Auftrag in der Warteschlange der Maschine (gleiche
Reihenfolge wie in der Auftragsauswahl: Priorität, dann ID)."""
cur.execute(
'SELECT * FROM "Auftraege" WHERE maschine_id = %s AND status IN (\'offen\', \'pause\') '
'ORDER BY sid ASC NULLS LAST, id ASC LIMIT 1',
(maschine_id,),
)
return cur.fetchone()
@app.route("/m<int:maschine_id>_show")
def machine_show(maschine_id):
with db.get_cursor() as cur:
maschine = get_maschine(cur, maschine_id)
if not maschine:
abort(404)
auftrag = get_aktiver_auftrag(cur, maschine_id)
naechster = get_naechster_auftrag(cur, maschine_id)
kennzahlen = None
protokoll = []
if auftrag:
nr = auftrag["auftragsnummer"]
gesamt = float(auftrag["einheiten"] or 0)
produziert = float(get_letzte_produktion(cur, maschine_id, nr) or 0)
soll_h = float(auftrag["soll_h"] or 0)
ist_h = float(get_letzter_h_avg(cur, maschine_id, nr) or 0)
kennzahlen = {
"gesamt": fmt_num(gesamt),
"produziert": fmt_num(produziert),
# Bei (gewollter) Überbuchung nicht negativ anzeigen.
"offen": fmt_num(max(gesamt - produziert, 0)),
"soll_h": fmt_num(soll_h),
"ist_h": fmt_num(ist_h),
# Abweichung der zuletzt gebuchten Rate vom Soll pro Stunde.
"abweichung": fmt_num(round(ist_h - soll_h, 2)),
}
cur.execute(
'SELECT id, timestamp, status, produziert, h_avg, p_grund FROM "MDE" '
'WHERE maschine_id = %s AND "order" = %s '
'ORDER BY timestamp DESC, id DESC LIMIT %s',
(maschine_id, nr, DASHBOARD_LOG_LIMIT),
)
for z in cur.fetchall():
ts = z["timestamp"]
protokoll.append({
"ts": ts,
"zeit": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(ts))) if ts else "-",
"status": z["status"] or "",
"produziert": fmt_num(z["produziert"]),
"h_avg": fmt_num(z["h_avg"]),
"grund": z["p_grund"] or "",
})
antwort = make_response(render_template(
"machine_show.html",
maschine=maschine,
maschine_badge_id=maschine["id"],
auftrag=auftrag,
naechster=naechster,
k=kennzahlen,
protokoll=protokoll,
))
antwort.headers["Cache-Control"] = "no-store"
return antwort
# ---------------------------------------------------------------------
# Produktionsleiter
# ---------------------------------------------------------------------
@@ -1359,6 +1439,29 @@ def index():
return redirect(url_for("leitstand_home"))
def _api_token_ok():
if not API_TOKEN:
return True
auth = request.headers.get("Authorization", "")
gesendet = auth[7:].strip() if auth.lower().startswith("bearer ") else request.headers.get("X-API-Key", "")
return hmac.compare_digest(gesendet.encode("utf-8"), API_TOKEN.encode("utf-8"))
@app.route("/api/maschinen")
def api_maschinen():
"""Maschinenliste (ID, Name, Standort) als JSON für andere Programme."""
if not _api_token_ok():
return {"error": "unauthorized"}, 401, {"WWW-Authenticate": "Bearer"}
with db.get_cursor() as cur:
maschinen = get_maschinen_liste(cur)
return {
"maschinen": [
{"id": m["id"], "name": m["name"], "standort": m["standort"]}
for m in maschinen
]
}
@app.route("/api/health")
def health():
try:
+123
View File
@@ -520,3 +520,126 @@ table.data-table td.col-actions { white-space: nowrap; }
font-size: 1.15em;
font-weight: 600;
}
/* ------------------------------------------------------------------
Anzeige-Dashboard (/m<N>_show) - reine Anzeige, skaliert mit der
Bildschirmbreite (Monitor in der Halle)
------------------------------------------------------------------ */
.dash-body { overflow: hidden; }
.dash {
height: 100vh;
padding: clamp(10px, 1.2vw, 24px);
display: grid;
grid-template-rows: auto auto minmax(0, 1fr);
gap: clamp(10px, 1.4vw, 28px);
}
.dash-top {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap: clamp(10px, 1.2vw, 24px);
align-items: center;
}
.dash-kpis {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: clamp(8px, 1vw, 20px);
}
.dash-tile { min-width: 0; padding: 4px 6px; }
.dash-title {
color: var(--text-dim);
font-size: clamp(0.8rem, 1.05vw, 1.3rem);
font-weight: 600;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.dash-order {
min-height: 1.2em;
margin-top: 0.4vw;
text-align: center;
font-size: clamp(1.6rem, 3.2vw, 4.2rem);
font-weight: 700;
line-height: 1.15;
overflow-wrap: anywhere;
}
.dash-article {
min-height: 1.3em;
text-align: center;
color: var(--text-dim);
font-size: clamp(0.9rem, 1.3vw, 1.7rem);
}
.dash-clock { text-align: center; }
.dash-clock-date { font-size: clamp(1rem, 1.5vw, 2rem); }
.dash-clock-time {
font-size: clamp(2.4rem, 5.2vw, 7rem);
font-weight: 500;
line-height: 1.05;
font-variant-numeric: tabular-nums;
}
.dash-logo img {
display: block;
width: auto;
height: clamp(60px, 9vw, 150px);
max-width: 100%;
margin: 0 auto;
}
.dash-value {
min-height: 1.1em;
margin-top: 0.6vw;
text-align: center;
font-size: clamp(2.2rem, 4.6vw, 6.4rem);
font-weight: 600;
line-height: 1.1;
font-variant-numeric: tabular-nums;
}
.dash-value.green { color: #73bf69; }
.dash-value.red { color: #f2495c; }
.dash-key {
background: #5794f2;
border-radius: 6px;
color: #fff;
}
.dash-key .dash-title { color: rgba(255, 255, 255, 0.85); }
.dash-log {
min-height: 0;
display: flex;
flex-direction: column;
border: 1px solid var(--border);
border-radius: 8px;
padding: clamp(8px, 0.9vw, 16px);
}
.dash-log-scroll { flex: 1; min-height: 0; overflow: auto; margin-top: 8px; }
.dash-table {
width: 100%;
border-collapse: collapse;
font-size: clamp(0.95rem, 1.35vw, 1.8rem);
font-variant-numeric: tabular-nums;
}
.dash-table th {
position: sticky;
top: 0;
background: var(--bg);
color: var(--text-dim);
text-align: left;
font-weight: 600;
padding: 0.5em 0.7em;
border-bottom: 1px solid var(--border);
}
.dash-table td { padding: 0.5em 0.7em; border-bottom: 1px solid var(--border); }
.dash-table .num { text-align: right; }
.dash-table tbody tr:first-child td { font-weight: 600; }
/* Ohne aktiven Auftrag (leerer Wert) keine blaue Fläche */
.dash-key:has(.dash-value:empty) { background: transparent; }
.dash-key:has(.dash-value:empty) .dash-title { color: var(--text-dim); }
+133
View File
@@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard {{ maschine.name or ('Maschine ' ~ maschine.id) }} – Hour by Hour</title>
<link rel="icon" type="image/svg+xml" href="{{ url_for('static', filename='logo.svg') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<noscript><meta http-equiv="refresh" content="30"></noscript>
</head>
<body class="dash-body">
<main class="dash">
<section class="dash-top">
<div class="dash-tile dash-wide" data-live="aktuell">
<div class="dash-title">Aktueller Auftrag</div>
<div class="dash-order">{% if auftrag %}{{ auftrag.auftragsnummer }}{% endif %}</div>
<div class="dash-article">{% if auftrag and auftrag.artikel %}{{ auftrag.artikel }}{% endif %}</div>
</div>
<div class="dash-tile dash-wide" data-live="naechster">
<div class="dash-title">N&auml;chster Auftrag</div>
<div class="dash-order">{% if naechster %}{{ naechster.auftragsnummer }}{% endif %}</div>
<div class="dash-article">{% if naechster and naechster.artikel %}{{ naechster.artikel }}{% endif %}</div>
</div>
<div class="dash-tile dash-clock">
<div class="dash-clock-date" id="dash-date">&nbsp;</div>
<div class="dash-clock-time" id="dash-time">&nbsp;</div>
</div>
<div class="dash-tile dash-logo">
<img src="{{ url_for('static', filename='logo.svg') }}" alt="Logo">
</div>
</section>
<section class="dash-kpis">
<div class="dash-tile">
<div class="dash-title">St&uuml;ckzahl (gesamt)</div>
<div class="dash-value green" data-live="k-gesamt">{% if k %}{{ k.gesamt }}{% endif %}</div>
</div>
<div class="dash-tile">
<div class="dash-title">St&uuml;ckzahl (produziert)</div>
<div class="dash-value green" data-live="k-produziert">{% if k %}{{ k.produziert }}{% endif %}</div>
</div>
<div class="dash-tile">
<div class="dash-title">St&uuml;ckzahl (offen)</div>
<div class="dash-value green" data-live="k-offen">{% if k %}{{ k.offen }}{% endif %}</div>
</div>
<div class="dash-tile">
<div class="dash-title">Soll pro Stunde</div>
<div class="dash-value green" data-live="k-soll">{% if k %}{{ k.soll_h }}{% endif %}</div>
</div>
<div class="dash-tile dash-key">
<div class="dash-title">St&uuml;ck/h (Buchung)</div>
<div class="dash-value" data-live="k-ist">{% if k %}{{ k.ist_h }}{% endif %}</div>
</div>
<div class="dash-tile">
<div class="dash-title">Abweichung</div>
<div class="dash-value red" data-live="k-abw">{% if k %}{{ k.abweichung }}{% endif %}</div>
</div>
</section>
<section class="dash-log">
<div class="dash-title">Protokoll</div>
<div class="dash-log-scroll">
<table class="dash-table">
<thead>
<tr><th>Zeit</th><th>Status</th><th class="num">Produziert</th><th class="num">Teile/Stunde</th><th>Grund</th></tr>
</thead>
<tbody data-live="protokoll">
{% for z in protokoll %}
<tr>
<td><time data-ts="{{ z.ts }}">{{ z.zeit }}</time></td>
<td>{{ z.status }}</td>
<td class="num">{{ z.produziert }}</td>
<td class="num">{{ z.h_avg }}</td>
<td>{{ z.grund }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</section>
</main>
{% if maschine_badge_id %}<div class="machine-id-badge">M{{ maschine_badge_id }}</div>{% endif %}
<script>
(function () {
function z2(n) { return (n < 10 ? '0' : '') + n; }
// Uhr (Browser-Zeit des Anzeigegeräts)
function tick() {
var d = new Date();
document.getElementById('dash-date').textContent =
z2(d.getDate()) + '.' + z2(d.getMonth() + 1) + '.' + d.getFullYear();
document.getElementById('dash-time').textContent =
z2(d.getHours()) + ':' + z2(d.getMinutes()) + ':' + z2(d.getSeconds());
}
tick();
setInterval(tick, 1000);
// Protokollzeiten in der Zeitzone des Anzeigegeräts darstellen (der Server
// läuft im Container meist in UTC).
function zeiten() {
document.querySelectorAll('time[data-ts]').forEach(function (el) {
var ts = parseInt(el.getAttribute('data-ts'), 10);
if (!ts) return;
var d = new Date(ts * 1000);
el.textContent = d.getFullYear() + '-' + z2(d.getMonth() + 1) + '-' + z2(d.getDate()) + ' ' +
z2(d.getHours()) + ':' + z2(d.getMinutes()) + ':' + z2(d.getSeconds());
});
}
zeiten();
// Daten alle 15 s ohne Neuladen der Seite aktualisieren. Schlägt ein Abruf
// fehl (Netz/Server kurz weg), bleibt einfach der letzte Stand stehen.
function aktualisieren() {
fetch(window.location.href, { cache: 'no-store' })
.then(function (r) { return r.ok ? r.text() : Promise.reject(); })
.then(function (html) {
var neu = new DOMParser().parseFromString(html, 'text/html');
neu.querySelectorAll('[data-live]').forEach(function (n) {
var ziel = document.querySelector('[data-live="' + n.getAttribute('data-live') + '"]');
if (ziel && ziel.innerHTML !== n.innerHTML) ziel.innerHTML = n.innerHTML;
});
zeiten();
})
.catch(function () {});
}
setInterval(aktualisieren, 15000);
})();
</script>
</body>
</html>