Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
from fastapi import FastAPI, HTTPException, status
|
| 4 |
-
from fastapi.
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
-
app = FastAPI(title="GSM-R
|
| 9 |
-
security = HTTPBearer()
|
| 10 |
|
| 11 |
-
# Secrets aus den Hugging Face Space Einstellungen auslesen
|
| 12 |
WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL")
|
| 13 |
-
|
| 14 |
-
SYSTEM_PIN = os.getenv("GSM_R_PIN", "9351")
|
| 15 |
|
| 16 |
class GSMRState(BaseModel):
|
| 17 |
zugnummer: str
|
| 18 |
rolle: str
|
| 19 |
event: str # "BOOT", "LOGIN", "NOTRUF", "FDL_ANRUF", "TRENNEN"
|
| 20 |
-
pin: str
|
| 21 |
-
fdl_kennung: Optional[str] = "Zentraler Fdl"
|
| 22 |
|
| 23 |
def send_to_discord(content: str, embed: dict = None):
|
| 24 |
if not WEBHOOK_URL:
|
| 25 |
-
print("🚨 Discord Webhook URL
|
| 26 |
return False
|
| 27 |
-
|
| 28 |
payload = {"content": content}
|
| 29 |
if embed:
|
| 30 |
payload["embeds"] = [embed]
|
| 31 |
-
|
| 32 |
try:
|
| 33 |
response = requests.post(WEBHOOK_URL, json=payload)
|
| 34 |
return response.status_code == 204
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"Fehler
|
| 37 |
return False
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
}
|
| 46 |
|
|
|
|
| 47 |
@app.post("/gsmr/event")
|
| 48 |
def handle_gsmr_event(data: GSMRState):
|
| 49 |
-
# 🔐 PIN-Überprüfung
|
| 50 |
if data.pin != SYSTEM_PIN:
|
| 51 |
raise HTTPException(
|
| 52 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 53 |
-
detail="
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# Funktionale Adresse (FKA) generieren
|
| 57 |
role_prefix = "01" if "Tf" in data.rolle or "Triebfahrzeug" in data.rolle else "02"
|
| 58 |
fka = f"{role_prefix}{data.zugnummer}"
|
| 59 |
|
| 60 |
-
if data.event == "
|
| 61 |
-
embed = {
|
| 62 |
-
"title": "📱 GSM-R Funkgerät hochgefahren",
|
| 63 |
-
"color": 8421504,
|
| 64 |
-
"description": "Mesa 23 initialisiert. Netzsuche erfolgreich."
|
| 65 |
-
}
|
| 66 |
-
send_to_discord(content="⚡ *Gerät eingeschaltet...*", embed=embed)
|
| 67 |
-
|
| 68 |
-
elif data.event == "LOGIN":
|
| 69 |
embed = {
|
| 70 |
"title": "📡 Im GSM-R Netz registriert",
|
| 71 |
"color": 3066993,
|
| 72 |
"fields": [
|
| 73 |
-
{"name": "
|
| 74 |
{"name": "Funktionale Adresse (FKA)", "value": fka, "inline": True},
|
| 75 |
{"name": "Rolle", "value": data.rolle, "inline": True}
|
| 76 |
]
|
|
@@ -81,11 +67,10 @@ def handle_gsmr_event(data: GSMRState):
|
|
| 81 |
embed = {
|
| 82 |
"title": "🚨🚨 RAILWAY EMERGENCY CALL (REC) 🚨🚨",
|
| 83 |
"color": 15158332,
|
| 84 |
-
"description":
|
| 85 |
"fields": [
|
| 86 |
{"name": "Absender (FKA)", "value": fka, "inline": True},
|
| 87 |
-
{"name": "
|
| 88 |
-
{"name": "Priorität", "value": "Höchste (EIRENE Prio 0)", "inline": True}
|
| 89 |
]
|
| 90 |
}
|
| 91 |
send_to_discord(content="@here ⚠️ **ZUGFUNK-NOTRUF AUSGELÖST!** ⚠️", embed=embed)
|
|
@@ -103,13 +88,10 @@ def handle_gsmr_event(data: GSMRState):
|
|
| 103 |
|
| 104 |
elif data.event == "TRENNEN":
|
| 105 |
embed = {
|
| 106 |
-
"title": "📴
|
| 107 |
"color": 15105570,
|
| 108 |
-
"description": f"Zug {data.zugnummer} hat
|
| 109 |
}
|
| 110 |
send_to_discord(content=f"⚪ Zug {data.zugnummer} abgemeldet.", embed=embed)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
raise HTTPException(status_code=400, detail="Unbekanntes GSM-R Event")
|
| 114 |
-
|
| 115 |
-
return {"status": "Event autorisiert und verarbeitet", "fka": fka}
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
from fastapi import FastAPI, HTTPException, status
|
| 4 |
+
from fastapi.responses import HTMLResponse
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
+
app = FastAPI(title="GSM-R Web Simulation")
|
|
|
|
| 10 |
|
|
|
|
| 11 |
WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL")
|
| 12 |
+
SYSTEM_PIN = os.getenv("GSM_R_PIN", "1234")
|
|
|
|
| 13 |
|
| 14 |
class GSMRState(BaseModel):
|
| 15 |
zugnummer: str
|
| 16 |
rolle: str
|
| 17 |
event: str # "BOOT", "LOGIN", "NOTRUF", "FDL_ANRUF", "TRENNEN"
|
| 18 |
+
pin: str
|
| 19 |
+
fdl_kennung: Optional[str] = "Zentraler Fdl"
|
| 20 |
|
| 21 |
def send_to_discord(content: str, embed: dict = None):
|
| 22 |
if not WEBHOOK_URL:
|
| 23 |
+
print("🚨 Discord Webhook URL nicht konfiguriert!")
|
| 24 |
return False
|
|
|
|
| 25 |
payload = {"content": content}
|
| 26 |
if embed:
|
| 27 |
payload["embeds"] = [embed]
|
|
|
|
| 28 |
try:
|
| 29 |
response = requests.post(WEBHOOK_URL, json=payload)
|
| 30 |
return response.status_code == 204
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Fehler bei Discord-Übertragung: {e}")
|
| 33 |
return False
|
| 34 |
|
| 35 |
+
# Route für die Weboberfläche
|
| 36 |
+
@app.get("/", response_class=HTMLResponse)
|
| 37 |
+
def get_gui():
|
| 38 |
+
# Liest die index.html ein und liefert sie aus
|
| 39 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 40 |
+
return f.read()
|
|
|
|
| 41 |
|
| 42 |
+
# API-Endpunkt für die Events
|
| 43 |
@app.post("/gsmr/event")
|
| 44 |
def handle_gsmr_event(data: GSMRState):
|
|
|
|
| 45 |
if data.pin != SYSTEM_PIN:
|
| 46 |
raise HTTPException(
|
| 47 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 48 |
+
detail="Ungültige GSM-R System-PIN!"
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
role_prefix = "01" if "Tf" in data.rolle or "Triebfahrzeug" in data.rolle else "02"
|
| 52 |
fka = f"{role_prefix}{data.zugnummer}"
|
| 53 |
|
| 54 |
+
if data.event == "LOGIN":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
embed = {
|
| 56 |
"title": "📡 Im GSM-R Netz registriert",
|
| 57 |
"color": 3066993,
|
| 58 |
"fields": [
|
| 59 |
+
{"name": "Zugnummer", "value": data.zugnummer, "inline": True},
|
| 60 |
{"name": "Funktionale Adresse (FKA)", "value": fka, "inline": True},
|
| 61 |
{"name": "Rolle", "value": data.rolle, "inline": True}
|
| 62 |
]
|
|
|
|
| 67 |
embed = {
|
| 68 |
"title": "🚨🚨 RAILWAY EMERGENCY CALL (REC) 🚨🚨",
|
| 69 |
"color": 15158332,
|
| 70 |
+
"description": "**ACHTUNG:** Ein Zugfunk-Notruf wurde abgesetzt!",
|
| 71 |
"fields": [
|
| 72 |
{"name": "Absender (FKA)", "value": fka, "inline": True},
|
| 73 |
+
{"name": "Kanal", "value": "Gruppe 200 (Strecke)", "inline": True}
|
|
|
|
| 74 |
]
|
| 75 |
}
|
| 76 |
send_to_discord(content="@here ⚠️ **ZUGFUNK-NOTRUF AUSGELÖST!** ⚠️", embed=embed)
|
|
|
|
| 88 |
|
| 89 |
elif data.event == "TRENNEN":
|
| 90 |
embed = {
|
| 91 |
+
"title": "📴 Abmeldung / Verbindung beendet",
|
| 92 |
"color": 15105570,
|
| 93 |
+
"description": f"Zug {data.zugnummer} hat sich abgemeldet."
|
| 94 |
}
|
| 95 |
send_to_discord(content=f"⚪ Zug {data.zugnummer} abgemeldet.", embed=embed)
|
| 96 |
|
| 97 |
+
return {"status": "Event verarbeitet", "fka": fka}
|
|
|
|
|
|
|
|
|