From: Jérôme Benoit Date: Mon, 24 Jul 2023 17:18:14 +0000 (+0200) Subject: feat: add support for relative charging profile X-Git-Tag: v1.2.20~158 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=73d87be1970bb3e8c32be6244fc510f6f501cb3a;p=e-mobility-charging-stations-simulator.git feat: add support for relative charging profile Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index aa590b78..6a64df3d 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -746,10 +746,13 @@ const getLimitFromChargingProfiles = ( } break; } + } else if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE) { + chargingSchedule.startSchedule = + chargingStation.getConnectorStatus(connectorId)?.transactionStart; } // Check if the charging profile is active if ( - isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate) + isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) ) { let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search the right schedule period @@ -769,7 +772,7 @@ const getLimitFromChargingProfiles = ( // Find the right schedule period if ( isAfter( - addSeconds(chargingSchedule.startSchedule, schedulePeriod.startPeriod), + addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), currentDate, ) ) { diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 163b006f..82647835 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -730,53 +730,23 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { } if (isNullOrUndefined(commandPayload.connectorId)) { let clearedCP = false; - const clearChargingProfiles = (connectorStatus: ConnectorStatus) => { - if (isNotEmptyArray(connectorStatus?.chargingProfiles)) { - connectorStatus?.chargingProfiles?.forEach( - (chargingProfile: OCPP16ChargingProfile, index: number) => { - let clearCurrentCP = false; - if (chargingProfile.chargingProfileId === commandPayload.id) { - clearCurrentCP = true; - } - if ( - !commandPayload.chargingProfilePurpose && - chargingProfile.stackLevel === commandPayload.stackLevel - ) { - clearCurrentCP = true; - } - if ( - !chargingProfile.stackLevel && - chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose - ) { - clearCurrentCP = true; - } - if ( - chargingProfile.stackLevel === commandPayload.stackLevel && - chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose - ) { - clearCurrentCP = true; - } - if (clearCurrentCP) { - connectorStatus?.chargingProfiles?.splice(index, 1); - logger.debug( - `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`, - chargingProfile, - ); - clearedCP = true; - } - }, - ); - } - }; if (chargingStation.hasEvses) { for (const evseStatus of chargingStation.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - clearChargingProfiles(connectorStatus); + clearedCP = OCPP16ServiceUtils.clearChargingProfiles( + chargingStation, + commandPayload, + connectorStatus.chargingProfiles, + ); } } } else { for (const connectorId of chargingStation.connectors.keys()) { - clearChargingProfiles(chargingStation.getConnectorStatus(connectorId)!); + clearedCP = OCPP16ServiceUtils.clearChargingProfiles( + chargingStation, + commandPayload, + chargingStation.getConnectorStatus(connectorId)?.chargingProfiles, + ); } } if (clearedCP) { diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index c3e708df..b2f1c400 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -5,6 +5,7 @@ import type { JSONSchemaType } from 'ajv'; import { type ChargingStation, getIdTagsFile } from '../../../charging-station'; import { OCPPError } from '../../../exception'; import { + type ClearChargingProfileRequest, type ConnectorStatus, CurrentType, ErrorType, @@ -832,6 +833,49 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { !cpReplaced && chargingStation.getConnectorStatus(connectorId)?.chargingProfiles?.push(cp); } + public static clearChargingProfiles = ( + chargingStation: ChargingStation, + commandPayload: ClearChargingProfileRequest, + chargingProfiles: OCPP16ChargingProfile[] | undefined, + ): boolean => { + let clearedCP = false; + if (isNotEmptyArray(chargingProfiles)) { + chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { + let clearCurrentCP = false; + if (chargingProfile.chargingProfileId === commandPayload.id) { + clearCurrentCP = true; + } + if ( + !commandPayload.chargingProfilePurpose && + chargingProfile.stackLevel === commandPayload.stackLevel + ) { + clearCurrentCP = true; + } + if ( + !chargingProfile.stackLevel && + chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose + ) { + clearCurrentCP = true; + } + if ( + chargingProfile.stackLevel === commandPayload.stackLevel && + chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose + ) { + clearCurrentCP = true; + } + if (clearCurrentCP) { + chargingProfiles.splice(index, 1); + logger.debug( + `${chargingStation.logPrefix()} Matching charging profile(s) cleared: %j`, + chargingProfile, + ); + clearedCP = true; + } + }); + } + return clearedCP; + }; + public static parseJsonSchemaFile( relativePath: string, moduleName?: string,