X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=457369ff1ad029f30046e47432cb40c3546cc551;hb=0a48f2b2d735de604a1604fd0e191212b355602f;hp=118805fefa4f72f2277e638d09842db1b618e611;hpb=42a3eee7406d9ebfe5afd43c3065345701542d67;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 118805fe..457369ff 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1,12 +1,16 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +import { ACElectricUtils, DCElectricUtils } from '../utils/ElectricUtils'; import { AvailabilityType, BootNotificationRequest, CachedRequest, + HeartbeatRequest, IncomingRequest, IncomingRequestCommand, + MeterValuesRequest, RequestCommand, + StatusNotificationRequest, } from '../types/ocpp/Requests'; import { BootNotificationResponse, @@ -15,11 +19,17 @@ import { RegistrationStatus, StatusNotificationResponse, } from '../types/ocpp/Responses'; +import { + ChargingProfile, + ChargingRateUnitType, + ChargingSchedulePeriod, +} from '../types/ocpp/ChargingProfile'; import ChargingStationConfiguration, { Section } from '../types/ChargingStationConfiguration'; import ChargingStationOcppConfiguration, { ConfigurationKey, } from '../types/ChargingStationOcppConfiguration'; import ChargingStationTemplate, { + AmpereUnits, CurrentType, PowerUnits, Voltage, @@ -32,14 +42,17 @@ import { VendorDefaultParametersKey, } from '../types/ocpp/Configuration'; import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; -import { StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; +import { + StopTransactionReason, + StopTransactionRequest, + StopTransactionResponse, +} from '../types/ocpp/Transaction'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; import WebSocket, { Data, OPEN, RawData } from 'ws'; import AutomaticTransactionGenerator from './AutomaticTransactionGenerator'; import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode'; import { ChargePointStatus } from '../types/ocpp/ChargePointStatus'; -import { ChargingProfile } from '../types/ocpp/ChargingProfile'; import ChargingStationInfo from '../types/ChargingStationInfo'; import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import Configuration from '../utils/Configuration'; @@ -229,6 +242,35 @@ export default class ChargingStation { : defaultVoltageOut; } + public getConnectorMaximumAvailablePower(connectorId: number): number { + let connectorAmperageLimitationPowerLimit: number; + if ( + !Utils.isNullOrUndefined(this.getAmperageLimitation()) && + this.getAmperageLimitation() < this.stationInfo.maximumAmperage + ) { + connectorAmperageLimitationPowerLimit = + (this.getCurrentOutType() === CurrentType.AC + ? ACElectricUtils.powerTotal( + this.getNumberOfPhases(), + this.getVoltageOut(), + this.getAmperageLimitation() * this.getNumberOfConnectors() + ) + : DCElectricUtils.power(this.getVoltageOut(), this.getAmperageLimitation())) / + this.stationInfo.powerDivider; + } + const connectorMaximumPower = + ((this.stationInfo['maxPower'] as number) ?? this.stationInfo.maximumPower) / + this.stationInfo.powerDivider; + const connectorChargingProfilePowerLimit = this.getChargingProfilePowerLimit(connectorId); + return Math.min( + isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, + isNaN(connectorAmperageLimitationPowerLimit) + ? Infinity + : connectorAmperageLimitationPowerLimit, + isNaN(connectorChargingProfilePowerLimit) ? Infinity : connectorChargingProfilePowerLimit + ); + } + public getTransactionIdTag(transactionId: number): string | undefined { for (const connectorId of this.connectors.keys()) { if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) { @@ -327,7 +369,7 @@ export default class ChargingStation { } if ( measurand !== MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && - !this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes( + !this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes( measurand ) ) { @@ -358,7 +400,7 @@ export default class ChargingStation { phase && sampledValueTemplates[index]?.phase === phase && sampledValueTemplates[index]?.measurand === measurand && - this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes( + this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes( measurand ) ) { @@ -367,7 +409,7 @@ export default class ChargingStation { !phase && !sampledValueTemplates[index].phase && sampledValueTemplates[index]?.measurand === measurand && - this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData).value.includes( + this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes( measurand ) ) { @@ -402,7 +444,7 @@ export default class ChargingStation { ) { // eslint-disable-next-line @typescript-eslint/no-misused-promises this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.HEARTBEAT ); }, this.getHeartbeatInterval()); @@ -474,7 +516,7 @@ export default class ChargingStation { this.getConnectorStatus(connectorId).transactionId, interval ); - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.METER_VALUES, { connectorId, @@ -579,14 +621,14 @@ export default class ChargingStation { await this.stopMessageSequence(reason); for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: ChargePointStatus.UNAVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - } - ); + await this.ocppRequestService.sendMessageHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: ChargePointStatus.UNAVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE; } } @@ -686,6 +728,82 @@ export default class ChargingStation { } } + public getChargingProfilePowerLimit(connectorId: number): number | undefined { + const timestamp = new Date().getTime(); + let matchingChargingProfile: ChargingProfile; + let chargingSchedulePeriods: ChargingSchedulePeriod[] = []; + if (!Utils.isEmptyArray(this.getConnectorStatus(connectorId)?.chargingProfiles)) { + const chargingProfiles: ChargingProfile[] = this.getConnectorStatus( + connectorId + ).chargingProfiles.filter( + (chargingProfile) => + timestamp >= chargingProfile.chargingSchedule?.startSchedule.getTime() && + timestamp < + chargingProfile.chargingSchedule?.startSchedule.getTime() + + chargingProfile.chargingSchedule.duration * 1000 && + chargingProfile?.stackLevel === Math.max(...chargingProfiles.map((cp) => cp?.stackLevel)) + ); + if (!Utils.isEmptyArray(chargingProfiles)) { + for (const chargingProfile of chargingProfiles) { + if (!Utils.isEmptyArray(chargingProfile.chargingSchedule.chargingSchedulePeriod)) { + chargingSchedulePeriods = + chargingProfile.chargingSchedule.chargingSchedulePeriod.filter( + (chargingSchedulePeriod, index) => { + timestamp >= + chargingProfile.chargingSchedule.startSchedule.getTime() + + chargingSchedulePeriod.startPeriod * 1000 && + ((chargingProfile.chargingSchedule.chargingSchedulePeriod[index + 1] && + timestamp < + chargingProfile.chargingSchedule.startSchedule.getTime() + + chargingProfile.chargingSchedule.chargingSchedulePeriod[index + 1] + ?.startPeriod * + 1000) || + !chargingProfile.chargingSchedule.chargingSchedulePeriod[index + 1]); + } + ); + if (!Utils.isEmptyArray(chargingSchedulePeriods)) { + matchingChargingProfile = chargingProfile; + break; + } + } + } + } + } + let limit: number; + if (!Utils.isEmptyArray(chargingSchedulePeriods)) { + switch (this.getCurrentOutType()) { + case CurrentType.AC: + limit = + matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT + ? chargingSchedulePeriods[0].limit + : ACElectricUtils.powerTotal( + this.getNumberOfPhases(), + this.getVoltageOut(), + chargingSchedulePeriods[0].limit + ); + break; + case CurrentType.DC: + limit = + matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT + ? chargingSchedulePeriods[0].limit + : DCElectricUtils.power(this.getVoltageOut(), chargingSchedulePeriods[0].limit); + } + } + const connectorMaximumPower = + ((this.stationInfo['maxPower'] as number) ?? this.stationInfo.maximumPower) / + this.stationInfo.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 { let cpReplaced = false; if (!Utils.isEmptyArray(this.getConnectorStatus(connectorId).chargingProfiles)) { @@ -719,6 +837,12 @@ export default class ChargingStation { this.stopMeterValues(connectorId); } + public hasFeatureProfile(featureProfile: SupportedFeatureProfiles) { + return this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)?.value.includes( + featureProfile + ); + } + public bufferMessage(message: string): void { this.messageBuffer.add(message); } @@ -802,6 +926,8 @@ export default class ChargingStation { (stationInfo.chargePointSerialNumber = existingStationInfo.chargePointSerialNumber); existingStationInfo?.chargeBoxSerialNumber && (stationInfo.chargeBoxSerialNumber = existingStationInfo.chargeBoxSerialNumber); + existingStationInfo?.meterSerialNumber && + (stationInfo.meterSerialNumber = existingStationInfo.meterSerialNumber); } else { const serialNumberSuffix = params?.randomSerialNumber ? this.getRandomSerialNumberSuffix({ upperCase: params.randomSerialNumberUpperCase }) @@ -812,6 +938,9 @@ export default class ChargingStation { stationInfo.chargeBoxSerialNumber = stationInfo?.chargeBoxSerialNumberPrefix && stationInfo.chargeBoxSerialNumberPrefix + serialNumberSuffix; + stationInfo.meterSerialNumber = + stationInfo?.meterSerialNumberPrefix && + stationInfo.meterSerialNumberPrefix + serialNumberSuffix; } } @@ -835,13 +964,13 @@ export default class ChargingStation { if (!Utils.isEmptyArray(stationInfo.power)) { stationInfo.power = stationInfo.power as number[]; const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationInfo.power.length); - stationInfo.maxPower = + stationInfo.maximumPower = stationInfo.powerUnit === PowerUnits.KILO_WATT ? stationInfo.power[powerArrayRandomIndex] * 1000 : stationInfo.power[powerArrayRandomIndex]; } else { stationInfo.power = stationInfo.power as number; - stationInfo.maxPower = + stationInfo.maximumPower = stationInfo.powerUnit === PowerUnits.KILO_WATT ? stationInfo.power * 1000 : stationInfo.power; @@ -935,8 +1064,8 @@ export default class ChargingStation { }), ...(!Utils.isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }), ...(!Utils.isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }), - ...(!Utils.isUndefined(stationInfo.meterSerialNumber) && { - meterSerialNumber: stationInfo.meterSerialNumber, + ...(!Utils.isUndefined(stationInfo.meterSerialNumberPrefix) && { + meterSerialNumber: stationInfo.meterSerialNumberPrefix, }), ...(!Utils.isUndefined(stationInfo.meterType) && { meterType: stationInfo.meterType, @@ -954,7 +1083,6 @@ export default class ChargingStation { this.bootNotificationRequest = this.createBootNotificationRequest(this.stationInfo); this.ocppConfiguration = this.getOcppConfiguration(); delete this.stationInfo.Configuration; - this.saveStationInfo(); // Build connectors if needed const maxConnectors = this.getMaxNumberOfConnectors(); if (maxConnectors <= 0) { @@ -1039,6 +1167,9 @@ export default class ChargingStation { } } } + // The connectors attribute need to be initialized + this.stationInfo.maximumAmperage = this.getMaximumAmperage(); + this.saveStationInfo(); // Avoid duplication of connectors related information in RAM delete this.stationInfo.Connectors; // Initialize transaction attributes on connectors @@ -1098,10 +1229,19 @@ export default class ChargingStation { ) { this.deleteConfigurationKey(this.getSupervisionUrlOcppKey(), { save: false }); } + if ( + this.stationInfo.amperageLimitationOcppKey && + !this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey) + ) { + this.addConfigurationKey( + this.stationInfo.amperageLimitationOcppKey, + (this.stationInfo.maximumAmperage * this.getAmperageLimitationUnitDivider()).toString() + ); + } if (!this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)) { this.addConfigurationKey( StandardParametersKey.SupportedFeatureProfiles, - `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.Local_Auth_List_Management},${SupportedFeatureProfiles.Smart_Charging}` + `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}` ); } this.addConfigurationKey( @@ -1141,8 +1281,8 @@ export default class ChargingStation { } if ( !this.getConfigurationKey(StandardParametersKey.LocalAuthListEnabled) && - this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles).value.includes( - SupportedFeatureProfiles.Local_Auth_List_Management + this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)?.value.includes( + SupportedFeatureProfiles.LocalAuthListManagement ) ) { this.addConfigurationKey(StandardParametersKey.LocalAuthListEnabled, 'false'); @@ -1257,22 +1397,24 @@ export default class ChargingStation { // Send BootNotification let registrationRetryCount = 0; do { - this.bootNotificationResponse = - await this.ocppRequestService.sendMessageHandler( - RequestCommand.BOOT_NOTIFICATION, - { - chargePointModel: this.bootNotificationRequest.chargePointModel, - chargePointVendor: this.bootNotificationRequest.chargePointVendor, - chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, - firmwareVersion: this.bootNotificationRequest.firmwareVersion, - chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, - iccid: this.bootNotificationRequest.iccid, - imsi: this.bootNotificationRequest.imsi, - meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, - meterType: this.bootNotificationRequest.meterType, - }, - { skipBufferingOnError: true } - ); + this.bootNotificationResponse = await this.ocppRequestService.sendMessageHandler< + BootNotificationRequest, + BootNotificationResponse + >( + RequestCommand.BOOT_NOTIFICATION, + { + chargePointModel: this.bootNotificationRequest.chargePointModel, + chargePointVendor: this.bootNotificationRequest.chargePointVendor, + chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, + firmwareVersion: this.bootNotificationRequest.firmwareVersion, + chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, + iccid: this.bootNotificationRequest.iccid, + imsi: this.bootNotificationRequest.imsi, + meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, + meterType: this.bootNotificationRequest.meterType, + }, + { skipBufferingOnError: true } + ); if (!this.isInAcceptedState()) { this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++; await Utils.sleep( @@ -1557,9 +1699,55 @@ export default class ChargingStation { return maxConnectors; } + private getMaximumAmperage(): number | undefined { + const maximumPower = (this.stationInfo['maxPower'] as number) ?? this.stationInfo.maximumPower; + switch (this.getCurrentOutType()) { + case CurrentType.AC: + return ACElectricUtils.amperagePerPhaseFromPower( + this.getNumberOfPhases(), + maximumPower / this.getNumberOfConnectors(), + this.getVoltageOut() + ); + case CurrentType.DC: + return DCElectricUtils.amperage(maximumPower, this.getVoltageOut()); + } + } + + private getAmperageLimitationUnitDivider(): number { + let unitDivider = 1; + switch (this.stationInfo.amperageLimitationUnit) { + case AmpereUnits.DECI_AMPERE: + unitDivider = 10; + break; + case AmpereUnits.CENTI_AMPERE: + unitDivider = 100; + break; + case AmpereUnits.MILLI_AMPERE: + unitDivider = 1000; + break; + } + return unitDivider; + } + + private getAmperageLimitation(): number | undefined { + if ( + this.stationInfo.amperageLimitationOcppKey && + this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey) + ) { + return ( + Utils.convertToInt( + this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey).value + ) / this.getAmperageLimitationUnitDivider() + ); + } + } + private async startMessageSequence(): Promise { if (this.stationInfo.autoRegister) { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler< + BootNotificationRequest, + BootNotificationResponse + >( RequestCommand.BOOT_NOTIFICATION, { chargePointModel: this.bootNotificationRequest.chargePointModel, @@ -1589,14 +1777,14 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template at startup - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - } - ); + await this.ocppRequestService.sendMessageHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if ( @@ -1605,36 +1793,36 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template after reset - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - } - ); + await this.ocppRequestService.sendMessageHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) { // Send previous status at template reload - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: this.getConnectorStatus(connectorId).status, - errorCode: ChargePointErrorCode.NO_ERROR, - } - ); + await this.ocppRequestService.sendMessageHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: this.getConnectorStatus(connectorId).status, + errorCode: ChargePointErrorCode.NO_ERROR, + }); } else { // Send default status - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: ChargePointStatus.AVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - } - ); + await this.ocppRequestService.sendMessageHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: ChargePointStatus.AVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE; } } @@ -1681,24 +1869,24 @@ export default class ChargingStation { connectorId, this.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.ocppRequestService.sendMessageHandler( - RequestCommand.METER_VALUES, - { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - } - ); - } - await this.ocppRequestService.sendMessageHandler( - RequestCommand.STOP_TRANSACTION, - { + await this.ocppRequestService.sendMessageHandler< + MeterValuesRequest, + MeterValuesResponse + >(RequestCommand.METER_VALUES, { + connectorId, transactionId, - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.getTransactionIdTag(transactionId), - reason, - } - ); + meterValue: transactionEndMeterValue, + }); + } + await this.ocppRequestService.sendMessageHandler< + StopTransactionRequest, + StopTransactionResponse + >(RequestCommand.STOP_TRANSACTION, { + transactionId, + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.getTransactionIdTag(transactionId), + reason, + }); } } } @@ -1919,7 +2107,7 @@ export default class ChargingStation { this.wsConnectionRestarted = true; } else if (this.getAutoReconnectMaxRetries() !== -1) { logger.error( - `${this.logPrefix()} WebSocket reconnect failure: max retries reached (${ + `${this.logPrefix()} WebSocket reconnect failure: maximum retries reached (${ this.autoReconnectRetryCount }) or retry disabled (${this.getAutoReconnectMaxRetries()})` );