]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: cleanup empty data structure checks
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 3 Jul 2025 10:50:13 +0000 (12:50 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 3 Jul 2025 10:50:13 +0000 (12:50 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ChargingStation.ts
src/charging-station/Helpers.ts
src/charging-station/ui-server/UIServerUtils.ts
src/utils/Configuration.ts

index de21923415e097637a9c912f41562aaf3d38be42..5dca7b722446827b26355143e3206656f5ab1f80 100644 (file)
@@ -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)
index a93d78452086795ec09ccc935a1010275952018a..cadae37bffe002041cdcebe35e54ef21aa7af51b 100644 (file)
@@ -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`
         )
index fd7774406b36a47864e576f3f4c5873c72e39494..419c9f63646a5a2216dde19677f88bb519b34e19 100644 (file)
@@ -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) {
index a6482afe27ce4c1ecb4cbd7dd15aa489033bd1a0..38b7d6c84c95c51647cea43fea2dd4cc5c7835c1 100644 (file)
@@ -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 {