import { Page } from "puppeteer"; import { LoxoneControlPlatform } from "../platform.js"; export type LoxoneComponent = { identifier: string; uuidAction: string; name: string; searchDescription: string; type: string; defaultIcon: string; controlType: string; groupDetail: string; room: string; isSecured: boolean; states: { [key: string]: string; }; }; export declare class LoxoneWebinterface { readonly platform: LoxoneControlPlatform; private browser; page: Page | undefined; collectedComponents: LoxoneComponent[]; controlStateMaps: { [uuidAction: string]: { [stateName: string]: string; }; }; private preventStandbyInterval; private healthCheckInterval; private isRecovering; private savedSession; /** * Origin the dns.loxonecloud.com redirector last sent us to * (`https://..dyndns.loxonecloud.com:`). When the * redirector itself fails (405/408/503 — 2026-10-18 it answered 408 for * hours while the Miniserver and the Loxone app were fine), navigate to this * origin directly instead of being dead until the redirector recovers. * Persisted next to the Homebridge storage so it survives restarts. */ private lastRelayOrigin; private relayLoaded; recoveryCount: number; private lastStatusPushAt; /** * Time of the last REAL status push from Loxone. Unlike lastStatusPushAt it * is never reset on (re)init, so /api/health can tell "browser and page are * fine but no data has arrived for N minutes". 2026-10-23: the cloud relay * was gone for hours; the plugin looped "No components collected" while * /api/health kept answering healthy/connected/responsive, so every * dashboard tile stayed green while nothing was controllable. */ private lastDataAt; private wsRecoveryTimer; private commandEchoTimer; private navErrorResolve; constructor(platform: LoxoneControlPlatform); /** * Returns true only when the interface came up far enough to serve HomeKit * (collection captured, onReady fired, health check armed). Callers — notably * triggerRecovery — need to distinguish that from the failure paths below, * which are all logged but deliberately not thrown. */ init(): Promise; /** * Run a page.evaluate with a hard timeout. If the underlying Chrome CDP call hangs * (the classic "Runtime.callFunctionOn timed out" symptom), kick off a recovery and * return null to the caller so it can fail fast instead of blocking on the 5-minute * protocol timeout. Returns null during recovery and if the page is not yet ready. */ safeEvaluate(fn: (...args: any[]) => T, ...args: any[]): Promise; /** * Snapshot cookies + localStorage from the live page into memory so a subsequent * init() (e.g. recovery) can skip the form-based login. */ private captureSession; /** * Public entry point for external recovery triggers (e.g. debug endpoint). */ getHealthStatus(): Promise<{ status: string; browser: string; page: string; data: string; dataAge: number | null; recovering: boolean; recoveryCount: number; uptime: number; }>; forceRecovery(): Promise; /** * Tear down the current page (and optionally the browser) and re-run init(). * Tries a page-only recovery first — keeping the browser alive preserves V8's * compiled code cache for comps.js, making re-init dramatically faster. Only * kills the whole browser if page-only recovery fails. * Idempotent — concurrent callers see isRecovering=true and exit. */ private triggerRecovery; /** * Watch the page's WebSockets via CDP so a dead data path is detected as an * EVENT (~20s) instead of being inferred from STATUS_STALE_THRESHOLD silence * (10 minutes). This is the fast path for the failure that actually happens * here: the Miniserver re-registers its cloud tunnel, gets a new relay * host:port, and the socket we hold is closed against an endpoint that no * longer exists — the app can retry forever and never get back. * * We do NOT recover on every close: the Loxone app reconnects itself after an * ordinary blip, so a close only arms a timer that any new socket cancels. * Recovery fires only if nothing reconnects within the grace window. */ private watchWebSocket; /** * Put the Loxone page in the background by bringing a blank tab to the front. * * The plugin only needs the app's data path (WebSocket -> patched comps.js -> * exposed callbacks), never its pixels. While the Loxone tab is the visible, * focused tab, Chrome runs every requestAnimationFrame loop at full frame * rate: the app's pulsing "unread NEWS" badge (react-native-web * Animated.loop, a 7x7 px SVG) alone drove ~60 style + layout passes per * second, ~24% of a NAS core, around the clock. A hidden page gets no * animation frames at all. * * Measured 2026-10-01 on the live system, 12 min hidden: Chrome CPU 24% -> * 0-1%, Loxone WebSocket messages kept flowing (~105-133/min, same as * visible), keepalive timers unaffected, and no stale-watchdog recovery — * i.e. status pushes kept reaching bumpStatusHeartbeat(). * * init() runs this after every (re)start, so a recovery re-backgrounds the * new page. Non-fatal: on failure the plugin keeps working as before, with * the page in front. */ private backgroundLoxonePage; /** * Quick liveness probe: create a throwaway page and close it within a deadline. * If the browser's CDP connection is wedged, both will time out. */ private isBrowserAlive; /** * Called from platform.onStatusUpdate / onStatusUpdateBefore on every Loxone * status push. The watchdog uses this timestamp to detect a silently dead * WS data path (page JS still answers but no events flow through). */ bumpStatusHeartbeat(): void; /** * Called right after a command goes out over the WebSocket. Loxone echoes a * status push for anything we control, so silence here is proof of death * rather than an inference from it — which makes this the fastest detector we * have, and the only one that fires on the interaction the user is actually * standing in front of. * * Deliberately armed only once: a burst of commands (e.g. "all blinds up") * waits on a single echo, not one timer per control. */ expectStatusAfterCommand(): void; /** * Background watchdog. Two checks per tick: * 1. JS-responsiveness — `safeEvaluate(() => 1)` exercises Chrome's CDP path; * a hang there triggers recovery via safeEvaluate's own catch. * 2. WS-data freshness — if no Loxone status push has landed within * STATUS_STALE_THRESHOLD ms, the WS path is dead even if Chrome is * responsive. The trivial probe in (1) cannot detect this. Trigger * the same recovery path explicitly. */ startHealthCheck(): void; shutdown(): Promise; private relayFile; private loadRelayOrigin; private rememberRelayOrigin; getLoxoneCredentials(): { serverUrl: string; user: any; password: any; }; }