X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=7eef4100355d0113f23848f72bac8c26c9b38acc;hb=0557254bd178be09468f48b28c570efa4ada83b2;hp=d130392e6c0257da7a6feea2cb8cb205249bc7bc;hpb=8cc482a9324a0989516b6eb6db85a16258c4b4d1;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d130392e..7eef4100 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -14,6 +14,7 @@ import { dirname, join } from 'node:path'; import { URL } from 'node:url'; import { parentPort } from 'node:worker_threads'; +import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns'; import merge from 'just-merge'; import { type RawData, WebSocket } from 'ws'; @@ -24,7 +25,7 @@ import { deleteConfigurationKey, getConfigurationKey, setConfigurationKeyValue, -} from './ChargingStationConfigurationUtils'; +} from './ConfigurationKeyUtils'; import { buildConnectorsMap, checkConnectorsConfiguration, @@ -42,11 +43,12 @@ import { getIdTagsFile, getMaxNumberOfEvses, getPhaseRotationValue, + hasFeatureProfile, initializeConnectorsMapStatus, propagateSerialNumber, stationTemplateToStationInfo, warnTemplateKeysDeprecation, -} from './ChargingStationUtils'; +} from './Helpers'; import { IdTagsCache } from './IdTagsCache'; import { OCPP16IncomingRequestService, @@ -102,7 +104,7 @@ import { RegistrationStatusEnumType, RequestCommand, type Reservation, - ReservationFilterKey, + type ReservationFilterKey, ReservationTerminationReason, type Response, StandardParametersKey, @@ -177,6 +179,7 @@ export class ChargingStation { private configurationFileHash!: string; private connectorsConfigurationHash!: string; private evsesConfigurationHash!: string; + private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration; private ocppIncomingRequestService!: OCPPIncomingRequestService; private readonly messageBuffer: Set; private configuredSupervisionUrl!: URL; @@ -515,11 +518,11 @@ export class ChargingStation { public getHeartbeatInterval(): number { const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval); if (HeartbeatInterval) { - return convertToInt(HeartbeatInterval.value) * 1000; + return secondsToMilliseconds(convertToInt(HeartbeatInterval.value)); } const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval); if (HeartBeatInterval) { - return convertToInt(HeartBeatInterval.value) * 1000; + return secondsToMilliseconds(convertToInt(HeartBeatInterval.value)); } this.stationInfo?.autoRegister === false && logger.warn( @@ -591,14 +594,14 @@ export class ChargingStation { public startMeterValues(connectorId: number, interval: number): void { if (connectorId === 0) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`, + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`, ); return; } if (!this.getConnectorStatus(connectorId)) { logger.error( `${this.logPrefix()} Trying to start MeterValues on non existing connector id - ${connectorId.toString()}`, + ${connectorId}`, ); return; } @@ -666,7 +669,7 @@ export class ChargingStation { if (this.getEnableStatistics() === true) { this.performanceStatistics?.start(); } - if (this.hasFeatureProfile(SupportedFeatureProfiles.Reservation)) { + if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { this.startReservationExpirationSetInterval(); } this.openWSConnection(); @@ -690,6 +693,7 @@ export class ChargingStation { this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!); // Restart the ATG this.stopAutomaticTransactionGenerator(); + delete this.automaticTransactionGeneratorConfiguration; if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) { this.startAutomaticTransactionGenerator(); } @@ -728,6 +732,9 @@ export class ChargingStation { if (this.getEnableStatistics() === true) { this.performanceStatistics?.stop(); } + if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { + this.stopReservationExpirationSetInterval(); + } this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.templateFileWatcher?.close(); this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash); @@ -757,13 +764,6 @@ export class ChargingStation { } } - public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined { - return getConfigurationKey( - this, - StandardParametersKey.SupportedFeatureProfiles, - )?.value?.includes(featureProfile); - } - public bufferMessage(message: string): void { this.messageBuffer.add(message); } @@ -775,7 +775,7 @@ export class ChargingStation { terminateOpened: false, }, ): void { - options = { handshakeTimeout: this.getConnectionTimeout() * 1000, ...options }; + options = { handshakeTimeout: secondsToMilliseconds(this.getConnectionTimeout()), ...options }; params = { ...{ closeOpened: false, terminateOpened: false }, ...params }; if (this.started === false && this.starting === false) { logger.warn( @@ -846,25 +846,28 @@ export class ChargingStation { } public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration { - let automaticTransactionGeneratorConfiguration: - | AutomaticTransactionGeneratorConfiguration - | undefined; - const automaticTransactionGeneratorConfigurationFromFile = - this.getConfigurationFromFile()?.automaticTransactionGenerator; - if ( - this.getAutomaticTransactionGeneratorPersistentConfiguration() && - automaticTransactionGeneratorConfigurationFromFile - ) { - automaticTransactionGeneratorConfiguration = - automaticTransactionGeneratorConfigurationFromFile; - } else { - automaticTransactionGeneratorConfiguration = - this.getTemplateFromFile()?.AutomaticTransactionGenerator; + if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration)) { + let automaticTransactionGeneratorConfiguration: + | AutomaticTransactionGeneratorConfiguration + | undefined; + const automaticTransactionGeneratorConfigurationFromFile = + this.getConfigurationFromFile()?.automaticTransactionGenerator; + if ( + this.getAutomaticTransactionGeneratorPersistentConfiguration() && + automaticTransactionGeneratorConfigurationFromFile + ) { + automaticTransactionGeneratorConfiguration = + automaticTransactionGeneratorConfigurationFromFile; + } else { + automaticTransactionGeneratorConfiguration = + this.getTemplateFromFile()?.AutomaticTransactionGenerator; + } + this.automaticTransactionGeneratorConfiguration = { + ...Constants.DEFAULT_ATG_CONFIGURATION, + ...automaticTransactionGeneratorConfiguration, + }; } - return { - ...Constants.DEFAULT_ATG_CONFIGURATION, - ...automaticTransactionGeneratorConfiguration, - }; + return this.automaticTransactionGeneratorConfiguration!; } public getAutomaticTransactionGeneratorStatuses(): Status[] | undefined { @@ -933,15 +936,15 @@ export class ChargingStation { ); } - public getReservationOnConnectorId0Enabled(): boolean { + public getReserveConnectorZeroSupported(): boolean { return convertToBoolean( getConfigurationKey(this, StandardParametersKey.ReserveConnectorZeroSupported)!.value, ); } public async addReservation(reservation: Reservation): Promise { - const [exists, reservationFound] = this.doesReservationExists(reservation); - if (exists) { + const reservationFound = this.getReservationBy('reservationId', reservation.reservationId); + if (!isUndefined(reservationFound)) { await this.removeReservation( reservationFound!, ReservationTerminationReason.REPLACE_EXISTING, @@ -964,8 +967,6 @@ export class ChargingStation { const connector = this.getConnectorStatus(reservation.connectorId)!; switch (reason) { case ReservationTerminationReason.CONNECTOR_STATE_CHANGED: - delete connector.reservation; - break; case ReservationTerminationReason.TRANSACTION_STARTED: delete connector.reservation; break; @@ -993,59 +994,55 @@ export class ChargingStation { if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { + if (connectorStatus?.reservation?.[filterKey] === value) { return connectorStatus.reservation; } } } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { + if (connectorStatus?.reservation?.[filterKey] === value) { return connectorStatus.reservation; } } } } - public doesReservationExists( - reservation: Partial, - ): [boolean, Reservation | undefined] { - const foundReservation = this.getReservationBy( - ReservationFilterKey.RESERVATION_ID, - reservation.id!, - ); - return isUndefined(foundReservation) ? [false, undefined] : [true, foundReservation]; - } - public startReservationExpirationSetInterval(customInterval?: number): void { const interval = customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_OBSERVATION_INTERVAL; - logger.info( - `${this.logPrefix()} Reservation expiration date interval is set to ${interval} - and starts on charging station now`, - ); if (interval > 0) { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.reservationExpirationSetInterval = setInterval(async (): Promise => { - const now = new Date(); + logger.info( + `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds( + interval, + )}`, + ); + this.reservationExpirationSetInterval = setInterval((): void => { + const currentDate = new Date(); if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus.reservation && connectorStatus.reservation.expiryDate < now) { - await this.removeReservation( + if ( + connectorStatus.reservation && + connectorStatus.reservation.expiryDate < currentDate + ) { + this.removeReservation( connectorStatus.reservation, ReservationTerminationReason.EXPIRED, - ); + ).catch(Constants.EMPTY_FUNCTION); } } } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus.reservation && connectorStatus.reservation.expiryDate < now) { - await this.removeReservation( + if ( + connectorStatus.reservation && + connectorStatus.reservation.expiryDate < currentDate + ) { + this.removeReservation( connectorStatus.reservation, ReservationTerminationReason.EXPIRED, - ); + ).catch(Constants.EMPTY_FUNCTION); } } } @@ -1058,27 +1055,24 @@ export class ChargingStation { this.startReservationExpirationSetInterval(); } - public validateIncomingRequestWithReservation(connectorId: number, idTag: string): boolean { - return this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId)?.idTag === idTag; - } - public isConnectorReservable( reservationId: number, idTag?: string, connectorId?: number, ): boolean { - const [alreadyExists] = this.doesReservationExists({ id: reservationId }); - if (alreadyExists) { - return alreadyExists; + const reservationExists = !isUndefined(this.getReservationBy('reservationId', reservationId)); + if (arguments.length === 1) { + return !reservationExists; + } else if (arguments.length > 1) { + const userReservationExists = + !isUndefined(idTag) && isUndefined(this.getReservationBy('idTag', idTag!)) ? false : true; + const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0; + const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0; + return ( + !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable + ); } - const userReservedAlready = isUndefined( - this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!), - ) - ? false - : true; - const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0; - const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0; - return !alreadyExists && !userReservedAlready && notConnectorZero && freeConnectorsAvailable; + return false; } private getNumberOfReservableConnectors(): number { @@ -1095,9 +1089,11 @@ export class ChargingStation { private getNumberOfReservationsOnConnectorZero(): number { let numberOfReservations = 0; - if (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation) { - ++numberOfReservations; - } else if (this.connectors.get(0)?.reservation) { + if ( + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation) || + (!this.hasEvses && this.connectors.get(0)?.reservation) + ) { ++numberOfReservations; } return numberOfReservations; @@ -1215,7 +1211,7 @@ export class ChargingStation { stationTemplate?.firmwareUpgrade ?? {}, ); stationInfo.resetTime = !isNullOrUndefined(stationTemplate?.resetTime) - ? stationTemplate.resetTime! * 1000 + ? secondsToMilliseconds(stationTemplate.resetTime!) : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo); return stationInfo; @@ -1329,7 +1325,7 @@ export class ChargingStation { if (this.stationInfo?.autoRegister === true) { this.bootNotificationResponse = { currentTime: new Date(), - interval: this.getHeartbeatInterval() / 1000, + interval: millisecondsToSeconds(this.getHeartbeatInterval()), status: RegistrationStatusEnumType.ACCEPTED, }; } @@ -1685,8 +1681,9 @@ export class ChargingStation { if (!existsSync(dirname(this.configurationFile))) { mkdirSync(dirname(this.configurationFile), { recursive: true }); } - let configurationData: ChargingStationConfiguration = - cloneObject(this.getConfigurationFromFile()!) ?? {}; + let configurationData: ChargingStationConfiguration = this.getConfigurationFromFile() + ? cloneObject(this.getConfigurationFromFile()!) + : {}; if (this.getStationInfoPersistentConfiguration() && this.stationInfo) { configurationData.stationInfo = this.stationInfo; } else { @@ -1814,7 +1811,7 @@ export class ChargingStation { this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount; await sleep( this?.bootNotificationResponse?.interval - ? this.bootNotificationResponse.interval * 1000 + ? secondsToMilliseconds(this.bootNotificationResponse.interval) : Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL, ); } @@ -2268,7 +2265,7 @@ export class ChargingStation { if (this.isWebSocketConnectionOpened() === true) { this.wsConnection?.ping(); } - }, webSocketPingInterval * 1000); + }, secondsToMilliseconds(webSocketPingInterval)); logger.info( `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds( webSocketPingInterval, @@ -2366,7 +2363,7 @@ export class ChargingStation { ++this.autoReconnectRetryCount; const reconnectDelay = this.getReconnectExponentialDelay() ? exponentialDelay(this.autoReconnectRetryCount) - : this.getConnectionTimeout() * 1000; + : secondsToMilliseconds(this.getConnectionTimeout()); const reconnectDelayWithdraw = 1000; const reconnectTimeout = reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0