UNPKG

homebridge-hilo

Version:

Plugin Homebridge (non officiel) pour la passerelle et les appareils Hilo de Hydro-Québec | Unofficial Homebridge plugin for Hydro-Québec Hilo bridge and devices

117 lines 4.57 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.connectToDeviceHub = connectToDeviceHub; exports.disconnectDeviceHub = disconnectDeviceHub; const signalR = __importStar(require("@microsoft/signalr")); const hiloApi_1 = require("./hiloApi"); const logger_1 = require("./logger"); let hubConnection = null; let webSocketRetries = 0; const MAX_RETRIES = 8; const BASE_RETRY_DELAY = 30000; async function connectToDeviceHub(locationId) { const logger = (0, logger_1.getLogger)(); let url; try { const response = await (0, hiloApi_1.negotiate)(); url = response.url; } catch (error) { logger.error("Unable to negotiate websocket connection", error instanceof Error ? error.message : error); return { devices: [], connected: false }; } hubConnection = new signalR.HubConnectionBuilder() .withUrl(url, { accessTokenFactory: () => (0, hiloApi_1.getWsAccessToken)() }) .configureLogging(signalR.LogLevel.Warning) .build(); const deviceListPromise = new Promise((resolve) => { const timeout = setTimeout(() => { logger.error("Timed out waiting for DeviceListInitialValuesReceived (30s)"); resolve({ devices: [], connected: false }); }, 30000); hubConnection.on("DeviceListInitialValuesReceived", (devices) => { clearTimeout(timeout); logger.debug(`Received ${devices.length} devices from DeviceHub`); resolve({ devices, connected: true }); }); }); hubConnection.onclose((error) => { if (error) { logger.error("DeviceHub connection closed with error:", error.message); } else { logger.debug("DeviceHub connection closed"); } retryConnection(locationId); }); try { await hubConnection.start(); webSocketRetries = 0; logger.info("Connected to DeviceHub"); await hubConnection.invoke("SubscribeToLocation", locationId.toString()); logger.debug(`Subscribed to location ${locationId}`); } catch (error) { logger.error("Failed to start DeviceHub connection", error instanceof Error ? error.message : error); return { devices: [], connected: false }; } return deviceListPromise; } function retryConnection(locationId) { const logger = (0, logger_1.getLogger)(); if (webSocketRetries >= MAX_RETRIES) { logger.error(`DeviceHub reconnection failed after ${MAX_RETRIES} attempts`); return; } const delay = BASE_RETRY_DELAY * Math.pow(2, webSocketRetries); webSocketRetries++; logger.info(`Retrying DeviceHub connection in ${delay / 1000}s (attempt ${webSocketRetries}/${MAX_RETRIES})`); setTimeout(async () => { try { await connectToDeviceHub(locationId); } catch (error) { logger.error("DeviceHub reconnection failed", error instanceof Error ? error.message : error); } }, delay); } function disconnectDeviceHub() { if (hubConnection) { hubConnection.stop().catch(() => { }); hubConnection = null; } } //# sourceMappingURL=deviceHub.js.map