From 60ddad538d0a01ece43f4f70928a9decf3531dda Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 3 Sep 2022 23:10:48 +0200 Subject: [PATCH] UI HTTP server: fix stop simulator response handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 1 - src/charging-station/ChargingStation.ts | 161 +++++++++--------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 7 +- .../ocpp/OCPPIncomingRequestService.ts | 2 +- .../ocpp/OCPPRequestService.ts | 2 +- .../ui-server/UIHttpServer.ts | 1 - 6 files changed, 82 insertions(+), 92 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index eb7876e2..83bedaab 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -226,7 +226,6 @@ export default class AutomaticTransactionGenerator { } this.connectorsStatus.get(connectorId).lastRunDate = new Date(); } - // await this.stopTransaction(connectorId); this.connectorsStatus.get(connectorId).stoppedDate = new Date(); logger.info( this.logPrefix(connectorId) + diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 7d633eb0..3b83dfe4 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -498,7 +498,7 @@ export default class ChargingStation { this.initialize(); // Restart the ATG this.stopAutomaticTransactionGenerator(); - if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable) { + if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true) { this.startAutomaticTransactionGenerator(); } if (this.getEnableStatistics()) { @@ -519,7 +519,8 @@ export default class ChargingStation { parentPort.postMessage(MessageChannelUtils.buildStartedMessage(this)); } - public async stop(): Promise { + public async stop(reason?: StopTransactionReason): Promise { + await this.stopMessageSequence(reason); for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { await this.ocppRequestService.requestHandler< @@ -545,8 +546,8 @@ export default class ChargingStation { parentPort.postMessage(MessageChannelUtils.buildStoppedMessage(this)); } - public async reset(): Promise { - await this.stop(); + public async reset(reason?: StopTransactionReason): Promise { + await this.stop(reason); await Utils.sleep(this.stationInfo.resetTime); this.initialize(); this.start(); @@ -558,57 +559,6 @@ export default class ChargingStation { } } - public getChargingProfilePowerLimit(connectorId: number): number | undefined { - let limit: number, matchingChargingProfile: ChargingProfile; - let chargingProfiles: ChargingProfile[] = []; - // Get charging profiles for connector and sort by stack level - chargingProfiles = this.getConnectorStatus(connectorId).chargingProfiles.sort( - (a, b) => b.stackLevel - a.stackLevel - ); - // Get profiles on connector 0 - if (this.getConnectorStatus(0).chargingProfiles) { - chargingProfiles.push( - ...this.getConnectorStatus(0).chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel) - ); - } - if (!Utils.isEmptyArray(chargingProfiles)) { - const result = ChargingStationUtils.getLimitFromChargingProfiles( - chargingProfiles, - this.logPrefix() - ); - if (!Utils.isNullOrUndefined(result)) { - limit = result.limit; - matchingChargingProfile = result.matchingChargingProfile; - switch (this.getCurrentOutType()) { - case CurrentType.AC: - limit = - matchingChargingProfile.chargingSchedule.chargingRateUnit === - ChargingRateUnitType.WATT - ? limit - : ACElectricUtils.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit); - break; - case CurrentType.DC: - limit = - matchingChargingProfile.chargingSchedule.chargingRateUnit === - ChargingRateUnitType.WATT - ? limit - : DCElectricUtils.power(this.getVoltageOut(), limit); - } - const connectorMaximumPower = this.getMaximumPower() / this.powerDivider; - if (limit > connectorMaximumPower) { - logger.error( - `${this.logPrefix()} Charging profile id ${ - matchingChargingProfile.chargingProfileId - } limit is greater than connector id ${connectorId} maximum, dump charging profiles' stack: %j`, - this.getConnectorStatus(connectorId).chargingProfiles - ); - limit = connectorMaximumPower; - } - } - } - return limit; - } - public setChargingProfile(connectorId: number, cp: ChargingProfile): void { if (Utils.isNullOrUndefined(this.getConnectorStatus(connectorId).chargingProfiles)) { logger.error( @@ -768,24 +718,6 @@ export default class ChargingStation { } } - public getNumberOfRunningTransactions(): number { - let trxCount = 0; - for (const connectorId of this.connectors.keys()) { - if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { - trxCount++; - } - } - return trxCount; - } - - public async stopRunningTransactions(reason = StopTransactionReason.NONE): Promise { - for (const connectorId of this.connectors.keys()) { - if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { - await this.stopTransactionOnConnector(connectorId, reason); - } - } - } - public async stopTransactionOnConnector( connectorId: number, reason = StopTransactionReason.NONE @@ -1468,7 +1400,6 @@ export default class ChargingStation { )}' and reason '${reason}'` ); this.autoReconnectRetryCount = 0; - await this.stopMessageSequence(StopTransactionReason.OTHER); break; // Abnormal close default: @@ -1663,6 +1594,24 @@ export default class ChargingStation { : true; } + private getNumberOfRunningTransactions(): number { + let trxCount = 0; + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { + trxCount++; + } + } + return trxCount; + } + + private async stopRunningTransactions(reason = StopTransactionReason.NONE): Promise { + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { + await this.stopTransactionOnConnector(connectorId, reason); + } + } + } + // 0 for disabling private getConnectionTimeout(): number | undefined { if ( @@ -1748,6 +1697,57 @@ export default class ChargingStation { } } + private getChargingProfilePowerLimit(connectorId: number): number | undefined { + let limit: number, matchingChargingProfile: ChargingProfile; + let chargingProfiles: ChargingProfile[] = []; + // Get charging profiles for connector and sort by stack level + chargingProfiles = this.getConnectorStatus(connectorId).chargingProfiles.sort( + (a, b) => b.stackLevel - a.stackLevel + ); + // Get profiles on connector 0 + if (this.getConnectorStatus(0).chargingProfiles) { + chargingProfiles.push( + ...this.getConnectorStatus(0).chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel) + ); + } + if (!Utils.isEmptyArray(chargingProfiles)) { + const result = ChargingStationUtils.getLimitFromChargingProfiles( + chargingProfiles, + this.logPrefix() + ); + if (!Utils.isNullOrUndefined(result)) { + limit = result.limit; + matchingChargingProfile = result.matchingChargingProfile; + switch (this.getCurrentOutType()) { + case CurrentType.AC: + limit = + matchingChargingProfile.chargingSchedule.chargingRateUnit === + ChargingRateUnitType.WATT + ? limit + : ACElectricUtils.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit); + break; + case CurrentType.DC: + limit = + matchingChargingProfile.chargingSchedule.chargingRateUnit === + ChargingRateUnitType.WATT + ? limit + : DCElectricUtils.power(this.getVoltageOut(), limit); + } + const connectorMaximumPower = this.getMaximumPower() / this.powerDivider; + if (limit > connectorMaximumPower) { + logger.error( + `${this.logPrefix()} Charging profile id ${ + matchingChargingProfile.chargingProfileId + } limit is greater than connector id ${connectorId} maximum, dump charging profiles' stack: %j`, + this.getConnectorStatus(connectorId).chargingProfiles + ); + limit = connectorMaximumPower; + } + } + } + return limit; + } + private async startMessageSequence(): Promise { if (this.stationInfo?.autoRegister) { await this.ocppRequestService.requestHandler< @@ -1834,7 +1834,7 @@ export default class ChargingStation { } } // Start the ATG - if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable) { + if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true) { this.startAutomaticTransactionGenerator(); } } @@ -1847,12 +1847,10 @@ export default class ChargingStation { // Stop heartbeat this.stopHeartbeat(); // Stop ongoing transactions - if (this.getNumberOfRunningTransactions() > 0) { - if (this.automaticTransactionGenerator?.started) { - this.stopAutomaticTransactionGenerator(); - } else { - await this.stopRunningTransactions(reason); - } + if (this.automaticTransactionGenerator?.started) { + this.stopAutomaticTransactionGenerator(); + } else { + await this.stopRunningTransactions(reason); } } @@ -2031,7 +2029,6 @@ export default class ChargingStation { ); this.wsConnectionRestarted = true; } else if (this.getAutoReconnectMaxRetries() !== -1) { - await this.stopMessageSequence(StopTransactionReason.OTHER); logger.error( `${this.logPrefix()} WebSocket reconnect failure: maximum retries reached (${ this.autoReconnectRetryCount diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index e80d0d3d..6c11ad57 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -380,12 +380,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ): DefaultResponse { // eslint-disable-next-line @typescript-eslint/no-misused-promises setImmediate(async (): Promise => { - if (chargingStation.getNumberOfRunningTransactions() > 0) { - await chargingStation.stopRunningTransactions( - (commandPayload.type + 'Reset') as OCPP16StopTransactionReason - ); - } - await chargingStation.reset(); + await chargingStation.reset((commandPayload.type + 'Reset') as OCPP16StopTransactionReason); }); logger.info( `${chargingStation.logPrefix()} ${ diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 2ee0465c..013cb62a 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -37,7 +37,7 @@ export default abstract class OCPPIncomingRequestService { params: HandleErrorParams = { throwError: true } ): T { logger.error( - `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command ${commandName} error:`, + `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, error ); if (!params?.throwError && params?.errorResponse) { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 25c89435..6993e1ec 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -361,7 +361,7 @@ export default abstract class OCPPRequestService { error: Error, params: HandleErrorParams = { throwError: true } ): void { - logger.error(`${chargingStation.logPrefix()} Request command ${commandName} error:`, error); + logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); if (params?.throwError) { throw error; } diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 2747738a..a6a6313e 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -41,7 +41,6 @@ export default class UIHttpServer extends AbstractUIServer { public stop(): void { this.chargingStations.clear(); - this.responseHandlers.clear(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars -- 2.34.1