diff --git a/tuya-ipc-terminal/tuya_auth_login.exp b/tuya-ipc-terminal/tuya_auth_login.exp index 15d5ee5..9574e35 100644 --- a/tuya-ipc-terminal/tuya_auth_login.exp +++ b/tuya-ipc-terminal/tuya_auth_login.exp @@ -1,107 +1,58 @@ -#!/usr/bin/expect -f +#!/usr/bin/with-contenv bashio -# Entferne den alten Konfigurationsordner, um eine Neu-Authentifizierung zu erzwingen. -set config_dir "/config/.tuya-data" -if { [file isdirectory $config_dir] } { - puts "Alter Konfigurationsordner gefunden. Wird entfernt, um Neu-Authentifizierung zu erzwingen." - if {[catch {file delete -force -- $config_dir} err]} { - puts stderr "Fehler: Konnte den existierenden Ordner '$config_dir' nicht löschen: $err" - exit 1 - } -} +# Konfiguration lesen +readonly TUYA_EMAIL=$(bashio::config 'TUYA_EMAIL') +readonly TUYA_PASSWORD=$(bashio::config 'TUYA_PASSWORD') +readonly TUYA_REGION=$(bashio::config 'TUYA_REGION') -puts "Starte den Authentifizierungsprozess..." - -set timeout 30 - -# Lese die Eingaben (Region, E-Mail, Passwort) von stdin -gets stdin region -if { [eof stdin] || $region eq "" } { - puts stderr "Fehler: Region konnte nicht von stdin gelesen werden oder ist leer." +# 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 -gets stdin email -if { [eof stdin] || $email eq "" } { - puts stderr "Fehler: E-Mail konnte nicht von stdin gelesen werden oder ist leer." +# --- 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 -gets stdin password -if { [eof stdin] || $password eq "" } { - puts stderr "Fehler: Passwort konnte nicht von stdin gelesen werden oder ist leer." - exit 1 -} +# --- Start: Watchdog-Schleife --- +bashio::log.info "Starte den Watchdog zur Überwachung der Sitzung." +while true; do + # Warte für 15 Minuten + sleep 900 -# --- KORRIGIERTE ZEILE --- -# Starte den Befehl zur Authentifizierung mit dem korrekten Sub-Befehl 'auth' -spawn tuya-ipc-terminal auth add $region $email --password -# --- ENDE DER KORREKTUR --- + bashio::log.info "Watchdog: Überprüfe den Status der Tuya-Sitzung..." + RESPONSE=$(tuya-ipc-terminal cameras refresh 2>&1) -# Behandele die Interaktion mit dem Prozess -expect { - "Password:" { - send "$password\r" - exp_continue - } - "Enter password:" { - send "$password\r" - exp_continue - } - "Please enter your password:" { - send "$password\r" - exp_continue - } - "Authentication successful" { - puts "Authentifizierung erfolgreich abgeschlossen!" - catch {expect eof} - catch {wait} - exit 0 - } - "Login successful" { - puts "Login erfolgreich abgeschlossen!" - catch {expect eof} - catch {wait} - exit 0 - } - "Authentication completed" { - puts "Authentifizierung abgeschlossen!" - catch {expect eof} - catch {wait} - exit 0 - } - "Invalid credentials" { - puts stderr "Authentifizierung fehlgeschlagen: Ungültige Anmeldedaten" - catch {expect eof} - catch {wait} + 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}" exit 1 - } - "Login failed" { - puts stderr "Authentifizierung fehlgeschlagen: Login fehlgeschlagen" - catch {expect eof} - catch {wait} + elif ! ps -p "${RTSP_PID}" > /dev/null; then + bashio::log.error "Watchdog: RTSP-Server-Prozess nicht gefunden! Starte das Addon neu." exit 1 - } - "Authentication failed" { - puts stderr "Authentifizierung fehlgeschlagen" - catch {expect eof} - catch {wait} - exit 1 - } - eof { - set exit_code [lindex [wait] 2] - if { $exit_code == 0 } { - puts "Prozess beendet, vermutlich erfolgreich." - exit 0 - } else { - puts stderr "Prozess unerwartet beendet mit Exit-Code $exit_code." - exit 1 - } - } - timeout { - puts stderr "Zeitüberschreitung beim Authentifizierungsprozess" - catch {expect eof} - catch {wait} - exit 1 - } -} + else + bashio::log.info "Watchdog: Sitzung ist gültig und RTSP-Server läuft." + fi +done