introduce nginex-proxy
use host ip if working in wsl
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
# dashboard/callbacks/overview_callbacks.py
|
||||
import sys
|
||||
print(sys.path)
|
||||
sys.path.append('/workspace')
|
||||
import threading
|
||||
import dash
|
||||
import requests
|
||||
from dash import Input, Output, State, MATCH, html, dcc
|
||||
from flask import session
|
||||
from utils.db import get_session # Diese Funktion muss eine SQLAlchemy-Session liefern!
|
||||
@@ -10,7 +11,8 @@ from utils.mqtt_client import publish, start_loop
|
||||
from config import ENV
|
||||
import dash_bootstrap_components as dbc
|
||||
import os
|
||||
from server.models import Client
|
||||
|
||||
API_BASE_URL = os.getenv("API_BASE_URL", "http://infoscreen-api:8000")
|
||||
|
||||
mqtt_thread_started = False
|
||||
SCREENSHOT_DIR = "received-screenshots"
|
||||
@@ -23,46 +25,48 @@ def ensure_mqtt_running():
|
||||
mqtt_thread_started = True
|
||||
|
||||
def get_latest_screenshot(client_uuid):
|
||||
prefix = f"{client_uuid}_"
|
||||
# TODO: Hier genau im Produkitv-Modus die IPs testen!
|
||||
# Wenn API_BASE_URL auf "http" beginnt, absolute URL verwenden (z.B. im lokalen Dev)
|
||||
if API_BASE_URL.startswith("http"):
|
||||
return f"{API_BASE_URL}/screenshots/{client_uuid}"
|
||||
# Sonst relative URL (nginx-Proxy übernimmt das Routing)
|
||||
return f"/screenshots/{client_uuid}"
|
||||
|
||||
def fetch_clients():
|
||||
try:
|
||||
files = [f for f in os.listdir('..', SCREENSHOT_DIR) if f.startswith(prefix)]
|
||||
if not files:
|
||||
return "/assets/placeholder.png"
|
||||
latest = max(files, key=lambda x: os.path.getmtime(os.path.join('.', SCREENSHOT_DIR, x)))
|
||||
return f"/received-screenshots/{latest}"
|
||||
except Exception:
|
||||
return "/assets/placeholder.png"
|
||||
resp = requests.get(f"{API_BASE_URL}/api/clients")
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except Exception as e:
|
||||
print("Fehler beim Abrufen der Clients:", e)
|
||||
return []
|
||||
|
||||
@dash.callback(
|
||||
Output("clients-cards-container", "children"),
|
||||
Input("interval-update", "n_intervals")
|
||||
)
|
||||
def update_clients(n):
|
||||
# Auto-Login im Development-Modus
|
||||
if "role" not in session:
|
||||
if ENV == "development":
|
||||
session["role"] = "admin"
|
||||
else:
|
||||
return dcc.Location(id="redirect-login", href="/login")
|
||||
|
||||
# ... Session-Handling wie gehabt ...
|
||||
ensure_mqtt_running()
|
||||
session_db = get_session()
|
||||
clients = session_db.query(Client).all()
|
||||
session_db.close()
|
||||
clients = fetch_clients()
|
||||
cards = []
|
||||
for client in clients:
|
||||
uuid = client.uuid
|
||||
uuid = client["uuid"]
|
||||
# screenshot = get_latest_screenshot(uuid)
|
||||
screenshot = get_latest_screenshot(uuid)
|
||||
# if screenshot[-3] != "jpg":
|
||||
# screenshot += ".jpg"
|
||||
print(f"UUID: {uuid}, Screenshot: {screenshot}")
|
||||
card = dbc.Card(
|
||||
[
|
||||
dbc.CardHeader(client.location or client.hardware_hash),
|
||||
dbc.CardHeader(client["location"]),
|
||||
dbc.CardBody([
|
||||
html.Img(
|
||||
src=screenshot,
|
||||
style={"width": "160px", "height": "90px", "object-fit": "cover"},
|
||||
),
|
||||
html.P(f"IP: {client.ip_address or '-'}", className="card-text"),
|
||||
html.P(f"Letzte Aktivität: {client.last_alive or '-'}", className="card-text"),
|
||||
html.P(f"IP: {client["ip_address"] or '-'}", className="card-text"),
|
||||
html.P(f"Letzte Aktivität: {client["last_alive"] or '-'}", className="card-text"),
|
||||
dbc.ButtonGroup([
|
||||
dbc.Button("Reload Page", color="primary", id={"type": "btn-reload", "index": uuid}, n_clicks=0),
|
||||
dbc.Button("Restart Client", color="danger", id={"type": "btn-restart", "index": uuid}, n_clicks=0),
|
||||
|
||||
Reference in New Issue
Block a user