From 370ae4ee4ed2b3499ad1c1d286cfe3ffe451b5ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 13 Apr 2022 21:44:38 +0200 Subject: [PATCH] Factor out feature profile check at OCPP command handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 58 +++++++++---------- .../ocpp/1.6/OCPP16ServiceUtils.ts | 25 +++++++- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 3f8870ae..81855cec 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1492,7 +1492,7 @@ export default class ChargingStation { } else { throw new OCPPError( ErrorType.PROTOCOL_ERROR, - 'Incoming request is not iterable', + 'Incoming message is not iterable', Utils.isString(commandName) && commandName, { payload: request } ); diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 937edd41..1befa1c7 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -381,14 +381,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private handleRequestSetChargingProfile( commandPayload: SetChargingProfileRequest ): SetChargingProfileResponse { - if (!this.chargingStation.hasFeatureProfile(OCPP16SupportedFeatureProfiles.SmartCharging)) { - logger.error( - `${this.chargingStation.logPrefix()} Trying to set charging profile(s) without '${ - OCPP16SupportedFeatureProfiles.SmartCharging - }' feature enabled in ${ - OCPP16StandardParametersKey.SupportedFeatureProfiles - } in configuration` - ); + if ( + !OCPP16ServiceUtils.checkFeatureProfile( + this.chargingStation, + OCPP16SupportedFeatureProfiles.SmartCharging, + OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE + ) + ) { return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED; } if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) { @@ -430,14 +429,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private handleRequestClearChargingProfile( commandPayload: ClearChargingProfileRequest ): ClearChargingProfileResponse { - if (!this.chargingStation.hasFeatureProfile(OCPP16SupportedFeatureProfiles.SmartCharging)) { - logger.error( - `${this.chargingStation.logPrefix()} Trying to clear charging profile(s) without '${ - OCPP16SupportedFeatureProfiles.SmartCharging - }' feature enabled in ${ - OCPP16StandardParametersKey.SupportedFeatureProfiles - } in configuration` - ); + if ( + !OCPP16ServiceUtils.checkFeatureProfile( + this.chargingStation, + OCPP16SupportedFeatureProfiles.SmartCharging, + OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE + ) + ) { return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; } const connectorStatus = this.chargingStation.getConnectorStatus(commandPayload.connectorId); @@ -828,15 +826,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer commandPayload: GetDiagnosticsRequest ): Promise { if ( - !this.chargingStation.hasFeatureProfile(OCPP16SupportedFeatureProfiles.FirmwareManagement) + !OCPP16ServiceUtils.checkFeatureProfile( + this.chargingStation, + OCPP16SupportedFeatureProfiles.FirmwareManagement, + OCPP16IncomingRequestCommand.GET_DIAGNOSTICS + ) ) { - logger.error( - `${this.chargingStation.logPrefix()} Trying to get diagnostics without '${ - OCPP16SupportedFeatureProfiles.FirmwareManagement - }' feature enabled in ${ - OCPP16StandardParametersKey.SupportedFeatureProfiles - } in configuration` - ); return Constants.OCPP_RESPONSE_EMPTY; } logger.debug( @@ -946,14 +941,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private handleRequestTriggerMessage( commandPayload: OCPP16TriggerMessageRequest ): OCPP16TriggerMessageResponse { - if (!this.chargingStation.hasFeatureProfile(OCPP16SupportedFeatureProfiles.RemoteTrigger)) { - logger.error( - `${this.chargingStation.logPrefix()} Trying to remote trigger message without '${ - OCPP16SupportedFeatureProfiles.RemoteTrigger - }' feature enabled in ${ - OCPP16StandardParametersKey.SupportedFeatureProfiles - } in configuration` - ); + if ( + !OCPP16ServiceUtils.checkFeatureProfile( + this.chargingStation, + OCPP16SupportedFeatureProfiles.RemoteTrigger, + OCPP16IncomingRequestCommand.TRIGGER_MESSAGE + ) + ) { return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; } try { diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 43b291be..2ad75f1d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -14,12 +14,19 @@ import { OCPP16MeterValuePhase, OCPP16SampledValue, } from '../../../types/ocpp/1.6/MeterValues'; +import { + OCPP16IncomingRequestCommand, + OCPP16RequestCommand, +} from '../../../types/ocpp/1.6/Requests'; +import { + OCPP16StandardParametersKey, + OCPP16SupportedFeatureProfiles, +} from '../../../types/ocpp/1.6/Configuration'; import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; import { ErrorType } from '../../../types/ocpp/ErrorType'; import MeasurandValues from '../../../types/MeasurandValues'; -import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; import OCPPError from '../../../exception/OCPPError'; import Utils from '../../../utils/Utils'; import logger from '../../../utils/Logger'; @@ -44,6 +51,22 @@ export class OCPP16ServiceUtils { } } + public static checkFeatureProfile( + chargingStation: ChargingStation, + featureProfile: OCPP16SupportedFeatureProfiles, + command: OCPP16RequestCommand | OCPP16IncomingRequestCommand + ): boolean { + if (!chargingStation.hasFeatureProfile(featureProfile)) { + logger.warn( + `${chargingStation.logPrefix()} Trying to '${command}' without '${featureProfile}' feature enabled in ${ + OCPP16StandardParametersKey.SupportedFeatureProfiles + } in configuration` + ); + return false; + } + return true; + } + public static buildSampledValue( sampledValueTemplate: SampledValueTemplate, value: number, -- 2.34.1