100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
import os
|
|
import sys
|
|
import base64
|
|
import subprocess
|
|
from dotenv import load_dotenv
|
|
|
|
# Cargar variables de entorno locales (.env de biblioteca_negocio_prolongo)
|
|
dotenv_path = os.path.expanduser("~/Documents/GitHub/biblioteca_negocio_prolongo/.env")
|
|
load_dotenv(dotenv_path)
|
|
|
|
# Permitir importar modulos del repo de negocio (ha_alexa).
|
|
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
if _SCRIPT_DIR not in sys.path:
|
|
sys.path.insert(0, _SCRIPT_DIR)
|
|
try:
|
|
import infra_paths
|
|
infra_paths.bootstrap_biblioteca_paths()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def cerrar_ventanas():
|
|
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) {
|
|
try {
|
|
$p.CloseMainWindow() | Out-Null
|
|
Start-Sleep -Milliseconds 200
|
|
if (-not $p.HasExited) { $p.Kill() }
|
|
} catch {}
|
|
}
|
|
"""
|
|
subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
|
|
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)...", flush=True)
|
|
try:
|
|
from ha_alexa import command
|
|
except Exception as 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}'", flush=True)
|
|
|
|
|
|
def apagar_pantalla_tapo():
|
|
"""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:
|
|
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" -> [SKIP] Error al apagar pantalla: {e}", flush=True)
|
|
|
|
|
|
def shutdown_pc():
|
|
print("4. Apagando el PC en 10 segundos...", flush=True)
|
|
os.system("shutdown /s /t 10")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=== INICIANDO PROTOCOLO DE APAGADO ===", flush=True)
|
|
cerrar_ventanas()
|
|
apagar_luces_alexa()
|
|
apagar_pantalla_tapo()
|
|
shutdown_pc()
|
|
print("=== PROTOCOLO COMPLETADO ===", flush=True)
|