From: Jérôme Benoit Date: Tue, 1 Aug 2023 17:37:56 +0000 (+0200) Subject: refactor: factor out charging profiles preparation X-Git-Tag: v1.2.20~47 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0eb666db012bc0c088e0b8c144381dfafbbeccf4;p=e-mobility-charging-stations-simulator.git refactor: factor out charging profiles preparation Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 05773f01..1632f28b 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -733,7 +733,7 @@ const getLimitFromChargingProfiles = ( ): ChargingProfilesLimit | undefined => { const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`; const currentDate = new Date(); - const connectorStatus = chargingStation.getConnectorStatus(connectorId); + const connectorStatus = chargingStation.getConnectorStatus(connectorId)!; if (!isArraySorted(chargingProfiles, (a, b) => b.stackLevel - a.stackLevel)) { logger.warn( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profiles are not sorted by stack level. Trying to sort them`, @@ -755,17 +755,8 @@ const getLimitFromChargingProfiles = ( ); chargingSchedule.startSchedule = convertToDate(chargingSchedule?.startSchedule)!; } - switch (chargingProfile.chargingProfileKind) { - case ChargingProfileKindType.RECURRING: - if (!canProceedRecurringChargingProfile(chargingProfile, logPrefix)) { - continue; - } - prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); - break; - case ChargingProfileKindType.RELATIVE: - connectorStatus?.transactionStarted && - (chargingSchedule.startSchedule = connectorStatus?.transactionStart); - break; + if (!prepareChargingProfileKind(connectorStatus, chargingProfile, currentDate, logPrefix)) { + continue; } if (!canProceedChargingProfile(chargingProfile, currentDate, logPrefix)) { continue; @@ -859,6 +850,27 @@ const getLimitFromChargingProfiles = ( } }; +export const prepareChargingProfileKind = ( + connectorStatus: ConnectorStatus, + chargingProfile: ChargingProfile, + currentDate: Date, + logPrefix: string, +): boolean => { + switch (chargingProfile.chargingProfileKind) { + case ChargingProfileKindType.RECURRING: + if (!canProceedRecurringChargingProfile(chargingProfile, logPrefix)) { + return false; + } + prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); + break; + case ChargingProfileKindType.RELATIVE: + connectorStatus?.transactionStarted && + (chargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart); + break; + } + return true; +}; + export const canProceedChargingProfile = ( chargingProfile: ChargingProfile, currentDate: Date, @@ -891,7 +903,7 @@ export const canProceedChargingProfile = ( return true; }; -export const canProceedRecurringChargingProfile = ( +const canProceedRecurringChargingProfile = ( chargingProfile: ChargingProfile, logPrefix: string, ): boolean => { @@ -914,7 +926,7 @@ export const canProceedRecurringChargingProfile = ( * @param currentDate - * @param logPrefix - */ -export const prepareRecurringChargingProfile = ( +const prepareRecurringChargingProfile = ( chargingProfile: ChargingProfile, currentDate: Date, logPrefix: string, diff --git a/src/charging-station/index.ts b/src/charging-station/index.ts index 07dbe9b6..3eccd258 100644 --- a/src/charging-station/index.ts +++ b/src/charging-station/index.ts @@ -7,12 +7,11 @@ export { } from './ConfigurationKeyUtils'; export { canProceedChargingProfile, - canProceedRecurringChargingProfile, checkChargingStation, getIdTagsFile, hasFeatureProfile, hasReservationExpired, - prepareRecurringChargingProfile, + prepareChargingProfileKind, removeExpiredReservations, resetConnectorStatus, } from './Helpers'; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index e3375d19..fbb76bec 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -21,10 +21,9 @@ import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import { type ChargingStation, canProceedChargingProfile, - canProceedRecurringChargingProfile, checkChargingStation, getConfigurationKey, - prepareRecurringChargingProfile, + prepareChargingProfileKind, removeExpiredReservations, setConfigurationKeyValue, } from '../../../charging-station'; @@ -32,7 +31,6 @@ import { OCPPError } from '../../../exception'; import { type ChangeConfigurationRequest, type ChangeConfigurationResponse, - ChargingProfileKindType, ChargingRateUnitType, type ClearChargingProfileRequest, type ClearChargingProfileResponse, @@ -687,7 +685,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ); return OCPP16Constants.OCPP_RESPONSE_REJECTED; } - const connectorStatus = chargingStation.getConnectorStatus(connectorId); + const connectorStatus = chargingStation.getConnectorStatus(connectorId)!; if ( isEmptyArray( connectorStatus?.chargingProfiles && @@ -729,21 +727,15 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { chargingProfile.chargingSchedule?.startSchedule, )!; } - switch (chargingProfile.chargingProfileKind) { - case ChargingProfileKindType.RECURRING: - if (!canProceedRecurringChargingProfile(chargingProfile, chargingStation.logPrefix())) { - continue; - } - prepareRecurringChargingProfile( - chargingProfile, - interval.start as Date, - chargingStation.logPrefix(), - ); - break; - case ChargingProfileKindType.RELATIVE: - connectorStatus?.transactionStarted && - (chargingProfile.chargingSchedule.startSchedule = connectorStatus?.transactionStart); - break; + if ( + !prepareChargingProfileKind( + connectorStatus, + chargingProfile, + interval.start as Date, + chargingStation.logPrefix(), + ) + ) { + continue; } if ( !canProceedChargingProfile(