From 381437531504475e02cc4faac4d83d52e9a1d47f Mon Sep 17 00:00:00 2001 From: Jordy van den Aardweg Date: Fri, 20 Dec 2024 00:37:48 +0100 Subject: [PATCH] refactor: prevent flooding logs when error happens during polling --- src/energy-socket-accessory.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/energy-socket-accessory.ts b/src/energy-socket-accessory.ts index d557a59..69fdb66 100644 --- a/src/energy-socket-accessory.ts +++ b/src/energy-socket-accessory.ts @@ -35,6 +35,7 @@ export class EnergySocketAccessory { longPollErrorCount = 0; longPollCrossedThresholdAboveAt: Date | null = null; longPollCrossedThresholdBelowAt: Date | null = null; + private statePollingErrorCount = 0; // Add a method to allow setting the service for testing protected setService(service: Service) { @@ -525,6 +526,8 @@ export class EnergySocketAccessory { try { const response = await this.energySocketApi.getState(); + // Reset error count on successful response + this.statePollingErrorCount = 0; // Keep the local state in sync this.setLocalStateResponse(response); @@ -543,10 +546,33 @@ export class EnergySocketAccessory { this.syncOutletInUseStateWithOnState(response.power_on); } } catch (error) { - if (error instanceof Undici.errors.HeadersTimeoutError) { - this.log.error('Error during state polling. Device is probably offline.', error); + const isFirstError = this.statePollingErrorCount === 0; + const isErrorCountAfterInterval = this.statePollingErrorCount > SHOW_POLLING_ERRORS_INTERVAL; + + // Only show the error if it's the first error or after the interval + if (isErrorCountAfterInterval || isFirstError) { + if (error instanceof Undici.errors.HeadersTimeoutError) { + this.log.error( + `Error during state polling. Device is probably offline.${ + this.statePollingErrorCount ? ` Total errors: ${this.statePollingErrorCount}` : '' + }`, + ); + } else { + this.log.error( + `Error polling state:${ + this.statePollingErrorCount ? ` Total errors: ${this.statePollingErrorCount}` : '' + }`, + error, + ); + } + + if (isErrorCountAfterInterval) { + // Reset the counter after showing the error + this.statePollingErrorCount = 0; + } } else { - this.log.error('Error polling state:', error); + // Continue counting + this.statePollingErrorCount += 1; } }