X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FChargingStation.ts;h=46fae1811d4e0f7bdc7b435047bb60f9ff00c70a;hb=0d6f335fa2cc78fc960d485f48fac41e8047972e;hp=831c662ef761b2c288ce7cddc24553e6500cab92;hpb=e302df1d4573ef07df7c44a210a34e429f571814;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 831c662e..46fae181 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -53,6 +53,7 @@ import { type ErrorCallback, type ErrorResponse, ErrorType, + type EvseStatus, FileType, FirmwareStatus, type FirmwareStatusNotificationRequest, @@ -108,6 +109,7 @@ export class ChargingStation { public ocppConfiguration!: ChargingStationOcppConfiguration | undefined; public wsConnection!: WebSocket | null; public readonly connectors: Map; + public readonly evses: Map; public readonly requests: Map; public performanceStatistics!: PerformanceStatistics | undefined; public heartbeatSetInterval!: NodeJS.Timeout; @@ -119,6 +121,7 @@ export class ChargingStation { private configurationFile!: string; private configurationFileHash!: string; private connectorsConfigurationHash!: string; + private evsesConfigurationHash!: string; private ocppIncomingRequestService!: OCPPIncomingRequestService; private readonly messageBuffer: Set; private configuredSupervisionUrl!: URL; @@ -138,6 +141,7 @@ export class ChargingStation { this.index = index; this.templateFile = templateFile; this.connectors = new Map(); + this.evses = new Map(); this.requests = new Map(); this.messageBuffer = new Set(); this.sharedLRUCache = SharedLRUCache.getInstance(); @@ -233,11 +237,11 @@ export class ChargingStation { } public isChargingStationAvailable(): boolean { - return this.getConnectorStatus(0)?.availability === AvailabilityType.OPERATIVE; + return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative; } public isConnectorAvailable(id: number): boolean { - return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.OPERATIVE; + return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.Operative; } public getNumberOfConnectors(): number { @@ -471,19 +475,19 @@ 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.toString()}` ); return; } if (!this.getConnectorStatus(connectorId)) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}` + `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}` ); return; } if (this.getConnectorStatus(connectorId)?.transactionStarted === false) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started` + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started` ); return; } else if ( @@ -491,7 +495,7 @@ export class ChargingStation { Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId) ) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id` + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id` ); return; } @@ -796,7 +800,7 @@ export class ChargingStation { private flushMessageBuffer(): void { if (this.messageBuffer.size > 0) { - this.messageBuffer.forEach((message) => { + for (const message of this.messageBuffer.values()) { let beginId: string; let commandName: RequestCommand; const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse; @@ -813,7 +817,7 @@ export class ChargingStation { )} payload sent: ${message}` ); this.messageBuffer.delete(message); - }); + } } } @@ -866,30 +870,10 @@ export class ChargingStation { logger.error(`${this.logPrefix()} ${errorMsg}`); throw new BaseError(errorMsg); } - // Deprecation template keys section - ChargingStationUtils.warnDeprecatedTemplateKey( - stationTemplate, - 'supervisionUrl', + ChargingStationUtils.warnTemplateKeysDeprecation( this.templateFile, - this.logPrefix(), - "Use 'supervisionUrls' instead" - ); - ChargingStationUtils.convertDeprecatedTemplateKey( - stationTemplate, - 'supervisionUrl', - 'supervisionUrls' - ); - ChargingStationUtils.warnDeprecatedTemplateKey( - stationTemplate, - 'authorizationFile', - this.templateFile, - this.logPrefix(), - "Use 'idTagsFile' instead" - ); - ChargingStationUtils.convertDeprecatedTemplateKey( stationTemplate, - 'authorizationFile', - 'idTagsFile' + this.logPrefix() ); const stationInfo: ChargingStationInfo = ChargingStationUtils.stationTemplateToStationInfo(stationTemplate); @@ -966,6 +950,7 @@ export class ChargingStation { } // Build connectors if needed (FIXME: should be factored out) this.initializeConnectors(stationInfo, configuredMaxConnectors, templateMaxConnectors); + this.initializeEvses(stationInfo); stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo); ChargingStationUtils.createStationInfoHash(stationInfo); return stationInfo; @@ -982,7 +967,10 @@ export class ChargingStation { private getStationInfo(): ChargingStationInfo { const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate(); const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile(); - // Priority: charging station info from template > charging station info from configuration file > charging station info attribute + // Priority: + // 1. charging station info from template + // 2. charging station info from configuration file + // 3. charging station info attribute if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) { if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) { return this.stationInfo; @@ -1272,7 +1260,7 @@ export class ChargingStation { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile - } with no connector Id 0 configuration` + } with no connector id 0 configuration` ); } if (stationInfo?.Connectors) { @@ -1285,7 +1273,7 @@ export class ChargingStation { if (this.connectors?.size === 0 || connectorsConfigChanged) { connectorsConfigChanged && this.connectors.clear(); this.connectorsConfigurationHash = connectorsConfigHash; - // Add connector Id 0 + // Add connector id 0 let lastConnector = '0'; for (lastConnector in stationInfo?.Connectors) { const connectorStatus = stationInfo?.Connectors[lastConnector]; @@ -1300,7 +1288,7 @@ export class ChargingStation { lastConnectorId, Utils.cloneObject(connectorStatus) ); - this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE; + this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.Operative; if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) { this.getConnectorStatus(lastConnectorId).chargingProfiles = []; } @@ -1315,7 +1303,7 @@ export class ChargingStation { const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()]; this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus); this.connectors.set(index, Utils.cloneObject(connectorStatus)); - this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE; + this.getConnectorStatus(index).availability = AvailabilityType.Operative; if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) { this.getConnectorStatus(index).chargingProfiles = []; } @@ -1340,14 +1328,62 @@ export class ChargingStation { } if ( connectorId > 0 && - (this.getConnectorStatus(connectorId)?.transactionStarted === undefined || - this.getConnectorStatus(connectorId)?.transactionStarted === null) + Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted) ) { this.initializeConnectorStatus(connectorId); } } } + private initializeEvses(stationInfo: ChargingStationInfo): void { + if (!stationInfo?.Evses && this.evses.size === 0) { + const logMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`; + logger.warn(`${this.logPrefix()} ${logMsg}`); + return; + } + if (!stationInfo?.Evses[0]) { + logger.warn( + `${this.logPrefix()} Charging station information from template ${ + this.templateFile + } with no evse id 0 configuration` + ); + } + if (stationInfo?.Evses) { + const evsesConfigHash = crypto + .createHash(Constants.DEFAULT_HASH_ALGORITHM) + .update(`${JSON.stringify(stationInfo?.Evses)}`) + .digest('hex'); + const evsesConfigChanged = + this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash; + if (this.evses?.size === 0 || evsesConfigChanged) { + evsesConfigChanged && this.evses.clear(); + this.evsesConfigurationHash = evsesConfigHash; + for (const evse in stationInfo?.Evses) { + const evseId = Utils.convertToInt(evse); + this.evses.set(evseId, Utils.cloneObject(stationInfo?.Evses[evse])); + this.evses.get(evseId).availability = AvailabilityType.Operative; + } + } + } else { + if (this.connectors.size === 0) { + const logMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no evses configuration defined`; + logger.error(`${this.logPrefix()} ${logMsg}`); + throw new BaseError(logMsg); + } + logger.info( + `${this.logPrefix()} Charging station information from template ${ + this.templateFile + } with no evses configuration defined, mapping one connector to one evse` + ); + for (const [connectorId, connectorStatus] of this.connectors) { + this.evses.set(connectorId, { + connectorIds: [connectorId], + availability: connectorStatus.availability, + }); + } + } + } + private checkStationInfoConnectorStatus( connectorId: number, connectorStatus: ConnectorStatus @@ -1865,15 +1901,7 @@ export class ChargingStation { // Set default status connectorStatus = ConnectorStatusEnum.Available; } - await this.ocppRequestService.requestHandler< - StatusNotificationRequest, - StatusNotificationResponse - >( - this, - RequestCommand.STATUS_NOTIFICATION, - OCPPServiceUtils.buildStatusNotificationRequest(this, connectorId, connectorStatus) - ); - this.getConnectorStatus(connectorId).status = connectorStatus; + await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorStatus); } if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) { await this.ocppRequestService.requestHandler<