03-oct: fix protocolo_apagado.py (ruta clave VPS dinamica + base64 + skip rapido) + changelog
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import base64
|
||||
import subprocess
|
||||
import urllib.request
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Cargar variables de entorno locales (.env de biblioteca_negocio_prolongo)
|
||||
@@ -19,8 +18,9 @@ try:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def cerrar_ventanas():
|
||||
print("1. Cerrando ventanas abiertas (excepto terminal de OpenCode)...")
|
||||
print("1. Cerrando ventanas abiertas (excepto terminal de OpenCode)...", flush=True)
|
||||
ps_command = """
|
||||
$procesos = Get-Process | Where-Object { $_.MainWindowHandle -ne 0 -and $_.ProcessName -notmatch 'OpenCode|WindowsTerminal|cmd|powershell|Code|conhost' }
|
||||
foreach ($p in $procesos) {
|
||||
@@ -32,38 +32,68 @@ def cerrar_ventanas():
|
||||
}
|
||||
"""
|
||||
subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
|
||||
print(" -> Ventanas cerradas.")
|
||||
print(" -> Ventanas cerradas.", flush=True)
|
||||
|
||||
|
||||
def apagar_luces_alexa():
|
||||
"""Apaga las luces 'barras' y 'cuartillo' via HOME ASSISTANT (sustituye a VoiceMonkey)."""
|
||||
print("2. Apagando luces de Alexa via Home Assistant (barras + cuartillo)...")
|
||||
print("2. Apagando luces de Alexa via Home Assistant (barras + cuartillo)...", flush=True)
|
||||
try:
|
||||
from ha_alexa import command
|
||||
except Exception as e:
|
||||
print(f" -> [ERROR] No se pudo cargar ha_alexa: {e}")
|
||||
print(f" -> [ERROR] No se pudo cargar ha_alexa: {e}", flush=True)
|
||||
return
|
||||
for nombre in ("barras", "cuartillo"):
|
||||
ok = command(f"apaga {nombre}")
|
||||
print(f" -> {'OK' if ok else 'ERROR'} al apagar '{nombre}'")
|
||||
print(f" -> {'OK' if ok else 'ERROR'} al apagar '{nombre}'", flush=True)
|
||||
|
||||
|
||||
def apagar_pantalla_tapo():
|
||||
print("3. Apagando pantalla (Tapo) via túnel RPi...")
|
||||
# Ejecutamos el apagado del enchufe enviando el comando a través del VPS a la RPi
|
||||
cmd = '''ssh -i C:\\Users\\juanm\\Documents\\GitHub\\contabo root@185.187.169.109 "ssh -i /root/.ssh/pi_deploy -p 2222 -o StrictHostKeyChecking=no pi@127.0.0.1 'python3 -c \\\"from PyP100 import PyP100; p100 = PyP100.P100(\\'192.168.50.224\\', \\'hermes211@gmail.com\\', \\'Nelo@36666\\'); p100.handshake(); p100.login(); p100.turnOff()\\\"'"'''
|
||||
"""Apaga el enchufe Tapo de la pantalla via cadena local -> VPS -> RPi.
|
||||
|
||||
FIX 03-oct-2026: la clave SSH del VPS estaba hardcodeada a C:\\Users\\juanm (perfil
|
||||
antiguo); ahora es dinamica (~/Documents/Github/contabo). Si no existe o el VPS no
|
||||
responde, se omite rapido en vez de colgarse 15 s. El codigo del RPi va en base64
|
||||
para evitar problemas de anidado de comillas.
|
||||
"""
|
||||
print("3. Apagando pantalla (Tapo) via tunel RPi...", flush=True)
|
||||
key = os.path.expanduser("~/Documents/Github/contabo")
|
||||
if not os.path.exists(key):
|
||||
print(f" -> [SKIP] No existe la clave SSH del VPS: {key}", flush=True)
|
||||
return
|
||||
pycode = (
|
||||
"from PyP100 import PyP100\n"
|
||||
"p100 = PyP100.P100('192.168.50.224', 'hermes211@gmail.com', 'Nelo@36666')\n"
|
||||
"p100.handshake()\n"
|
||||
"p100.login()\n"
|
||||
"p100.turnOff()\n"
|
||||
)
|
||||
b64 = base64.b64encode(pycode.encode("utf-8")).decode("ascii")
|
||||
cmd = (
|
||||
f'ssh -i "{key}" -o BatchMode=yes -o ConnectTimeout=8 root@185.187.169.109 '
|
||||
f'"ssh -i /root/.ssh/pi_deploy -p 2222 -o StrictHostKeyChecking=no -o ConnectTimeout=8 '
|
||||
f"pi@127.0.0.1 'echo {b64} | base64 -d | python3'\""
|
||||
)
|
||||
try:
|
||||
subprocess.run(cmd, shell=True, capture_output=True, timeout=15)
|
||||
print(" -> Pantalla apagada.")
|
||||
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=25)
|
||||
if r.returncode == 0:
|
||||
print(" -> Pantalla apagada.", flush=True)
|
||||
else:
|
||||
detalle = (r.stderr or r.stdout or "").strip().replace("\n", " ")[:180]
|
||||
print(f" -> [SKIP] No se pudo apagar la pantalla (rc={r.returncode}): {detalle}", flush=True)
|
||||
except Exception as e:
|
||||
print(f" -> Error al apagar pantalla: {e}")
|
||||
print(f" -> [SKIP] Error al apagar pantalla: {e}", flush=True)
|
||||
|
||||
|
||||
def shutdown_pc():
|
||||
print("4. Apagando el PC en 10 segundos...")
|
||||
print("4. Apagando el PC en 10 segundos...", flush=True)
|
||||
os.system("shutdown /s /t 10")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=== INICIANDO PROTOCOLO DE APAGADO ===")
|
||||
print("=== INICIANDO PROTOCOLO DE APAGADO ===", flush=True)
|
||||
cerrar_ventanas()
|
||||
apagar_luces_alexa()
|
||||
apagar_pantalla_tapo()
|
||||
shutdown_pc()
|
||||
print("=== PROTOCOLO COMPLETADO ===")
|
||||
print("=== PROTOCOLO COMPLETADO ===", flush=True)
|
||||
|
||||
Reference in New Issue
Block a user