From 73d87be1970bb3e8c32be6244fc510f6f501cb3a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Jul 2023 19:18:14 +0200 Subject: [PATCH] feat: add support for relative charging profile MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationUtils.ts | 7 ++- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 50 ++++--------------- .../ocpp/1.6/OCPP16ServiceUtils.ts | 44 ++++++++++++++++ 3 files changed, 59 insertions(+), 42 deletions(-) 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, -- 2.34.1