diff --git a/tuya-ipc-terminal/tuya_auth_login.exp b/tuya-ipc-terminal/tuya_auth_login.exp index 9574e35..3bf58f5 100644 --- a/tuya-ipc-terminal/tuya_auth_login.exp +++ b/tuya-ipc-terminal/tuya_auth_login.exp @@ -1,58 +1,72 @@ -#!/usr/bin/with-contenv bashio +#!/usr/bin/expect -f -# Konfiguration lesen -readonly TUYA_EMAIL=$(bashio::config 'TUYA_EMAIL') -readonly TUYA_PASSWORD=$(bashio::config 'TUYA_PASSWORD') -readonly TUYA_REGION=$(bashio::config 'TUYA_REGION') +# --- START SUPER-DEBUG --- +# Aktiviere das interne Debugging von Expect. Dies erzeugt SEHR viel Output. +exp_internal 1 +puts "DEBUG: Expect-Skript gestartet. Internes Debugging ist aktiviert." +# --- ENDE SUPER-DEBUG --- -# Prüfen, ob Konfiguration vorhanden ist -if [[ -z "${TUYA_EMAIL}" || -z "${TUYA_PASSWORD}" ]]; then - bashio::log.fatal "Tuya-E-Mail und Passwort müssen konfiguriert sein." - exit 1 -fi - -# --- START: Authentifizierung und Kamera-Sync --- -bashio::log.info "Authentifizierung bei Tuya..." -printf "%s\n%s\n%s\n" "${TUYA_REGION}" "${TUYA_EMAIL}" "${TUYA_PASSWORD}" | /tuya_auth_login.exp - -# --- NEU: Kamera-Refresh erzwingen --- -bashio::log.info "Synchronisiere Kameraliste mit Tuya-Cloud..." -# Dieser Befehl sorgt dafür, dass die lokale Konfiguration mit den Kameras aus Ihrem Account gefüllt wird. -tuya-ipc-terminal cameras refresh -# Eine kurze Pause kann helfen, damit alle Prozesse synchron sind. -sleep 5 -# --- ENDE NEU --- - -# Starte den RTSP-Server im Hintergrund -bashio::log.info "Starte den Tuya RTSP-Server im Hintergrund..." -tuya-ipc-terminal rtsp start --port 8554 & -# Speichere die Prozess-ID (PID) des RTSP-Servers -RTSP_PID=$! -bashio::log.info "RTSP-Server gestartet mit PID ${RTSP_PID}." - -# Überprüfe, ob der Prozess noch läuft -if ! ps -p "${RTSP_PID}" > /dev/null; then - bashio::log.fatal "RTSP-Server konnte nicht gestartet werden. Prüfen Sie das Log auf Fehler wie 'no cameras found'." - exit 1 -fi - -# --- Start: Watchdog-Schleife --- -bashio::log.info "Starte den Watchdog zur Überwachung der Sitzung." -while true; do - # Warte für 15 Minuten - sleep 900 - - bashio::log.info "Watchdog: Überprüfe den Status der Tuya-Sitzung..." - RESPONSE=$(tuya-ipc-terminal cameras refresh 2>&1) - - if echo "${RESPONSE}" | grep -q -E "401|USER_SESSION_LOSS|Not login"; then - bashio::log.error "Watchdog: Abgelaufene Sitzung erkannt! Starte das Addon neu." - kill "${RTSP_PID}" +# Entferne den alten Konfigurationsordner, um eine Neu-Authentifizierung zu erzwingen. +set config_dir "/config/.tuya-data" +puts "DEBUG: Prüfe auf Verzeichnis: $config_dir" +if { [file isdirectory $config_dir] } { + puts "DEBUG: Altes Konfigurationsverzeichnis gefunden. Wird entfernt." + if {[catch {file delete -force -- $config_dir} err]} { + puts stderr "FATAL: Konnte existierendes Verzeichnis '$config_dir' nicht löschen: $err" exit 1 - elif ! ps -p "${RTSP_PID}" > /dev/null; then - bashio::log.error "Watchdog: RTSP-Server-Prozess nicht gefunden! Starte das Addon neu." + } + puts "DEBUG: Verzeichnis erfolgreich entfernt." +} else { + puts "DEBUG: Kein altes Konfigurationsverzeichnis gefunden. Fortfahren." +} + +puts "DEBUG: Starte den Authentifizierungsprozess..." +set timeout 60 + +# Lese die Eingaben +puts "DEBUG: Lese Region von stdin..." +gets stdin region +puts "DEBUG: Region gelesen: $region" + +puts "DEBUG: Lese Email von stdin..." +gets stdin email +puts "DEBUG: Email gelesen: $email" + +puts "DEBUG: Lese Passwort von stdin..." +gets stdin password +# Das Passwort selbst nicht loggen +puts "DEBUG: Passwort wurde gelesen." + +if { $region eq "" || $email eq "" || $password eq "" } { + puts stderr "FATAL: Eine der Eingaben (Region, Email, Passwort) ist leer." + exit 1 +} + +puts "DEBUG: Starte 'spawn tuya-ipc-terminal auth add ...'" +spawn tuya-ipc-terminal auth add $region $email --password + +# Interaktion +expect { + "Password:" { + puts "DEBUG: 'Password:' Prompt erkannt. Sende Passwort..." + send "$password\r" + puts "DEBUG: Passwort gesendet." + exp_continue + } + "Authentication successful" { + puts "DEBUG: Erfolg-Nachricht 'Authentication successful' erkannt." + catch {expect eof} + catch {wait} + exit 0 + } + eof { + puts "DEBUG: 'eof' (End of File) vom Prozess erreicht." + set exit_code [lindex [wait] 2] + puts "DEBUG: Prozess beendet mit Exit-Code $exit_code." + exit $exit_code + } + timeout { + puts stderr "FATAL: Timeout nach 60 Sekunden." exit 1 - else - bashio::log.info "Watchdog: Sitzung ist gültig und RTSP-Server läuft." - fi -done + } +}