From 3cc104c22939ea5c126faf04543745f7773b5548 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 3 Jul 2025 12:50:13 +0200 Subject: [PATCH] refactor: cleanup empty data structure checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 13 +++++++------ src/charging-station/Helpers.ts | 2 +- src/charging-station/ui-server/UIServerUtils.ts | 4 ++-- src/utils/Configuration.ts | 5 ++++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index de219234..5dca7b72 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -88,6 +88,7 @@ import { formatDurationSeconds, getWebSocketCloseEventStatusString, handleFileException, + isEmpty, isNotEmptyArray, isNotEmptyString, logger, @@ -177,7 +178,7 @@ export class ChargingStation extends EventEmitter { public wsConnection: null | WebSocket public get hasEvses (): boolean { - return this.connectors.size === 0 && this.evses.size > 0 + return isEmpty(this.connectors) && this.evses.size > 0 } public get wsConnectionUrl (): URL { @@ -1587,7 +1588,7 @@ export class ChargingStation extends EventEmitter { } private initializeConnectorsFromTemplate (stationTemplate: ChargingStationTemplate): void { - if (stationTemplate.Connectors == null && this.connectors.size === 0) { + if (stationTemplate.Connectors == null && isEmpty(this.connectors)) { const errorMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) @@ -1609,7 +1610,7 @@ export class ChargingStation extends EventEmitter { ) const connectorsConfigChanged = this.connectors.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash - if (this.connectors.size === 0 || connectorsConfigChanged) { + if (isEmpty(this.connectors) || connectorsConfigChanged) { connectorsConfigChanged && this.connectors.clear() this.connectorsConfigurationHash = connectorsConfigHash if (templateMaxConnectors > 0) { @@ -1705,7 +1706,7 @@ export class ChargingStation extends EventEmitter { } private initializeEvsesFromTemplate (stationTemplate: ChargingStationTemplate): void { - if (stationTemplate.Evses == null && this.evses.size === 0) { + if (stationTemplate.Evses == null && isEmpty(this.evses)) { const errorMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined` logger.error(`${this.logPrefix()} ${errorMsg}`) throw new BaseError(errorMsg) @@ -1739,7 +1740,7 @@ export class ChargingStation extends EventEmitter { ) const evsesConfigChanged = this.evses.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash - if (this.evses.size === 0 || evsesConfigChanged) { + if (isEmpty(this.evses) || evsesConfigChanged) { evsesConfigChanged && this.evses.clear() this.evsesConfigurationHash = evsesConfigHash const templateMaxEvses = getMaxNumberOfEvses(stationTemplate.Evses) @@ -2333,7 +2334,7 @@ export class ChargingStation extends EventEmitter { if (this.isWebSocketConnectionOpened() && this.inAcceptedState()) { this.flushMessageBuffer() } - if (!this.isWebSocketConnectionOpened() || this.messageQueue.length === 0) { + if (!this.isWebSocketConnectionOpened() || isEmpty(this.messageQueue)) { this.clearIntervalFlushMessageBuffer() } }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL) diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index a93d7845..cadae37b 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -231,7 +231,7 @@ export const validateStationInfo = (chargingStation: ChargingStation): void => { switch (chargingStation.stationInfo.ocppVersion) { case OCPPVersion.VERSION_20: case OCPPVersion.VERSION_201: - if (chargingStation.evses.size === 0) { + if (isEmpty(chargingStation.evses)) { throw new BaseError( `${chargingStationId}: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration` ) diff --git a/src/charging-station/ui-server/UIServerUtils.ts b/src/charging-station/ui-server/UIServerUtils.ts index fd777440..419c9f63 100644 --- a/src/charging-station/ui-server/UIServerUtils.ts +++ b/src/charging-station/ui-server/UIServerUtils.ts @@ -2,7 +2,7 @@ import type { IncomingMessage } from 'node:http' import { BaseError } from '../../exception/index.js' import { Protocol, ProtocolVersion } from '../../types/index.js' -import { logger, logPrefix } from '../../utils/index.js' +import { isEmpty, logger, logPrefix } from '../../utils/index.js' export const getUsernameAndPasswordFromAuthorizationToken = ( authorizationToken: string, @@ -25,7 +25,7 @@ export const handleProtocols = ( ): false | string => { let protocol: Protocol | undefined let version: ProtocolVersion | undefined - if (protocols.size === 0) { + if (isEmpty(protocols)) { return false } for (const fullProtocol of protocols) { diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index a6482afe..38b7d6c8 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -575,7 +575,10 @@ export class Configuration { } private static getConfigurationFileWatcher (): FSWatcher | undefined { - if (Configuration.configurationFile == null || Configuration.configurationFile.length === 0) { + if ( + Configuration.configurationFile == null || + Configuration.configurationFile.trim().length === 0 + ) { return } try { -- 2.43.0