X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=84930a5f628be1f99ed0e010778b55474961729c;hb=2cc5d5ec705dae9c7b88be0733dddc22d652a17e;hp=f30bf18952b8a5233a3475cc20da2883756f723e;hpb=cda962601b73f2de23fa089ee102160dd055af2e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f30bf189..84930a5f 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -36,7 +36,6 @@ import type { ChargingStationOcppConfiguration } from '../types/ChargingStationO import { type ChargingStationTemplate, CurrentType, - type FirmwareUpgrade, PowerUnits, type WsOptions, } from '../types/ChargingStationTemplate'; @@ -44,7 +43,6 @@ import { SupervisionUrlDistribution } from '../types/ConfigurationData'; import type { ConnectorStatus } from '../types/ConnectorStatus'; import { FileType } from '../types/FileType'; import type { JsonType } from '../types/JsonType'; -import { ChargingProfile, ChargingRateUnitType } from '../types/ocpp/ChargingProfile'; import { ConnectorPhaseRotation, StandardParametersKey, @@ -266,6 +264,11 @@ export default class ChargingStation { : defaultVoltageOut; } + public getMaximumPower(stationInfo?: ChargingStationInfo): number { + const localStationInfo = stationInfo ?? this.stationInfo; + return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower; + } + public getConnectorMaximumAvailablePower(connectorId: number): number { let connectorAmperageLimitationPowerLimit: number; if ( @@ -283,13 +286,14 @@ export default class ChargingStation { this.powerDivider; } const connectorMaximumPower = this.getMaximumPower() / this.powerDivider; - const connectorChargingProfilePowerLimit = this.getChargingProfilePowerLimit(connectorId); + const connectorChargingProfilesPowerLimit = + ChargingStationUtils.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId); return Math.min( isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, isNaN(connectorAmperageLimitationPowerLimit) ? Infinity : connectorAmperageLimitationPowerLimit, - isNaN(connectorChargingProfilePowerLimit) ? Infinity : connectorChargingProfilePowerLimit + isNaN(connectorChargingProfilesPowerLimit) ? Infinity : connectorChargingProfilesPowerLimit ); } @@ -342,16 +346,16 @@ export default class ChargingStation { public getEnergyActiveImportRegisterByTransactionId( transactionId: number, - meterStop = false + rounded = false ): number { return this.getEnergyActiveImportRegister( this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)), - meterStop + rounded ); } - public getEnergyActiveImportRegisterByConnectorId(connectorId: number): number { - return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId)); + public getEnergyActiveImportRegisterByConnectorId(connectorId: number, rounded = false): number { + return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId), rounded); } public getAuthorizeRemoteTxRequests(): boolean { @@ -378,12 +382,15 @@ export default class ChargingStation { this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval ) { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.requestHandler( - this, - RequestCommand.HEARTBEAT - ); + this.heartbeatSetInterval = setInterval(() => { + this.ocppRequestService + .requestHandler(this, RequestCommand.HEARTBEAT) + .catch((error) => { + logger.error( + `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`, + error + ); + }); }, this.getHeartbeatInterval()); logger.info( this.logPrefix() + @@ -449,18 +456,16 @@ export default class ChargingStation { return; } if (interval > 0) { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - this.getConnectorStatus(connectorId).transactionSetInterval = setInterval( - // eslint-disable-next-line @typescript-eslint/no-misused-promises - async (): Promise => { - // FIXME: Implement OCPP version agnostic helpers - const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue( - this, - connectorId, - this.getConnectorStatus(connectorId).transactionId, - interval - ); - await this.ocppRequestService.requestHandler( + this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(() => { + // FIXME: Implement OCPP version agnostic helpers + const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue( + this, + connectorId, + this.getConnectorStatus(connectorId).transactionId, + interval + ); + this.ocppRequestService + .requestHandler( this, RequestCommand.METER_VALUES, { @@ -468,10 +473,14 @@ export default class ChargingStation { transactionId: this.getConnectorStatus(connectorId).transactionId, meterValue: [meterValue], } - ); - }, - interval - ); + ) + .catch((error) => { + logger.error( + `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`, + error + ); + }); + }, interval); } else { logger.error( `${this.logPrefix()} Charging station ${ @@ -1559,7 +1568,7 @@ export default class ChargingStation { logger.debug( `${this.logPrefix()} << Command '${ requestCommandName ?? Constants.UNKNOWN_COMMAND - }' received error payload: ${JSON.stringify(request)}` + }' received error response payload: ${JSON.stringify(request)}` ); errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)); break; @@ -1633,19 +1642,16 @@ export default class ChargingStation { logger.error(this.logPrefix() + ' WebSocket error:', error); } - private getEnergyActiveImportRegister( - connectorStatus: ConnectorStatus, - meterStop = false - ): number { + private getEnergyActiveImportRegister(connectorStatus: ConnectorStatus, rounded = false): number { if (this.getMeteringPerTransaction() === true) { return ( - (meterStop === true + (rounded === true ? Math.round(connectorStatus?.transactionEnergyActiveImportRegisterValue) : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0 ); } return ( - (meterStop === true + (rounded === true ? Math.round(connectorStatus?.energyActiveImportRegisterValue) : connectorStatus?.energyActiveImportRegisterValue) ?? 0 ); @@ -1723,11 +1729,6 @@ export default class ChargingStation { return powerDivider; } - private getMaximumPower(stationInfo?: ChargingStationInfo): number { - const localStationInfo = stationInfo ?? this.stationInfo; - return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower; - } - private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined { const maximumPower = this.getMaximumPower(stationInfo); switch (this.getCurrentOutType(stationInfo)) { @@ -1761,57 +1762,6 @@ 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 ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}, dump charging profiles' stack: %j`, - this.getConnectorStatus(connectorId).chargingProfiles - ); - limit = connectorMaximumPower; - } - } - } - return limit; - } - private async startMessageSequence(): Promise { if (this.stationInfo?.autoRegister === true) { await this.ocppRequestService.requestHandler<