# -*- coding: utf-8 -*-
"""
================================================================================
INSTRUCCIONES DE COMPILACIÓN Y USO (PORTABILIDAD Y ARQUITECTURA)
================================================================================
Este script puede ser empaquetado en un único archivo ejecutable binario (.exe en Windows
o binario nativo en Linux) que se puede ejecutar en cualquier máquina de su misma
arquitectura sin necesidad de instalar Python o dependencias adicionales.
--------------------------------------------------------------------------------
1. REQUISITOS PREVIOS (Instalación de Python y PyInstaller)
--------------------------------------------------------------------------------
Si la máquina de desarrollo NO tiene instalado Python o PyInstaller:
A) Instalar Python:
- Windows: Descargue el instalador desde https://www.python.org/
Asegúrese de marcar la casilla "Add Python.exe to PATH" durante la instalación.
- Linux (Debian/Ubuntu): Viene preinstalado. Si no, instálelo usando:
sudo apt update && sudo apt install python3 python3-pip python3-venv
B) Instalar Dependencias del Script:
Abra la terminal (cmd/PowerShell en Windows, o bash en Linux) y ejecute:
pip install total-connect-client pyinstaller tzdata
--------------------------------------------------------------------------------
2. COMANDOS DE COMPILACIÓN
--------------------------------------------------------------------------------
Ejecute el comando correspondiente según el sistema operativo en la carpeta del script:
A) En Windows:
- Para generar un ejecutable estándar con ventana negra de consola:
python -m PyInstaller --onefile --name totalconnect index.py
- Para generar un ejecutable silencioso en segundo plano (sin ventana negra):
python -m PyInstaller --onefile --noconsole --name totalconnect index.py
B) En Linux:
- Para generar el ejecutable binario nativo de Linux:
python3 -m PyInstaller --onefile --name totalconnect index.py
--------------------------------------------------------------------------------
3. INSTRUCCIONES DE USO DEL EJECUTABLE
--------------------------------------------------------------------------------
Una vez compilado, busque el ejecutable generado dentro de la carpeta "dist/".
- Mueva o copie el ejecutable a la carpeta que desee.
- Copie el archivo 'credentials.json' a esa misma carpeta (al lado del ejecutable).
- Estatus del Servidor Web (Inicia Servidor Web en http://localhost:PORT/):
- Windows: Doble clic en 'totalconnect.exe' (se iniciará como servidor y abrirá el navegador de inmediato).
- Linux: ./totalconnect
- El arranque es INSTANTÁNEO. Si no hay reporte previo ('status.html'), genera un placeholder limpio indicando que debe hacer clic en "Actualizar" para su primera carga.
- Parámetros CLI de Navegador: Puede usar '--no-browser' o '-nb' para evitar que se abra la ventana del navegador.
- Ejecución Única (On-Demand / Guarda HTML y sale):
- Ejecutar agregando el argumento '--once' o '-o' (ej: totalconnect.exe --once o ./totalconnect -o)
- Sirve para programar el script mediante el Programador de Tareas en Windows o el demonio Cron en Linux.
--------------------------------------------------------------------------------
4. FORMATOS SOPORTADOS PARA 'credentials.json'
--------------------------------------------------------------------------------
El archivo 'credentials.json' debe colocarse junto al ejecutable y soporta dos formatos:
Formato A (Formato por objeto con puerto, exclusiones y navegador - RECOMENDADO):
{
"port": 8080,
"exclude": ["1071", "1171", "1215"], // Lista de sucursales a ignorar (ID, número o nombre)
"open_browser": true, // true para abrir navegador al iniciar (defecto en GUI)
"accounts": [
{
"label": "2020",
"username": "TuUsuario2020",
"password": "TuPassword2020"
},
{
"label": "2019",
"username": "TuUsuario2019",
"password": "TuPassword2019"
}
]
}
Formato B (Formato original de lista - Puerto por defecto 8080):
[
{
"label": "2020",
"username": "TuUsuario2020",
"password": "TuPassword2020"
},
{
"label": "2019",
"username": "TuUsuario2019",
"password": "TuPassword2019"
}
]
--------------------------------------------------------------------------------
5. MOTOR DE POLLING HÍBRIDO Y AUDITORÍA EN VIVO (Mantenimiento Inteligente)
--------------------------------------------------------------------------------
Este script implementa un motor de polling de alto rendimiento optimizado contra rate-limits:
A) Motor Dinámico Híbrido (Lote 27 + 7 por ciclo de 30s):
- Cada 30 segundos, consulta a 27 sucursales del turno ordinario (ordenadas de forma
persistente por antigüedad de verificación) + hasta 7 de alta frecuencia (paneles
desarmados, en alarma o con fallas físicas).
- Realiza un Poll Lite SOAP (GetLiveEvents) rápido si la antigüedad de datos es menor
a 10 minutos. Si detecta actividad física, programa un Poll Full REST (fullStatus)
para el siguiente ciclo de 30s.
- Realiza un Poll Full REST de mantenimiento automático si la antigüedad supera los
10 minutos, garantizando coherencia de datos con Honeywell.
- Limitador integrado de velocidad de máximo 35 Polls Full por ciclo para prevenir
bloqueos de cuenta en arranques en frío.
B) Visor de Logs y Autogestión de Almacenamiento:
- Se incluye un botón interactivo "Ver Log" en la cabecera del dashboard para abrir
una terminal oscura en el navegador con las últimas 150 líneas del log de actividad.
- Rotación automática de 'activity.log' en caliente al alcanzar los 5 MB de tamaño,
preservando un respaldo anterior ('activity.log.1').
- En producción, es recomendable iniciar el daemon silenciosamente en Linux:
nohup ./venv/bin/python3 index.py > /dev/null 2> server.log &
Esto descarta la consola redundante (que ya va a activity.log) y mantiene
'server.log' ocupado únicamente con tracebacks de error reales del sistema.
C) Reinicio de Fábrica (Hard Reset) por Clic Largo (3 segundos):
- Al mantener presionado el botón "Actualizar" por 3 segundos completos, se ejecuta
un Hard Reset (limpieza de cachés de disco, purga de memoria de sesiones de clientes
y reinicio de escaneo de cero).
- Ergonomía del botón:
* Clic Rápido (<300ms): Ejecuta un refresco clásico en caliente de la UI.
* Clic Medio / Cancelado (300ms a 3000ms): Cancela de forma silenciosa ("aquí no pasó nada").
* Retraso Visual de 1s: El botón no se pinta en rojo ni cambia su texto hasta cruzar
el primer segundo (1000ms) de presión continua, evitando destellos molestos en
clics habituales.
================================================================================
"""
import os
import sys
import json
import time
import datetime
import threading
import http.server
import socketserver
import webbrowser
import platform
import logging
from socketserver import ThreadingMixIn
from zoneinfo import ZoneInfo
import requests
import xml.etree.ElementTree as ET
from total_connect_client.client import TotalConnectClient
from total_connect_client.const import ArmingState, make_http_endpoint
# Silenciar advertencias internas irrelevantes de la librería TotalConnect Client
logging.getLogger("total_connect_client").setLevel(logging.ERROR)
# Configurar codificación UTF-8 para la consola (evita UnicodeEncodeError con emojis en Windows)
if sys.stdout is not None:
try:
sys.stdout.reconfigure(encoding='utf-8')
except Exception:
pass
if sys.stderr is not None:
try:
sys.stderr.reconfigure(encoding='utf-8')
except Exception:
pass
# Directorio del script (soporta empaquetado con PyInstaller)
if getattr(sys, 'frozen', False):
BASE_DIR = os.path.dirname(sys.executable)
else:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CREDENTIALS_FILE = os.path.join(BASE_DIR, 'credentials.json')
HTML_OUTPUT_FILE = os.path.join(BASE_DIR, 'status.html')
CACHE_FILE = os.path.join(BASE_DIR, 'locations_cache.json')
LAST_STATE_FILE = os.path.join(BASE_DIR, 'last_state.json')
LAST_EVENT_IDS_FILE = os.path.join(BASE_DIR, 'last_event_ids.json')
LOG_FILE = os.path.join(BASE_DIR, 'activity.log')
polling_trigger_event = threading.Event()
soap_disabled_accounts = set()
soap_session_ids = {} # dict: username -> GUID SessionId para SOAP GetLiveEvents
def load_last_event_ids():
"""Carga los últimos IDs de eventos SOAP guardados en disco."""
if os.path.exists(LAST_EVENT_IDS_FILE):
try:
with open(LAST_EVENT_IDS_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
if isinstance(data, dict):
return data
except Exception as e:
log_msg(f"⚠️ No se pudieron cargar los IDs de eventos: {e}")
return {}
def save_last_event_ids(event_ids):
"""Guarda los últimos IDs de eventos SOAP en disco."""
try:
with open(LAST_EVENT_IDS_FILE, 'w', encoding='utf-8') as f:
json.dump(event_ids, f, ensure_ascii=False, indent=2)
except Exception as e:
log_msg(f"⚠️ No se pudieron guardar los IDs de eventos: {e}")
def load_polling_state():
"""Carga el estado del polling (activo/pausado) exclusivamente desde credentials.json."""
if os.path.exists(CREDENTIALS_FILE):
try:
with open(CREDENTIALS_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
if isinstance(data, dict):
return data.get("polling_enabled", True)
except Exception:
pass
return True
def save_polling_state(enabled):
"""Guarda el estado del polling (activo/pausado) exclusivamente en credentials.json."""
target_files = [CREDENTIALS_FILE]
if platform.system() == "Windows":
target_files += [
r"Z:\data2\Software\Bot-WhatsApp\TotalConnect\credentials.json",
r"E:\Software\TotalConnect\credentials.json",
r"E:\Software\TotalConnect\dist\credentials.json"
]
for target_path in set(target_files):
if os.path.exists(target_path):
try:
with open(target_path, 'r', encoding='utf-8') as f:
creds_data = json.load(f)
if isinstance(creds_data, dict):
creds_data["polling_enabled"] = enabled
with open(target_path, 'w', encoding='utf-8') as f:
json.dump(creds_data, f, ensure_ascii=False, indent=2)
log_msg(f"💾 Estado de polling ({enabled}) guardado en: {target_path}")
except Exception as e:
log_msg(f"⚠️ No se pudo actualizar polling_enabled en {target_path}: {e}")
polling_enabled = load_polling_state()
def log_msg(msg):
"""Escribe un mensaje de log con timestamp tanto en la consola (con flush) como en activity.log."""
try:
now_str = datetime.datetime.now(ZoneInfo("America/Mexico_City")).strftime('%Y-%m-%d %I:%M:%S %p')
formatted = f"[{now_str}] {msg}"
except Exception:
formatted = f"[LOG] {msg}"
print(formatted, flush=True)
# Rotar activity.log si supera los 5 MB
try:
if os.path.exists(LOG_FILE) and os.path.getsize(LOG_FILE) > 5 * 1024 * 1024:
backup_file = LOG_FILE + ".1"
if os.path.exists(backup_file):
os.remove(backup_file)
os.rename(LOG_FILE, backup_file)
except Exception:
pass
try:
with open(LOG_FILE, 'a', encoding='utf-8') as f:
f.write(formatted + '\n')
except Exception:
pass
update_lock = threading.Lock()
current_loading_status = ""
polling_lock = threading.Lock()
last_event_ids = load_last_event_ids()
consecutive_errors = {}
last_poll_timestamp = "Nunca"
client_pool = {}
client_pool_lock = threading.Lock()
README_CONTENT = """# TotalConnect 2.0 - Panel de Monitoreo de Alarmería
Este proyecto proporciona un panel web centralizado, responsivo y de alto rendimiento para el monitoreo consolidado de múltiples cuentas de alarmas de TotalConnect 2.0 (Resideo/Honeywell). Está diseñado para ejecutarse nativamente tanto en Linux como en Windows y generar reportes visuales dinámicos.
---
## 🛠️ Requerimientos del Sistema y Servicios
### 1. Sistema Operativo y Servidor
* **Linux (Debian/Ubuntu/Proxmox VM o similar):** Probado y validado en Debian/Ubuntu con Python 3.11.
* **Windows (7/10/11 o Windows Server):** Ejecutable mediante el binario autónomo `totalconnect.exe` (compilado con PyInstaller).
* **Acceso de Red:** El host debe tener salida a Internet (para conectarse al API de Total Connect) y ser accesible en la red local (para visualizar el panel en el puerto configurado).
### 2. Servicios de Sistema Recomendados
* **En Linux:** Servicio Systemd (`totalconnect.service`) configurado para mantener el servidor web encendido de forma persistente y asegurar su auto-recuperación.
* **En Windows:** Ejecutar el binario en segundo plano o utilizar el Programador de Tareas para actualizaciones silenciosas.
---
## 📦 Dependencias de Python
Si ejecutas el script directamente desde el código fuente (`index.py`), se requiere:
1. **`total-connect-client`:** Librería principal para interactuar con la API REST y OAuth2 de Total Connect.
2. **`tzdata`:** Requerido en Windows para soportar zonas horarias locales (`America/Mexico_City`).
*Nota: Si utilizas el ejecutable compilado `totalconnect.exe`, estas dependencias ya vienen empaquetadas en su interior.*
---
## 📂 Estructura de Archivos del Proyecto
* **`totalconnect.exe` / `totalconnect`:** El binario ejecutable autónomo.
* **`index.py`:** El script principal de código fuente en Python.
* **`credentials.json`:** Archivo confidencial que contiene las credenciales y el puerto HTTP.
* **`locations_cache.json`:** Historial temporal auto-generado de nombres de sucursales para soporte sin conexión.
* **`status.html`:** Reporte final auto-generado.
* **`activity.log`:** Archivo de logs de la aplicación.
* **`activity.log.1`:** Respaldo anterior del log (se rota automáticamente al superar los 5 MB).
* **`server.log`:** Registro de errores del sistema del comando de arranque en segundo plano.
---
## 🚀 Modos de Ejecución y Guía rápida
### Modo 1: Servidor Web Continuo (Por Defecto)
Arranca el Servidor HTTP integrado. Lee el puerto del archivo `credentials.json` (por defecto `8080`).
* **En Windows:** Haz doble clic en `totalconnect.exe` o corre en consola: `totalconnect.exe`
* **En Linux:** Ejecuta `./totalconnect`
* **Acceso:** Abre tu navegador en `http://localhost:PORT/` o `http://IP_DEL_SERVIDOR:PORT/`
### Modo 2: Actualización Única (On-Demand / Tareas Programadas)
Consulta los estados una sola vez, genera `status.html` y finaliza. Ideal para automatizar con **Cron (Linux)** o **Programador de Tareas (Windows)**.
* **Comando:** Agregar el argumento `--once` o `-o` (ej: `totalconnect.exe --once` o `./totalconnect -o`).
---
## 🔒 Robustez y Resiliencia Implementada
* **Motor Híbrido Dinámico (27 + 7):** En cada ciclo de 30s se procesan 27 sucursales del turno ordinario (ordenadas secuencialmente por antigüedad) + hasta 7 de alta frecuencia (desarmadas/alarmadas/fallos), previniendo rate-limits.
* **Decisión Inteligente (SOAP vs REST):** Realiza un Poll Lite SOAP (GetLiveEvents) rápido si los datos de la sucursal tienen menos de 10 minutos. Si detecta actividad física, programa un Poll Full REST (fullStatus) para la siguiente vuelta.
* **Tope de Consultas REST:** Limitador integrado de máximo 35 consultas REST por ciclo para un arranque en frío seguro sin bloqueos de cuenta.
* **Visor de Logs Interactiva (150 líneas):** Botón "Ver Log" en el dashboard para auditar el servidor en vivo directamente desde el navegador.
* **Autogestión de Espacio:** Rotación automática de logs a los 5 MB de tamaño.
* **Arranque Silencioso en Producción (VM):**
`nohup ./venv/bin/python3 index.py > /dev/null 2> server.log &`
Descarta prints de consola duplicados y deja `server.log` activo únicamente para atrapar excepciones de crash.
* **Reinicio de Fábrica (Hard Reset) por Clic Largo (3s):**
Al mantener presionado el botón "Actualizar" por 3 segundos, se purga la memoria y base de datos física para resolver inconsistencias. Cuenta con retraso de 1 segundo en el cambio de color para evitar destellos y cancelación silenciosa ergonómica si te arrepientes antes de completarse los 3 segundos.
"""
def auto_generate_docs():
"""Genera automáticamente el archivo README.md y credentials.json si no existen."""
# Generar README.md si no existe
readme_path = os.path.join(BASE_DIR, 'README.md')
if not os.path.exists(readme_path):
try:
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(README_CONTENT)
print("📝 Se ha creado el archivo de documentación README.md de forma automática.")
except Exception as e:
print(f"⚠️ No se pudo auto-generar README.md: {e}")
# Generar plantilla de credentials.json si no existe
if not os.path.exists(CREDENTIALS_FILE):
dummy_credentials = {
"port": 8080,
"accounts": [
{
"label": "CUENTA_EJEMPLO",
"username": "TuUsuarioTotalConnect",
"password": "TuPasswordTotalConnect"
}
]
}
try:
with open(CREDENTIALS_FILE, 'w', encoding='utf-8') as f:
json.dump(dummy_credentials, f, ensure_ascii=False, indent=2)
print(f"🔑 Se ha creado una plantilla de credenciales en: {CREDENTIALS_FILE}")
print(" Por favor edita este archivo con tus credenciales reales de TotalConnect.")
except Exception as e:
print(f"⚠️ No se pudo crear la plantilla de credenciales: {e}")
def parse_arming_state(state):
"""Mapea los estados de ArmingState de total-connect-client a etiquetas legibles."""
if isinstance(state, str) and state == "REVOKED":
return {
"label": "Token Revocado / Error",
"class": "state-revoked",
"badge": "Desconectado"
}
if isinstance(state, int):
try:
state = ArmingState(state)
except Exception:
state = ArmingState.UNKNOWN
if hasattr(state, 'is_disarmed') and state.is_disarmed():
if state == ArmingState.DISARMED_ZONE_FAULTED:
return {
"label": "Desarmado (Zona Abierta / Fallo)",
"class": "state-fault",
"badge": "Revisar"
}
return {
"label": "Desarmado",
"class": "state-disarmed",
"badge": "Listo"
}
elif hasattr(state, 'is_armed_away') and state.is_armed_away():
return {
"label": "Armado Away",
"class": "state-armed-away",
"badge": "Armado (Away)"
}
elif hasattr(state, 'is_armed_home') and (state.is_armed_home() or state.is_armed_night() or state.is_armed_custom_bypass()):
return {
"label": "Armado Stay",
"class": "state-armed-stay",
"badge": "Armado (Stay)"
}
elif hasattr(state, 'is_triggered') and state.is_triggered():
return {
"label": "¡ALARMA ACTIVA!",
"class": "state-alarm",
"badge": "¡ALERTA!"
}
elif hasattr(state, 'is_pending') and state.is_pending():
return {
"label": "Procesando Cambio...",
"class": "state-pending",
"badge": "Pendiente"
}
else:
state_name = getattr(state, 'name', str(state))
return {
"label": f"Desconocido ({state_name})",
"class": "state-unknown",
"badge": "Desconocido"
}
def format_trigger_time(raw_time_str):
"""Formatea la hora de disparo de alarma. Si contiene 'Z', convierte de UTC a CDMX. Si no, formatea la hora local existente."""
if not raw_time_str:
return ""
try:
s = str(raw_time_str).strip()
if 'T' in s:
if s.endswith('Z'):
clean_str = s.rstrip('Z')
dt = datetime.datetime.fromisoformat(clean_str).replace(tzinfo=datetime.timezone.utc)
dt_cdmx = dt.astimezone(ZoneInfo("America/Mexico_City"))
return f" @ {dt_cdmx.strftime('%I:%M:%S %p')}"
else:
dt = datetime.datetime.fromisoformat(s)
return f" @ {dt.strftime('%I:%M:%S %p')}"
return f" @ {s}"
except Exception:
try:
return f" @ {str(raw_time_str).split('T')[1].rstrip('Z')}"
except Exception:
return f" @ {raw_time_str}"
def _get_location_sort_priority(loc_item):
"""Calcula la prioridad de ordenamiento de una sucursal según su estado de armado."""
state = loc_item['status']
if isinstance(state, str) and state == "REVOKED":
return 3
if isinstance(state, int):
try:
state = ArmingState(state)
except Exception:
state = ArmingState.UNKNOWN
if hasattr(state, 'is_triggered') and state.is_triggered():
return 0
if hasattr(state, 'is_disarmed') and state.is_disarmed():
return 1
return 2
def generate_html_report(data):
"""Genera un archivo HTML premium, limpio, claro y compacto con el estatus consolidado."""
now = datetime.datetime.now(ZoneInfo("America/Mexico_City"))
dias = ["lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo"]
meses = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]
timestamp = f"{dias[now.weekday()]}, {now.day} de {meses[now.month - 1]} de {now.year} - {now.strftime('%I:%M:%S %p')}"
# Generar checkboxes dinámicos para las cuentas de credentials.json
checkboxes_html = ""
accounts_cfg, _, _, _ = load_credentials_config()
if accounts_cfg:
for acc in accounts_cfg:
label = acc.get("label", "")
checkboxes_html += f"""
"""
# Consolidar todas las sucursales en una sola lista plana
all_locations = []
for account in data:
for loc in account['locations']:
all_locations.append({
"account_label": account['label'],
"id": loc['id'],
"name": loc['name'],
"status": loc['status'],
"triggered_partitions": loc.get('triggered_partitions', []),
"active_zones": loc.get('active_zones', []),
"last_download_time": loc.get('last_download_time'),
"last_update_time": loc.get('last_update_time')
})
# Ordenar prioritariamente:
# 0: Alarmas activas (¡ALARMA ACTIVA!)
# 1: Desarmadas (sucursales abiertas)
# 2: Armadas (Stay, Away, etc.)
# 3: Revocadas/Desconectadas (con error de token / caché)
# Dentro de cada grupo se ordena de forma secuencial por número/nombre.
all_locations.sort(key=lambda x: (_get_location_sort_priority(x), x['name']))
cards_html = ""
if not all_locations:
cards_html = """
⏳
Sin datos de sucursales
Haz clic en el botón "Actualizar" arriba para conectar a los paneles de alarma por primera vez.
"""
else:
for loc in all_locations:
state = parse_arming_state(loc['status'])
# Generar detalles adicionales si la sucursal está alarmada o tiene zonas abiertas/fallos
details_html = ""
if loc["triggered_partitions"]:
parts_str = ", ".join(loc["triggered_partitions"])
details_html += f"""
🚨 Alarma en: {parts_str}
"""
if loc["active_zones"]:
zones_list_html = ""
for z in loc["active_zones"]:
badge_cls = "zone-alarm" if z["status"] == "Alarma" else "zone-open"
# Formatear la hora de activación si existe y es alarma
time_str = ""
if z["status"] == "Alarma" and z.get("trigger_time"):
time_str = format_trigger_time(z["trigger_time"])
zones_list_html += f"""
{z["status"]}
{z["name"]} (Z{z["id"]}){time_str}
"""
details_html += f"""
Zonas activas / fallos:
{zones_list_html}
"""
card_class = "branch-card"
if isinstance(loc['status'], str) and loc['status'] == "REVOKED":
card_class += " branch-card-disabled"
dl_time = loc.get('last_download_time') or 'Nunca'
up_time = loc.get('last_update_time') or 'Nunca'
footer_html = f"""
"""
cards_html += f"""
{state['label']}
{loc['account_label']}
🏢 {loc['name']}
{details_html}
{footer_html}
"""
html_content = f"""
Estatus de Sucursales - TotalConnect
TotalConnect 2.0
Monitoreo centralizado de alarmas
Cuentas:
{checkboxes_html}
Última Actualización
{timestamp}
{cards_html}
Log de Actividad del Servidor (Últimas 150 líneas)
Cargando bitácora de actividad...
Actualizando Estatus
Conectando a los paneles de alarma... Esto tomará aproximadamente 1-2 minutos.
"""
with open(HTML_OUTPUT_FILE, 'w', encoding='utf-8') as f:
f.write(html_content)
log_msg(f"🎉 Reporte HTML premium generado con éxito en: {HTML_OUTPUT_FILE}")
class DashboardHandler(http.server.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
# Silenciar logs de peticiones de red ordinarias en consola/stderr
pass
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
self.end_headers()
try:
with open(HTML_OUTPUT_FILE, 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
except Exception as e:
self.wfile.write(f"Error cargando el archivo: {e}".encode('utf-8'))
elif self.path == '/activity-log':
self.send_response(200)
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
self.end_headers()
try:
if os.path.exists(LOG_FILE):
with open(LOG_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Devolver las últimas 150 líneas de actividad para la modal
last_lines = "".join(lines[-150:])
self.wfile.write(last_lines.encode('utf-8'))
else:
self.wfile.write("No hay actividad registrada aún.".encode('utf-8'))
except Exception as e:
self.wfile.write(f"Error al leer activity.log: {e}".encode('utf-8'))
elif self.path == '/current-loading':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
self.wfile.write(json.dumps({"status": current_loading_status}).encode('utf-8'))
elif self.path == '/polling-status':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
data = {
"enabled": polling_enabled,
"last_poll": last_poll_timestamp,
"loading_status": current_loading_status
}
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
# Comportamiento predeterminado para otros archivos
super().do_GET()
def do_POST(self):
if self.path == '/refresh':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# Leer el cuerpo del POST si existe
content_length = int(self.headers.get('Content-Length', 0))
selected_labels = None
if content_length > 0:
try:
post_data = self.rfile.read(content_length)
body_json = json.loads(post_data.decode('utf-8'))
selected_labels = body_json.get('accounts')
except Exception as e:
print(f"⚠️ Error al parsear cuerpo de solicitud POST: {e}")
# Intentar adquirir el lock en modo no-bloqueante
acquired = update_lock.acquire(blocking=False)
if not acquired:
self.wfile.write(json.dumps({
"status": "error",
"message": "Ya hay una actualización en curso en el servidor. Por favor espera a que termine la anterior."
}).encode('utf-8'))
return
try:
# Cargar configuración completa
accounts_cfg, _, exclude_cfg, _ = load_credentials_config()
# Filtrar si el usuario seleccionó específicas
if selected_labels is not None:
accounts_to_run = [acc for acc in accounts_cfg if acc.get('label') in selected_labels]
else:
accounts_to_run = accounts_cfg
success = run_update(accounts=accounts_to_run, exclude=exclude_cfg, full_refresh=True)
if success:
self.wfile.write(json.dumps({"status": "success"}).encode('utf-8'))
else:
self.wfile.write(json.dumps({"status": "error", "message": "No se pudieron recuperar datos de ninguna cuenta."}).encode('utf-8'))
except Exception as e:
self.wfile.write(json.dumps({"status": "error", "message": str(e)}).encode('utf-8'))
finally:
update_lock.release()
elif self.path == '/reset-cache':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
acquired = update_lock.acquire(blocking=False)
if not acquired:
self.wfile.write(json.dumps({
"status": "error",
"message": "Ya hay una actualización en curso en el servidor. Por favor espera."
}).encode('utf-8'))
return
try:
# 1. Limpiar sesiones en caliente
with client_pool_lock:
client_pool.clear()
consecutive_errors.clear()
# 2. Borrar archivos físicos
for fpath in [LAST_STATE_FILE, CACHE_FILE, LAST_EVENT_IDS_FILE]:
if os.path.exists(fpath):
try:
os.remove(fpath)
except Exception:
pass
log_msg("🗑️ Hard Reset: Se eliminó la caché del servidor a petición del usuario. Iniciando escaneo limpio.")
# 3. Disparar el poller inmediatamente
polling_trigger_event.set()
self.wfile.write(json.dumps({
"status": "success",
"message": "Limpieza de caché completada con éxito. Reiniciando de cero..."
}).encode('utf-8'))
except Exception as e:
self.wfile.write(json.dumps({
"status": "error",
"message": f"Error al purgar caché: {e}"
}).encode('utf-8'))
finally:
update_lock.release()
elif self.path == '/toggle-polling':
global polling_enabled
polling_enabled = not polling_enabled
save_polling_state(polling_enabled)
state_str = "activado" if polling_enabled else "pausado"
log_msg(f"🛑/🟢 Polling en segundo plano {state_str} por el usuario.")
if polling_enabled:
polling_trigger_event.set()
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
self.wfile.write(json.dumps({"status": "success", "enabled": polling_enabled}).encode('utf-8'))
else:
self.send_response(404)
self.end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
class ThreadingHTTPServer(ThreadingMixIn, http.server.HTTPServer):
pass
def load_credentials_config():
"""Carga las credenciales, el puerto, la lista de exclusión y la opción de navegador de credentials.json."""
is_windows = platform.system() == "Windows"
has_display = is_windows or "DISPLAY" in os.environ
default_open = True if has_display else False
if not os.path.exists(CREDENTIALS_FILE):
return None, 8080, [], default_open
try:
with open(CREDENTIALS_FILE, 'r', encoding='utf-8') as f:
config = json.load(f)
# Si es un objeto (nuevo formato recomendado)
if isinstance(config, dict):
port = config.get("port", 8080)
exclude = config.get("exclude", [])
open_browser = config.get("open_browser", default_open)
accounts = config.get("accounts", [])
return accounts, port, exclude, open_browser
# Si es una lista (formato heredado compatible)
elif isinstance(config, list):
return config, 8080, [], default_open
except Exception as e:
print(f"⚠️ Error al leer credenciales: {e}")
return None, 8080, [], default_open
return None, 8080, [], default_open
def background_poller_thread():
global polling_enabled, last_poll_timestamp, consecutive_errors
log_msg("🔄 Motor de polling en segundo plano iniciado (intervalo: 30s)...")
while True:
polling_trigger_event.wait(timeout=30)
polling_trigger_event.clear()
if not polling_enabled:
continue
acquired = update_lock.acquire(blocking=False)
if not acquired:
continue
try:
accounts_cfg, _, exclude_cfg, _ = load_credentials_config()
if not accounts_cfg:
continue
now = datetime.datetime.now(ZoneInfo("America/Mexico_City"))
last_poll_timestamp = now.strftime('%I:%M:%S %p')
# Comprobar si hay errores persistentes acumulados (Circuit Breaker)
has_persistent_errors = any(err_count >= 3 for err_count in consecutive_errors.values())
if has_persistent_errors:
log_msg("⚠️ Circuit Breaker: Polling pausado automáticamente por múltiples fallos de autenticación.")
polling_enabled = False
continue
log_msg(f"🔄 Polling automático iniciado ({last_poll_timestamp})...")
# Ejecutar actualización real de datos desde Honeywell
run_update(accounts=accounts_cfg, exclude=exclude_cfg)
except Exception as e:
log_msg(f"⚠️ Excepción en hilo de polling: {e}")
finally:
update_lock.release()
def run_server(port=8080, open_browser=True):
# Cargar y re-renderizar la plantilla HTML desde last_state.json al arrancar el servidor
if os.path.exists(LAST_STATE_FILE):
try:
with open(LAST_STATE_FILE, 'r', encoding='utf-8') as f:
last_state = json.load(f)
# Reconstruir objetos ArmingState
for acc_state in last_state:
for loc in acc_state.get('locations', []):
if isinstance(loc['status'], int):
try:
loc['status'] = ArmingState(loc['status'])
except Exception:
loc['status'] = ArmingState.UNKNOWN
generate_html_report(last_state)
except Exception as e:
print(f"⚠️ No se pudo regenerar el reporte desde last_state.json: {e}")
elif not os.path.exists(HTML_OUTPUT_FILE):
print("⚠️ No se encontró reporte previo. Generando primer reporte vacío...")
generate_html_report([])
# Arrancar hilo en segundo plano para el polling continuo de eventos
poller = threading.Thread(target=background_poller_thread, daemon=True)
poller.start()
socketserver.TCPServer.allow_reuse_address = True
with ThreadingHTTPServer(("", port), DashboardHandler) as httpd:
url = f"http://localhost:{port}/"
print(f"\n🌐 Servidor de monitoreo iniciado en:")
print(f" - Local: {url}")
print(f" - VM Red: http://192.168.100.50:{port}/")
print("Presiona Ctrl+C para detener el servidor.")
if open_browser:
print(f" 🚀 Abriendo navegador en {url}...")
try:
webbrowser.open(url)
except Exception as e:
print(f" ⚠️ No se pudo abrir el navegador automáticamente: {e}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n🛑 Servidor detenido.")
def soap_get_events(session_id, last_event_id, location_id=0):
"""Consulta nuevos eventos SOAP desde last_event_id. Devuelve (lista_de_eventos, next_event_id)."""
url = "https://rs.alarmnet.com/TC21API/tc2.asmx"
headers = {
'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'https://services.alarmnet.com/TC2/GetLiveEvents'
}
event_count = 50 if last_event_id > 0 else 1
soap_body = f"""
{session_id}{location_id}{last_event_id}{event_count}11"""
events = []
max_event_id = last_event_id
try:
res = requests.post(url, data=soap_body.encode('utf-8'), headers=headers, timeout=12)
res.raise_for_status()
root = ET.fromstring(res.text)
# Encontrar nodo GetLiveEventsResult o GetSessionUserEventsResult ignorando namespaces
result_node = None
for elem in root.iter():
if elem.tag.endswith('GetLiveEventsResult') or elem.tag.endswith('GetSessionUserEventsResult'):
result_node = elem
break
if result_node is None:
result_node = root
# Extraer ResultCode
result_code_text = "0"
for child in result_node:
if child.tag.endswith('ResultCode'):
result_code_text = child.text
break
if result_code_text == '0':
# Buscar dinámicamente cualquier nodo que represente un evento por sus campos internos
for elem in result_node.iter():
event_id_val = None
loc_id_val = None
class_id_val = None
type_id_val = None
for child in elem:
tag_local = child.tag.split('}')[-1].lower() if '}' in child.tag else child.tag.lower()
if tag_local in ('eventid', 'eventrecordid'):
try:
event_id_val = int(child.text)
except (ValueError, TypeError):
pass
elif tag_local in ('locationid', 'deviceid'):
try:
loc_id_val = int(child.text)
except (ValueError, TypeError):
pass
elif tag_local in ('eventclassid', 'filterclass'):
try:
class_id_val = int(child.text)
except (ValueError, TypeError):
pass
elif tag_local in ('eventtypeid', 'eventtype'):
try:
type_id_val = int(child.text)
except (ValueError, TypeError):
pass
if event_id_val is not None:
events.append({
'event_id': event_id_val,
'location_id': loc_id_val if loc_id_val is not None else 0,
'class_id': class_id_val if class_id_val is not None else 0,
'type_id': type_id_val if type_id_val is not None else 0
})
if event_id_val > max_event_id:
max_event_id = event_id_val
elif result_code_text in ('-102', '-103', '4101', '4102'):
raise ConnectionResetError("SOAP Session is invalid or expired.")
else:
# Reportar error en la respuesta
data_str = ""
for child in result_node:
if child.tag.endswith('ResultData'):
data_str = child.text or ""
break
log_msg(f" ⚠️ GetLiveEvents falló con código {result_code_text}: {data_str}")
except ConnectionResetError:
raise
except Exception as e:
log_msg(f" ❌ Error al consultar GetLiveEvents: {e}")
return events, max_event_id
def get_soap_session_id(client, force_refresh=False):
"""Obtiene o refresca el SessionID GUID para SOAP usando el cliente REST."""
username = client.username
if not force_refresh:
guid = soap_session_ids.get(username)
if guid:
return guid
try:
from total_connect_client.const import HTTP_API_SESSION_DETAILS_ENDPOINT
sd_res = client.http_request(
endpoint=HTTP_API_SESSION_DETAILS_ENDPOINT,
method="GET",
params={"appId": client._app_id, "appVersion": client._app_version}
)
soap_guid = sd_res.get("SessionDetailsResult", {}).get("SessionID")
if soap_guid:
soap_session_ids[username] = soap_guid
soap_disabled_accounts.discard(username)
return soap_guid
except Exception as e:
log_msg(f" ⚠️ Error al obtener SessionID SOAP para {username}: {e}")
return None
def get_authenticated_client(acc, force_reauth=False):
"""Obtiene un cliente autenticado reutilizando la sesión activa en memoria si está viva."""
global client_pool, consecutive_errors
username = acc['username']
password = acc['password']
with client_pool_lock:
if force_reauth:
client_pool.pop(username, None)
client = client_pool.get(username)
if client and getattr(client, '_logged_in', False):
return client
print(f"🔑 Autenticando sesión para {acc['label']} ({username})...")
client = TotalConnectClient(
username=username,
password=password,
load_details=False
)
client_pool[username] = client
consecutive_errors[acc['label']] = 0
# Extraer SessionID GUID del endpoint sessiondetails para usarlo en SOAP GetLiveEvents
get_soap_session_id(client)
return client
def _apply_soap_events(events_list, next_event_id, acc, curr_last_event_id, device_to_location, locations_to_update, log_suffix=""):
"""Procesa eventos SOAP recibidos, actualiza locations_to_update y persiste el ID de referencia."""
suffix_str = f" {log_suffix}" if log_suffix else ""
if curr_last_event_id == 0:
last_event_ids[acc['username']] = next_event_id
save_last_event_ids(last_event_ids)
log_msg(f" ✓ Inicializado rastreador de eventos SOAP{suffix_str} en ID: {next_event_id} (sin procesar eventos previos)")
else:
if events_list:
log_msg(f" 🔔 Detectados {len(events_list)} eventos nuevos en la cuenta {acc['label']}{suffix_str}.")
for ev in events_list:
dev_id_or_loc_id = ev['location_id']
mapped_loc_id = device_to_location.get(dev_id_or_loc_id, dev_id_or_loc_id)
if mapped_loc_id > 0:
locations_to_update.add(mapped_loc_id)
last_event_ids[acc['username']] = next_event_id
save_last_event_ids(last_event_ids)
def run_update(accounts=None, exclude=None, full_refresh=False):
global current_loading_status
current_loading_status = "Iniciando cliente de consulta..."
log_msg("🚀 Iniciando cliente de consulta TotalConnect 2.0 (REST/OAuth2)...")
try:
if accounts is None or exclude is None:
accounts_cfg, _, exclude_cfg, _ = load_credentials_config()
if accounts is None:
accounts = accounts_cfg
if exclude is None:
exclude = exclude_cfg
if not accounts:
log_msg(f"❌ No se pudieron cargar las cuentas desde {CREDENTIALS_FILE}")
return False
# Cargar el último estado consolidado si existe
last_state = []
if os.path.exists(LAST_STATE_FILE):
try:
with open(LAST_STATE_FILE, 'r', encoding='utf-8') as f:
last_state = json.load(f)
except Exception as e:
log_msg(f"⚠️ No se pudo cargar el último estado: {e}")
# Reconstruir los objetos ArmingState y mapear sucursales por ID
last_loc_map = {}
for acc_state in last_state:
for loc in acc_state.get('locations', []):
if isinstance(loc['status'], int):
try:
loc['status'] = ArmingState(loc['status'])
except Exception:
loc['status'] = ArmingState.UNKNOWN
last_loc_map[str(loc['id'])] = loc
last_state_map = {acc_item['label']: acc_item for acc_item in last_state}
updated_labels = set()
# Cargar caché de ubicaciones conocidas
cache_data = {}
if os.path.exists(CACHE_FILE):
try:
with open(CACHE_FILE, 'r', encoding='utf-8') as f:
cache_data = json.load(f)
except Exception:
cache_data = {}
report_data = []
for acc in accounts:
current_loading_status = f"Conectando a cuenta: {acc['label']}"
log_msg(f"👤 Conectando a cuenta: {acc['label']} ({acc['username']})...")
try:
# Reutilizar sesión autenticada en memoria para velocidad instantánea
try:
client = get_authenticated_client(acc)
except Exception as auth_err:
log_msg(f" ⚠️ Reintentando autenticación limpia para {acc['label']}...")
client = get_authenticated_client(acc, force_reauth=True)
log_msg(f" ✓ Conexión establecida con éxito. ({len(client.locations)} sucursales)")
# Construir mapa de device_id a location_id para GetLiveEvents
device_to_location = {}
for loc_id, location in client.locations.items():
if getattr(location, 'security_device_id', None):
try:
dev_id = int(location.security_device_id)
device_to_location[dev_id] = loc_id
except Exception:
pass
# Obtener SessionID SOAP si está habilitado
soap_sess_id = None
if acc['username'] not in soap_disabled_accounts:
try:
soap_sess_id = get_soap_session_id(client)
except Exception as soap_init_err:
log_msg(f" ⚠️ SOAP no disponible para {acc['label']}: {soap_init_err}")
soap_disabled_accounts.add(acc['username'])
# 1. Agrupar sucursales activas (no excluidas)
active_locs_with_obj = []
for loc_id, location in client.locations.items():
loc_id_str = str(loc_id)
loc_name_str = location.location_name
if exclude:
if any(str(ex).strip() in loc_id_str or str(ex).strip() in loc_name_str for ex in exclude):
continue
active_locs_with_obj.append((loc_id, location))
# Reconstruir mapa de device_id a location_id (si se requiere)
device_to_location = {}
for loc_id, location in active_locs_with_obj:
if getattr(location, 'security_device_id', None):
try:
dev_id = int(location.security_device_id)
device_to_location[dev_id] = loc_id
except Exception:
pass
# 2. Ordenar por antigüedad de consulta profunda (last_download_epoch)
def get_download_epoch(loc_tuple):
loc_id_str = str(loc_tuple[0])
prev = last_loc_map.get(loc_id_str, {})
return prev.get('last_download_epoch', 0)
active_locs_with_obj.sort(key=get_download_epoch)
# 3. Construir Grupo A (Secuencial - 27 sucursales)
group_a = active_locs_with_obj[:27]
group_a_ids = {x[0] for x in group_a}
# 4. Construir Grupo B (Alta Frecuencia - Hasta 7 sucursales)
group_b_pool = []
for loc_id, location in active_locs_with_obj:
if loc_id in group_a_ids:
continue
loc_id_str = str(loc_id)
prev = last_loc_map.get(loc_id_str, {})
status_val = prev.get('status', 0)
if isinstance(status_val, ArmingState):
status_val = status_val.value
is_disarmed = (status_val == 10200 or str(status_val).upper() == "DISARMED")
is_triggered = len(prev.get('triggered_partitions', [])) > 0
has_faults = len(prev.get('active_zones', [])) > 0
if is_disarmed or is_triggered or has_faults:
group_b_pool.append((loc_id, location))
group_b_pool.sort(key=get_download_epoch)
group_b = group_b_pool[:7]
group_b_ids = {x[0] for x in group_b}
# 5. Construir Grupo C (Triggers de SOAP pendientes)
group_c = []
for loc_id, location in active_locs_with_obj:
if loc_id in group_a_ids or loc_id in group_b_ids:
continue
loc_id_str = str(loc_id)
prev = last_loc_map.get(loc_id_str, {})
if prev.get('needs_full_update', False):
group_c.append((loc_id, location))
# Lote completo a procesar en este ciclo
lote_completo = group_a + group_b + group_c
lote_ids = {x[0] for x in lote_completo}
loc_list = []
cache_locations = {}
full_status_calls = 0
MAX_FULL_CALLS_PER_CYCLE = 35
# Procesar todas las sucursales de la cuenta
for loc_id, location in client.locations.items():
loc_id_str = str(loc_id)
loc_name_str = location.location_name
if exclude:
if any(str(ex).strip() in loc_id_str or str(ex).strip() in loc_name_str for ex in exclude):
continue
prev_loc = last_loc_map.get(loc_id_str, {})
# Si no está en el lote de este ciclo, re-utilizar la caché de forma instantánea
if loc_id not in lote_ids:
status_val = prev_loc.get('status', ArmingState.UNKNOWN)
loc_list.append({
"id": loc_id,
"name": loc_name_str,
"status": status_val,
"triggered_partitions": prev_loc.get('triggered_partitions', []),
"active_zones": prev_loc.get('active_zones', []),
"last_download_epoch": prev_loc.get('last_download_epoch', 0),
"last_download_time": prev_loc.get('last_download_time', "Nunca"),
"last_update_epoch": prev_loc.get('last_update_epoch', 0),
"last_update_time": prev_loc.get('last_update_time', "Nunca"),
"needs_full_update": prev_loc.get('needs_full_update', False)
})
cache_locations[loc_id_str] = loc_name_str
continue
# Determinar si requiere Poll Full (REST) o Poll Lite (SOAP)
needs_full_update = prev_loc.get('needs_full_update', False)
last_dl_epoch = prev_loc.get('last_download_epoch', 0)
antiguedad_seg = time.time() - last_dl_epoch if last_dl_epoch else 999999
reason = None
if full_refresh:
reason = "Refresco total manual solicitado"
elif not prev_loc:
reason = "Arranque en frío (sin caché previa)"
elif needs_full_update:
reason = "Cambio o evento previo detectado por SOAP"
elif antiguedad_seg > 600:
reason = f"Antigüedad de datos superó 10 minutos ({int(antiguedad_seg / 60)} min)"
# Limitador de velocidad: degradar a SOAP Lite si superamos el tope
if reason and full_status_calls >= MAX_FULL_CALLS_PER_CYCLE:
log_msg(f" ⚠️ Límite de Polls Full por ciclo alcanzado. Degradando '{loc_name_str}' a Poll Lite.")
reason = None
# --- CASO A: POLL FULL (REST fullStatus) ---
if reason:
full_status_calls += 1
log_msg(f" ⏳ Poll FULL en '{loc_name_str}' (Motivo: {reason})")
current_loading_status = f"Cargando sucursal: {loc_name_str}"
try:
endpoint = make_http_endpoint(f"api/v3/locations/{loc_id}/partitions/fullStatus")
result = client.http_request(endpoint=endpoint, method="GET")
client.raise_for_resultcode(result)
# Actualizar la librería
location._update_status(result)
location._update_partitions(result["PanelStatus"]["Partitions"])
location._update_zones(result["PanelStatus"]["Zones"])
triggered_parts = []
active_zones = []
# Particiones
for p_id, p in location.partitions.items():
if p.arming_state.is_triggered():
triggered_parts.append(p.name or f"Partición {p_id}")
# Zonas
raw_zones = result.get("PanelStatus", {}).get("Zones", [])
raw_zones_map = {int(z["ZoneID"]): z for z in raw_zones if "ZoneID" in z}
for z_id, z in location.zones.items():
partition_id = z.partition
partition = location.partitions.get(partition_id)
is_partition_triggered = partition.arming_state.is_triggered() if partition else location.arming_state.is_triggered()
is_alarm_state = z.is_triggered() and is_partition_triggered
is_fault_state = z.is_faulted()
if is_alarm_state or is_fault_state:
status_lbl = "Alarma" if is_alarm_state else "Abierta/Fallo"
raw_z = raw_zones_map.get(z_id, {})
trigger_time = raw_z.get("AlarmTriggerTimeLocalized", "") if is_alarm_state else ""
active_zones.append({
"id": z_id,
"name": z.description or f"Zona {z_id}",
"status": status_lbl,
"partition": z.partition,
"trigger_time": trigger_time
})
def get_zone_sort_key(z_item):
is_alarm = 0 if z_item["status"] == "Alarma" else 1
t_time = z_item["trigger_time"] or "9999-99-99T99:99:99"
return (is_alarm, t_time)
active_zones.sort(key=get_zone_sort_key)
effective_status = location.arming_state
if hasattr(effective_status, 'is_triggered') and effective_status.is_triggered() and not triggered_parts:
if location.partitions:
first_partition = next(iter(location.partitions.values()))
effective_status = first_partition.arming_state
now_dt = datetime.datetime.now(ZoneInfo("America/Mexico_City"))
now_str = now_dt.strftime('%d/%m/%Y %I:%M:%S %p')
# Determinar si el estado físico de armado cambió en este Full
status_changed = False
prev_status_val = prev_loc.get('status')
if isinstance(prev_status_val, ArmingState):
prev_status_val = prev_status_val.value
curr_status_val = effective_status.value if isinstance(effective_status, ArmingState) else effective_status
if prev_status_val != curr_status_val:
status_changed = True
last_up_epoch = time.time() if status_changed else prev_loc.get('last_update_epoch', time.time())
last_up_time = now_str if status_changed else prev_loc.get('last_update_time', now_str)
loc_list.append({
"id": loc_id,
"name": loc_name_str,
"status": effective_status,
"triggered_partitions": triggered_parts,
"active_zones": active_zones,
"last_download_epoch": time.time(),
"last_download_time": now_str,
"last_update_epoch": last_up_epoch,
"last_update_time": last_up_time,
"needs_full_update": False
})
except Exception as full_err:
log_msg(f" ❌ Falló Poll FULL en '{loc_name_str}': {full_err}. Usando caché.")
loc_list.append({
"id": loc_id,
"name": loc_name_str,
"status": prev_loc.get('status', ArmingState.UNKNOWN),
"triggered_partitions": prev_loc.get('triggered_partitions', []),
"active_zones": prev_loc.get('active_zones', []),
"last_download_epoch": prev_loc.get('last_download_epoch', 0),
"last_download_time": prev_loc.get('last_download_time', "Nunca"),
"last_update_epoch": prev_loc.get('last_update_epoch', 0),
"last_update_time": prev_loc.get('last_update_time', "Nunca"),
"needs_full_update": prev_loc.get('needs_full_update', False)
})
# --- CASO B: POLL LITE (SOAP GetLiveEvents) ---
else:
soap_success = False
if soap_sess_id:
if not isinstance(last_event_ids.get(acc['username']), dict):
last_event_ids[acc['username']] = {}
curr_last_event_id = last_event_ids[acc['username']].get(str(loc_id), 0)
try:
events_list, next_event_id = soap_get_events(soap_sess_id, curr_last_event_id, loc_id)
if curr_last_event_id == 0:
last_event_ids[acc['username']][str(loc_id)] = next_event_id
save_last_event_ids(last_event_ids)
else:
if events_list:
log_msg(f" 🔔 SOAP detectó {len(events_list)} eventos nuevos en '{loc_name_str}'.")
needs_full_update = True
now_dt = datetime.datetime.now(ZoneInfo("America/Mexico_City"))
now_str = now_dt.strftime('%d/%m/%Y %I:%M:%S %p')
prev_loc['last_update_epoch'] = time.time()
prev_loc['last_update_time'] = now_str
last_event_ids[acc['username']][str(loc_id)] = next_event_id
save_last_event_ids(last_event_ids)
soap_success = True
except ConnectionResetError:
log_msg(f" ⚠️ Sesión SOAP expirada al consultar '{loc_name_str}'. Se reintentará en la siguiente vuelta.")
except Exception as soap_err:
log_msg(f" ⚠️ Error de SOAP en '{loc_name_str}': {soap_err}")
loc_list.append({
"id": loc_id,
"name": loc_name_str,
"status": prev_loc.get('status', ArmingState.UNKNOWN),
"triggered_partitions": prev_loc.get('triggered_partitions', []),
"active_zones": prev_loc.get('active_zones', []),
"last_download_epoch": prev_loc.get('last_download_epoch', 0),
"last_download_time": prev_loc.get('last_download_time', "Nunca"),
"last_update_epoch": prev_loc.get('last_update_epoch', 0),
"last_update_time": prev_loc.get('last_update_time', "Nunca"),
"needs_full_update": needs_full_update
})
cache_locations[str(loc_id)] = loc_name_str
cache_data[acc['username']] = {
"label": acc['label'],
"locations": cache_locations
}
report_data.append({
"label": acc['label'],
"username": acc['username'],
"locations": loc_list
})
updated_labels.add(acc['label'])
log_msg(f" ✓ Procesamiento de {acc['label']} finalizado. (Consultas profundas: {full_status_calls})")
except Exception as e:
log_msg(f" ❌ Error al procesar cuenta {acc['label']}: {e}")
cached_acc = cache_data.get(acc['username'])
if cached_acc:
log_msg(f" ⚠️ Cargando {len(cached_acc['locations'])} sucursales desde la caché local...")
loc_list = []
for loc_id, loc_name in cached_acc['locations'].items():
loc_id_str = str(loc_id)
if exclude:
if any(str(ex).strip() in loc_id_str or str(ex).strip() in loc_name for ex in exclude):
continue
loc_list.append({
"id": loc_id,
"name": loc_name,
"status": "REVOKED",
"triggered_partitions": [],
"active_zones": [],
"last_download_epoch": 0,
"last_download_time": "Nunca",
"last_update_epoch": 0,
"last_update_time": "Nunca",
"needs_full_update": False
})
report_data.append({
"label": acc['label'],
"username": acc['username'],
"locations": loc_list
})
updated_labels.add(acc['label'])
# Intentar persistir la caché de nombres
if cache_data:
try:
with open(CACHE_FILE, 'w', encoding='utf-8') as f:
json.dump(cache_data, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f" ⚠️ No se pudo guardar la caché de ubicaciones: {e}")
# Mezclar los datos de las cuentas que no se actualizaron en esta ejecución
for label, old_acc_data in last_state_map.items():
if label not in updated_labels:
report_data.append(old_acc_data)
# Guardar el estado consolidado actual en last_state.json
serializable_report = []
for acc_data in report_data:
serializable_locs = []
for loc in acc_data['locations']:
status_val = loc['status'].value if isinstance(loc['status'], ArmingState) else loc['status']
serializable_locs.append({
"id": loc['id'],
"name": loc['name'],
"status": status_val,
"triggered_partitions": loc['triggered_partitions'],
"active_zones": loc['active_zones'],
"last_download_epoch": loc.get('last_download_epoch', 0),
"last_download_time": loc.get('last_download_time', "Nunca"),
"last_update_epoch": loc.get('last_update_epoch', 0),
"last_update_time": loc.get('last_update_time', "Nunca"),
"needs_full_update": loc.get('needs_full_update', False)
})
serializable_report.append({
"label": acc_data['label'],
"username": acc_data['username'],
"locations": serializable_locs
})
try:
with open(LAST_STATE_FILE, 'w', encoding='utf-8') as f:
json.dump(serializable_report, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"⚠️ No se pudo guardar el archivo de estado consolidado: {e}")
if report_data:
generate_html_report(report_data)
return True
else:
print("❌ No se pudieron recuperar datos de ninguna cuenta.")
return False
finally:
current_loading_status = ""
def main():
# Asegurar que la documentación y plantillas existan en la carpeta de ejecución
auto_generate_docs()
# Cargar credenciales, puerto, exclusiones y opción de navegador
accounts, port, exclude, open_browser = load_credentials_config()
# Si se pasa --once o -o como argumento, corre una sola vez y sale
if len(sys.argv) > 1 and (sys.argv[1] == '--once' or sys.argv[1] == '-o'):
run_update(accounts, exclude)
else:
# Desactivar navegador si se pasa --no-browser o -nb por CLI
if "--no-browser" in sys.argv or "-nb" in sys.argv:
open_browser = False
# Modo por defecto: arrancar como servidor HTTP
# Si se especifica un puerto por parámetro de línea de comandos, se prioriza
cmd_port = None
for arg in sys.argv[1:]:
try:
cmd_port = int(arg)
except ValueError:
pass
final_port = cmd_port if cmd_port is not None else port
run_server(final_port, open_browser)
if __name__ == '__main__':
main()