X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=ecd858c9a78e676b2aa0e9990d9359d593144899;hb=a9671b9e315a2807c2659fdda962203229d35c0a;hp=9bf299aa750b0bcf7ac61641c7f6ef826655d80d;hpb=81c74884a3bc7441eb454ee90bda854d752e1908;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9bf299aa..ecd858c9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1,6 +1,6 @@ // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -import { createHash } from 'node:crypto' +import { createHash, randomInt } from 'node:crypto' import { EventEmitter } from 'node:events' import { existsSync, type FSWatcher, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' @@ -88,7 +88,6 @@ import { exponentialDelay, formatDurationMilliSeconds, formatDurationSeconds, - getRandomInteger, getWebSocketCloseEventStatusString, handleFileException, isNotEmptyArray, @@ -229,7 +228,7 @@ export class ChargingStation extends EventEmitter { this.wsConnectionRetried ? true : this.getAutomaticTransactionGeneratorConfiguration()?.stopAbsoluteDuration - ).catch(error => { + ).catch((error: unknown) => { logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error) }) this.wsConnectionRetried = false @@ -287,7 +286,7 @@ export class ChargingStation extends EventEmitter { readFileSync(this.templateFile, 'utf8') ) as ChargingStationTemplate } catch { - stationTemplate = undefined + // Ignore } return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`) } @@ -553,7 +552,7 @@ export class ChargingStation extends EventEmitter { this.heartbeatSetInterval = setInterval(() => { this.ocppRequestService .requestHandler(this, RequestCommand.HEARTBEAT) - .catch(error => { + .catch((error: unknown) => { logger.error( `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`, error @@ -638,7 +637,7 @@ export class ChargingStation extends EventEmitter { meterValue: [meterValue] } ) - .catch(error => { + .catch((error: unknown) => { logger.error( `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`, error @@ -842,7 +841,7 @@ export class ChargingStation extends EventEmitter { this.wsConnection.on('close', this.onClose.bind(this)) // Handle WebSocket open this.wsConnection.on('open', () => { - this.onOpen().catch(error => + this.onOpen().catch((error: unknown) => logger.error(`${this.logPrefix()} Error while opening WebSocket connection:`, error) ) }) @@ -1192,15 +1191,6 @@ export class ChargingStation extends EventEmitter { } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'` ) } - stationInfo.firmwareUpgrade = mergeDeepRight( - { - versionUpgrade: { - step: 1 - }, - reset: true - }, - stationTemplate.firmwareUpgrade ?? {} - ) if (stationTemplate.resetTime != null) { stationInfo.resetTime = secondsToMilliseconds(stationTemplate.resetTime) } @@ -1237,6 +1227,7 @@ export class ChargingStation extends EventEmitter { const stationInfoFromFile = this.getStationInfoFromFile( stationInfoFromTemplate.stationInfoPersistentConfiguration ) + let stationInfo: ChargingStationInfo // Priority: // 1. charging station info from template // 2. charging station info from configuration file @@ -1244,19 +1235,14 @@ export class ChargingStation extends EventEmitter { stationInfoFromFile != null && stationInfoFromFile.templateHash === stationInfoFromTemplate.templateHash ) { - return setChargingStationOptions( - { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromFile }, - options - ) + stationInfo = stationInfoFromFile + } else { + stationInfo = stationInfoFromTemplate + stationInfoFromFile != null && + propagateSerialNumber(this.getTemplateFromFile(), stationInfoFromFile, stationInfo) } - stationInfoFromFile != null && - propagateSerialNumber( - this.getTemplateFromFile(), - stationInfoFromFile, - stationInfoFromTemplate - ) return setChargingStationOptions( - { ...Constants.DEFAULT_STATION_INFO, ...stationInfoFromTemplate }, + mergeDeepRight(Constants.DEFAULT_STATION_INFO, stationInfo), options ) } @@ -1554,7 +1540,7 @@ export class ChargingStation extends EventEmitter { } const templateConnectorId = connectorId > 0 && stationTemplate.randomConnectors === true - ? getRandomInteger(templateMaxAvailableConnectors, 1) + ? randomInt(1, templateMaxAvailableConnectors) : connectorId const connectorStatus = stationTemplate.Connectors[templateConnectorId] checkStationInfoConnectorStatus( @@ -1767,7 +1753,7 @@ export class ChargingStation extends EventEmitter { this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash) this.sharedLRUCache.setChargingStationConfiguration(configurationData) this.configurationFileHash = configurationHash - }).catch(error => { + }).catch((error: unknown) => { handleFileException( this.configurationFile, FileType.ChargingStationConfiguration, @@ -1911,7 +1897,9 @@ export class ChargingStation extends EventEmitter { .then(() => { this.emit(ChargingStationEvents.updated) }) - .catch(error => logger.error(`${this.logPrefix()} Error while reconnecting:`, error)) + .catch((error: unknown) => + logger.error(`${this.logPrefix()} Error while reconnecting:`, error) + ) break } } @@ -2233,21 +2221,28 @@ export class ChargingStation extends EventEmitter { for (const [evseId, evseStatus] of this.evses) { if (evseId > 0) { for (const [connectorId, connectorStatus] of evseStatus.connectors) { - const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus) - await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus, evseId) + await sendAndSetConnectorStatus( + this, + connectorId, + getBootConnectorStatus(this, connectorId, connectorStatus), + evseId + ) } } } } else { for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { - const connectorBootStatus = getBootConnectorStatus( + await sendAndSetConnectorStatus( this, connectorId, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getConnectorStatus(connectorId)! + getBootConnectorStatus( + this, + connectorId, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.getConnectorStatus(connectorId)! + ) ) - await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus) } } }