introduce nginex-proxy
use host ip if working in wsl
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
import sys
|
||||
sys.path.append('/workspace')
|
||||
import os
|
||||
import json
|
||||
import base64
|
||||
import glob
|
||||
from datetime import datetime, timezone
|
||||
# import paho.mqtt.client as mqtt
|
||||
from datetime import datetime
|
||||
from paho.mqtt import client as mqtt_client
|
||||
import pytz
|
||||
from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models import Client, Base
|
||||
from helpers.check_folder import ensure_folder_exists
|
||||
import shutil
|
||||
|
||||
# Basisverzeichnis relativ zum aktuellen Skript
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# Konfiguration
|
||||
MQTT_BROKER = os.getenv("MQTT_BROKER_HOST", "localhost")
|
||||
@@ -27,24 +31,26 @@ topics = [
|
||||
("infoscreen/heartbeat", 0),
|
||||
# ... weitere Topics hier
|
||||
]
|
||||
SAVE_DIR = "received_screenshots"
|
||||
|
||||
# Verzeichnisse für Screenshots
|
||||
RECEIVED_DIR = os.path.join(BASE_DIR, "received_screenshots")
|
||||
LATEST_DIR = os.path.join(BASE_DIR, "screenshots")
|
||||
MAX_PER_CLIENT = 20
|
||||
|
||||
# Ordner für empfangene Screenshots anlegen
|
||||
ensure_folder_exists(SAVE_DIR)
|
||||
# Ordner für empfangene Screenshots und den neuesten Screenshot anlegen
|
||||
ensure_folder_exists(RECEIVED_DIR)
|
||||
ensure_folder_exists(LATEST_DIR)
|
||||
|
||||
# Datenbank konfigurieren (MariaDB)
|
||||
# Ersetze user, password, host und datenbankname entsprechend.
|
||||
DB_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"
|
||||
engine = create_engine(DB_URL, echo=False)
|
||||
Session = sessionmaker(bind=engine)
|
||||
# Falls Tabellen noch nicht existieren
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
|
||||
def prune_old_screenshots(client_id: str):
|
||||
"""Löscht alte Screenshots, wenn mehr als MAX_PER_CLIENT vorhanden sind."""
|
||||
pattern = os.path.join(SAVE_DIR, f"{client_id}_*.jpg")
|
||||
pattern = os.path.join(RECEIVED_DIR, f"{client_id}_*.jpg")
|
||||
files = sorted(glob.glob(pattern), key=os.path.getmtime)
|
||||
while len(files) > MAX_PER_CLIENT:
|
||||
oldest = files.pop(0)
|
||||
@@ -68,12 +74,17 @@ def handle_screenshot(msg):
|
||||
|
||||
# Dateiname mit Client-ID und Zeitstempel
|
||||
filename = ts.strftime(f"{client_id}_%Y%m%d_%H%M%S.jpg")
|
||||
filepath = os.path.join(SAVE_DIR, filename)
|
||||
received_path = os.path.join(RECEIVED_DIR, filename)
|
||||
|
||||
# Bild speichern
|
||||
with open(filepath, "wb") as f:
|
||||
# Bild im Verzeichnis "received_screenshots" speichern
|
||||
with open(received_path, "wb") as f:
|
||||
f.write(img_data)
|
||||
print(f"Bild gespeichert: {filepath}")
|
||||
print(f"Bild gespeichert: {received_path}")
|
||||
|
||||
# Kopiere den neuesten Screenshot in das Verzeichnis "screenshots"
|
||||
latest_path = os.path.join(LATEST_DIR, f"{client_id}.jpg")
|
||||
shutil.copy(received_path, latest_path)
|
||||
print(f"Neuester Screenshot aktualisiert: {latest_path}")
|
||||
|
||||
# Alte Screenshots beschneiden
|
||||
prune_old_screenshots(client_id)
|
||||
@@ -81,6 +92,7 @@ def handle_screenshot(msg):
|
||||
except Exception as e:
|
||||
print("Fehler beim Verarbeiten der Screenshot-Nachricht:", e)
|
||||
|
||||
|
||||
def handle_heartbeat(msg):
|
||||
"""Verarbeitet Heartbeat und aktualisiert oder legt Clients an."""
|
||||
session = Session()
|
||||
@@ -100,7 +112,6 @@ def handle_heartbeat(msg):
|
||||
else:
|
||||
# Neuer Client: Location per input abfragen
|
||||
location = input(f"Neuer Client {uuid} gefunden. Bitte Standort eingeben: ")
|
||||
# ip_address = msg._sock.getpeername()[0]
|
||||
new_client = Client(
|
||||
uuid=uuid,
|
||||
hardware_hash=hardware_hash,
|
||||
|
||||
Reference in New Issue
Block a user