This commit is contained in:
+104
-1
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user