From ffbe13de35d55172b64c2183368bc570b572b076 Mon Sep 17 00:00:00 2001 From: kyleboyer Date: Wed, 7 Aug 2024 11:23:29 -0500 Subject: [PATCH] Added Native API heartbeat --- config.schema.json | 5 +++++ package-lock.json | 4 ++-- package.json | 2 +- src/hub.ts | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/config.schema.json b/config.schema.json index 3c6d4a0..2effed3 100644 --- a/config.schema.json +++ b/config.schema.json @@ -53,6 +53,11 @@ } } }, + "enableNativeAPIHeartbeat": { + "title": "Prevent random restarts by performing a heartbeat on the Native API", + "type": "boolean", + "default": true + }, "garageDoorType": { "title": "Enable publishing a separate \"occupancy\" obstruction sensor for the garage", "type": "string", diff --git a/package-lock.json b/package-lock.json index 6d3016e..f295248 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.30", + "version": "0.2.31", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.30", + "version": "0.2.31", "funding": [ { "type": "github", diff --git a/package.json b/package.json index b5af16f..ed7ee65 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Konnected BlaQ", "name": "homebridge-blaq", - "version": "0.2.30", + "version": "0.2.31", "description": "Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware", "license": "Apache-2.0", "type": "module", diff --git a/src/hub.ts b/src/hub.ts index b485034..21de0de 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -1,6 +1,7 @@ import { AutoReconnectingEventSource, LogMessageEvent, PingMessageEvent, StateUpdateMessageEvent } from './utils/eventsource.js'; import { Logger, PlatformAccessory, PlatformConfig } from 'homebridge'; import stripAnsi from 'strip-ansi'; +import { createConnection } from 'net'; import { BlaQTextSensorEvent, ConfigDevice } from './types.js'; import { BaseBlaQAccessoryInterface } from './accessory/base.js'; import { BlaQHomebridgePluginPlatform } from './platform.js'; @@ -64,6 +65,11 @@ export class BlaQHub { this.initAccessoryCallback = initAccessoryCallback; this.logger = logger; this.reinitializeEventSource(); + if(pluginConfig.enableNativeAPIHeartbeat){ + setInterval(() => { + this.performNativeAPIHeartbeat(); + }, 5 * 60 * 1000); + } logger.debug('Initialized BlaQHub!'); } @@ -96,6 +102,20 @@ export class BlaQHub { } } + public performNativeAPIHeartbeat() { + const socket = createConnection( + {host: this.host, port: 6053}, + ( + () => setTimeout( + () => { + socket.destroy(); + }, + 5 * 1000, + ) + ), + ); + } + private possiblyFinalizeInit(){ if(!this.initialized && this.friendlyName && this.deviceMac){ this.logger.info(`[${this.configDevice.displayName}] [init] Publishing accessories with device model:`, this.friendlyName);