@echo off REM =================================================== REM S.W.A.R.M-Watchdog - Komplett-Launcher REM - Stoppt laufenden Server (damit Code-Aenderungen greifen) REM - Startet pythonw frisch REM - Wartet bis Port lauscht REM - Oeffnet Chrome REM REM Aufruf: REM swarm-start.bat (Force-Restart, default) REM swarm-start.bat /noreload (nur starten falls nicht laeuft) REM =================================================== setlocal EnableDelayedExpansion set PORT=8765 set URL=http://localhost:%PORT%/04_Fristen-Watchdog/Fristen-Watchdog.html set TASK=SWARM-Watchdog-Server set FORCE_RESTART=1 set NO_TUNNEL=1 REM Tailscale Funnel ersetzt Cloudflare-Tunnel set TUNNEL_LOG=%~dp0cloudflared.log set TUNNEL_URL_FILE=%~dp0tunnel_url.txt if /I "%~1"=="/noreload" set FORCE_RESTART=0 if /I "%~1"=="/notunnel" set NO_TUNNEL=1 if /I "%~2"=="/notunnel" set NO_TUNNEL=1 REM ---------- Python-Pfad finden ---------- set PYTHONW= if exist "%LocalAppData%\Programs\Python\Python312\pythonw.exe" set PYTHONW="%LocalAppData%\Programs\Python\Python312\pythonw.exe" if not defined PYTHONW if exist "%LocalAppData%\Programs\Python\Python311\pythonw.exe" set PYTHONW="%LocalAppData%\Programs\Python\Python311\pythonw.exe" if not defined PYTHONW if exist "%LocalAppData%\Programs\Python\Python313\pythonw.exe" set PYTHONW="%LocalAppData%\Programs\Python\Python313\pythonw.exe" if not defined PYTHONW set PYTHONW=pythonw.exe REM ---------- cloudflared-Pfad finden ---------- set CLOUDFLARED= where cloudflared >nul 2>&1 if %ERRORLEVEL% EQU 0 set CLOUDFLARED=cloudflared if not defined CLOUDFLARED for /f "delims=" %%C in ('dir /b /s "%LocalAppData%\Microsoft\WinGet\Packages\Cloudflare.cloudflared*\cloudflared.exe" 2^>nul') do set CLOUDFLARED="%%C" REM ---------- Chrome-Pfad finden ---------- set CHROME= if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" set CHROME="C:\Program Files\Google\Chrome\Application\chrome.exe" if not defined CHROME if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" set CHROME="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" if not defined CHROME if exist "%LocalAppData%\Google\Chrome\Application\chrome.exe" set CHROME="%LocalAppData%\Google\Chrome\Application\chrome.exe" echo ============================================ echo S.W.A.R.M-Watchdog Launcher echo ============================================ echo Port : %PORT% echo URL : %URL% echo Force-Restart : %FORCE_RESTART% echo Server-Script : %~dp0scripts\watchdog_server.py echo ============================================ echo. REM ---------- Bestehende watchdog_server.py-Prozesse beenden ---------- if "%FORCE_RESTART%"=="1" ( echo [INFO] Stoppe bestehende watchdog_server.py-Prozesse... powershell -NoProfile -Command "Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -match 'watchdog_server\.py' } | ForEach-Object { Write-Output (' - Beende PID ' + $_.ProcessId); Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }" REM Falls Scheduled Task laeuft: anhalten schtasks /End /TN "%TASK%" >nul 2>&1 REM Kurz warten bis Port frei timeout /t 1 /nobreak >nul ) REM ---------- Pruefe ob Server schon antwortet ---------- netstat -an | findstr ":%PORT%" | findstr "LISTENING" >nul if %ERRORLEVEL% EQU 0 ( if "%FORCE_RESTART%"=="1" ( echo [WARN] Port %PORT% ist noch belegt - warte 2 Sekunden... timeout /t 2 /nobreak >nul ) else ( echo [OK] Server laeuft bereits. goto :open_browser ) ) REM ---------- Server starten ---------- echo [INFO] Starte Server: %PYTHONW% scripts\watchdog_server.py start "" /B %PYTHONW% "%~dp0scripts\watchdog_server.py" REM ---------- Warten bis Port lauscht ---------- echo [INFO] Warte auf Server-Start (max 12sec)... set /a TRIES=0 :wait_loop timeout /t 1 /nobreak >nul netstat -an | findstr ":%PORT%" | findstr "LISTENING" >nul if %ERRORLEVEL% EQU 0 ( echo [OK] Server ist hochgefahren. goto :probe_endpoint ) set /a TRIES+=1 if %TRIES% LSS 12 goto :wait_loop echo [WARN] Server scheint nicht zu starten - oeffne Chrome trotzdem goto :open_browser REM ---------- Probe Status-Endpoint (zeigt ob neue Version laeuft) ---------- :probe_endpoint set ENDPOINT_OK=0 for /f "delims=" %%S in ('powershell -NoProfile -Command "try { $r = Invoke-WebRequest -UseBasicParsing -Method POST -Uri 'http://localhost:%PORT%/api/status/save' -ContentType 'application/json' -Body '{\"fristen\":{}}' -TimeoutSec 3; $r.StatusCode } catch { try { $_.Exception.Response.StatusCode.value__ } catch { 0 } }"') do set HTTPCODE=%%S if "%HTTPCODE%"=="200" set ENDPOINT_OK=1 if "%HTTPCODE%"=="400" set ENDPOINT_OK=1 if "%ENDPOINT_OK%"=="1" ( echo [OK] /api/status/save erreichbar - Status-Sync aktiv. ) else ( echo [WARN] /api/status/save antwortet HTTP %HTTPCODE% - Server-Code evtl. veraltet. ) REM ---------- Cloudflared-Tunnel ---------- if "%NO_TUNNEL%"=="1" goto :open_browser if not defined CLOUDFLARED ( echo [WARN] cloudflared nicht gefunden - ueberspringe Tunnel. goto :open_browser ) echo [INFO] Stoppe bestehende cloudflared-Prozesse... taskkill /F /IM cloudflared.exe >nul 2>&1 if exist "%TUNNEL_LOG%" del /Q "%TUNNEL_LOG%" >nul 2>&1 echo [INFO] Starte Cloudflare-Tunnel auf http://localhost:%PORT% start "" /B %CLOUDFLARED% tunnel --url http://localhost:%PORT% --no-autoupdate --logfile "%TUNNEL_LOG%" echo [INFO] Warte auf Tunnel-URL (max 15sec)... set /a TUNNEL_TRIES=0 :tunnel_wait timeout /t 1 /nobreak >nul set TUNNEL_URL= for /f "tokens=*" %%U in ('powershell -NoProfile -Command "if (Test-Path '%TUNNEL_LOG%') { (Select-String -Path '%TUNNEL_LOG%' -Pattern 'https://[a-z0-9-]+\.trycloudflare\.com' -ErrorAction SilentlyContinue | Select-Object -First 1).Matches.Value }"') do set TUNNEL_URL=%%U if defined TUNNEL_URL goto :tunnel_ready set /a TUNNEL_TRIES+=1 if %TUNNEL_TRIES% LSS 15 goto :tunnel_wait echo [WARN] Tunnel-URL nicht gefunden - cloudflared.log pruefen. goto :open_browser :tunnel_ready echo [OK] Tunnel-URL: %TUNNEL_URL% > "%TUNNEL_URL_FILE%" echo %TUNNEL_URL% REM ---------- Chrome oeffnen ---------- :open_browser if not defined CHROME ( echo [WARN] Chrome nicht gefunden. URL manuell oeffnen: echo %URL% pause exit /b 1 ) echo [INFO] Oeffne Chrome neues Fenster... start "" %CHROME% --new-window "%URL%" REM Kurz warten, dann Konsole schliessen timeout /t 2 /nobreak >nul exit /b 0