X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=a47bc68182a77de166b1dc9fd273a7d31d59265b;hb=f90c1757e0620ff455e9cd0f90245872b9d77b01;hp=d771ebe08ab969147e65dcc2d7efcaa726ed6c2e;hpb=03ebf4c1db6ba11903b42e56692ed3d8538ba1d3;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d771ebe0..a47bc681 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -96,12 +96,12 @@ export default class ChargingStation { public performanceStatistics!: PerformanceStatistics; public heartbeatSetInterval!: NodeJS.Timeout; public ocppRequestService!: OCPPRequestService; + public bootNotificationRequest!: BootNotificationRequest; public bootNotificationResponse!: BootNotificationResponse | null; public powerDivider!: number; private readonly index: number; private configurationFile!: string; private configurationFileHash!: string; - private bootNotificationRequest!: BootNotificationRequest; private connectorsConfigurationHash!: string; private ocppIncomingRequestService!: OCPPIncomingRequestService; private readonly messageBuffer: Set; @@ -152,10 +152,6 @@ export default class ChargingStation { ); } - public getBootNotificationRequest(): BootNotificationRequest { - return this.bootNotificationRequest; - } - public getRandomIdTag(): string { const authorizationFile = ChargingStationUtils.getAuthorizationFile(this.stationInfo); const index = Math.floor( @@ -507,7 +503,9 @@ export default class ChargingStation { this.initialize(); // Restart the ATG this.stopAutomaticTransactionGenerator(); - this.startAutomaticTransactionGenerator(); + if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable) { + this.startAutomaticTransactionGenerator(); + } if (this.getEnableStatistics()) { this.performanceStatistics.restart(); } else { @@ -707,8 +705,15 @@ export default class ChargingStation { break; } + if (this.isWebSocketConnectionOpened()) { + logger.warn( + `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened` + ); + return; + } + logger.info( - this.logPrefix() + ' Open OCPP connection to URL ' + this.wsConnectionUrl.toString() + `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}` ); this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options); @@ -743,7 +748,26 @@ export default class ChargingStation { } } - private flushMessageBuffer() { + public startAutomaticTransactionGenerator(): void { + if (!this.automaticTransactionGenerator) { + this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance( + this.getAutomaticTransactionGeneratorConfigurationFromTemplate(), + this + ); + } + if (!this.automaticTransactionGenerator.started) { + this.automaticTransactionGenerator.start(); + } + } + + public stopAutomaticTransactionGenerator(): void { + if (this.automaticTransactionGenerator?.started) { + this.automaticTransactionGenerator.stop(); + this.automaticTransactionGenerator = null; + } + } + + private flushMessageBuffer(): void { if (this.messageBuffer.size > 0) { this.messageBuffer.forEach((message) => { // TODO: evaluate the need to track performance @@ -1742,27 +1766,8 @@ export default class ChargingStation { } } // Start the ATG - this.startAutomaticTransactionGenerator(); - } - - private startAutomaticTransactionGenerator() { if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable) { - if (!this.automaticTransactionGenerator) { - this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance( - this.getAutomaticTransactionGeneratorConfigurationFromTemplate(), - this - ); - } - if (!this.automaticTransactionGenerator.started) { - this.automaticTransactionGenerator.start(); - } - } - } - - private stopAutomaticTransactionGenerator(): void { - if (this.automaticTransactionGenerator?.started) { - this.automaticTransactionGenerator.stop(); - this.automaticTransactionGenerator = null; + this.startAutomaticTransactionGenerator(); } } @@ -1774,42 +1779,44 @@ export default class ChargingStation { // Stop heartbeat this.stopHeartbeat(); // Stop ongoing transactions - if (this.automaticTransactionGenerator?.configuration?.enable) { - this.stopAutomaticTransactionGenerator(); - } else { - for (const connectorId of this.connectors.keys()) { - if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) { - const transactionId = this.getConnectorStatus(connectorId).transactionId; - if ( - this.getBeginEndMeterValues() && - this.getOcppStrictCompliance() && - !this.getOutOfOrderEndMeterValues() - ) { - // FIXME: Implement OCPP version agnostic helpers - const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( - this, - connectorId, - this.getEnergyActiveImportRegisterByTransactionId(transactionId) - ); - await this.ocppRequestService.requestHandler( - this, - RequestCommand.METER_VALUES, - { + if (this.getNumberOfRunningTransactions() > 0) { + if (this.automaticTransactionGenerator?.started) { + this.stopAutomaticTransactionGenerator(); + } else { + for (const connectorId of this.connectors.keys()) { + if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) { + const transactionId = this.getConnectorStatus(connectorId).transactionId; + if ( + this.getBeginEndMeterValues() && + this.getOcppStrictCompliance() && + !this.getOutOfOrderEndMeterValues() + ) { + // FIXME: Implement OCPP version agnostic helpers + const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( + this, connectorId, - transactionId, - meterValue: [transactionEndMeterValue], - } - ); + this.getEnergyActiveImportRegisterByTransactionId(transactionId) + ); + await this.ocppRequestService.requestHandler( + this, + RequestCommand.METER_VALUES, + { + connectorId, + transactionId, + meterValue: [transactionEndMeterValue], + } + ); + } + await this.ocppRequestService.requestHandler< + StopTransactionRequest, + StopTransactionResponse + >(this, RequestCommand.STOP_TRANSACTION, { + transactionId, + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.getTransactionIdTag(transactionId), + reason, + }); } - await this.ocppRequestService.requestHandler< - StopTransactionRequest, - StopTransactionResponse - >(this, RequestCommand.STOP_TRANSACTION, { - transactionId, - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.getTransactionIdTag(transactionId), - reason, - }); } } }