From ada189a839743451d477d5b3bddc698f2634d799 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 11 Aug 2022 22:54:24 +0200 Subject: [PATCH] Separate OCPP command support check implementation per type 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 | 42 +++++++++---------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 2 +- .../ocpp/1.6/OCPP16RequestService.ts | 9 ++-- .../ocpp/1.6/OCPP16ResponseService.ts | 2 +- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index ad145f0b..2d6f8464 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -27,7 +27,7 @@ import Configuration from '../utils/Configuration'; import Constants from '../utils/Constants'; import logger from '../utils/Logger'; import Utils from '../utils/Utils'; -import ChargingStation from './ChargingStation'; +import type ChargingStation from './ChargingStation'; import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; export class ChargingStationUtils { @@ -534,14 +534,25 @@ export class ChargingStationUtils { ); } - public static isCommandSupported( - command: RequestCommand | IncomingRequestCommand, + public static isRequestCommandSupported( + command: RequestCommand, chargingStation: ChargingStation ): boolean { - const isIncomingRequestCommand = Object.values(IncomingRequestCommand).includes( - command as IncomingRequestCommand - ); - const isRequestCommand = Object.values(RequestCommand).includes(command as RequestCommand); + const isRequestCommand = Object.values(RequestCommand).includes(command); + if (isRequestCommand && !chargingStation.stationInfo?.commandsSupport?.outgoingCommands) { + return true; + } else if (isRequestCommand && chargingStation.stationInfo?.commandsSupport?.outgoingCommands) { + return chargingStation.stationInfo?.commandsSupport?.outgoingCommands[command] ?? false; + } + logger.error(`${chargingStation.logPrefix()} Unknown outgoing OCPP command '${command}'`); + return false; + } + + public static isIncomingRequestCommandSupported( + command: IncomingRequestCommand, + chargingStation: ChargingStation + ): boolean { + const isIncomingRequestCommand = Object.values(IncomingRequestCommand).includes(command); if ( isIncomingRequestCommand && !chargingStation.stationInfo?.commandsSupport?.incomingCommands @@ -551,22 +562,9 @@ export class ChargingStationUtils { isIncomingRequestCommand && chargingStation.stationInfo?.commandsSupport?.incomingCommands ) { - return ( - (chargingStation.stationInfo?.commandsSupport?.incomingCommands[command] as boolean) ?? - false - ); - } else if ( - isRequestCommand && - !chargingStation.stationInfo?.commandsSupport?.outgoingCommands - ) { - return true; - } else if (isRequestCommand && chargingStation.stationInfo?.commandsSupport?.outgoingCommands) { - return ( - (chargingStation.stationInfo?.commandsSupport?.outgoingCommands[command] as boolean) ?? - false - ); + return chargingStation.stationInfo?.commandsSupport?.incomingCommands[command] ?? false; } - logger.error(`${chargingStation.logPrefix()} Unknown OCPP command '${command}'`); + logger.error(`${chargingStation.logPrefix()} Unknown incoming OCPP command '${command}'`); return false; } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 7495ff0c..6624b9ca 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -159,7 +159,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ) { if ( this.incomingRequestHandlers.has(commandName) && - ChargingStationUtils.isCommandSupported(commandName, chargingStation) + ChargingStationUtils.isIncomingRequestCommandSupported(commandName, chargingStation) ) { try { // Call the method to build the response diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 1c98bc37..aa0ca4f1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -29,10 +29,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandParams?: JsonType, params?: RequestParams ): Promise { - if ( - Object.values(OCPP16RequestCommand).includes(commandName) && - ChargingStationUtils.isCommandSupported(commandName, chargingStation) - ) { + if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { return (await this.sendMessage( chargingStation, Utils.generateUUID(), @@ -43,7 +40,7 @@ export default class OCPP16RequestService extends OCPPRequestService { } throw new OCPPError( ErrorType.NOT_SUPPORTED, - `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`, + `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`, commandName, commandParams ); @@ -148,7 +145,7 @@ export default class OCPP16RequestService extends OCPPRequestService { throw new OCPPError( ErrorType.NOT_SUPPORTED, // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`, + `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`, commandName, commandParams ); diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 3af8a1fc..9093187d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -68,7 +68,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { if (chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) { if ( this.responseHandlers.has(commandName) && - ChargingStationUtils.isCommandSupported(commandName, chargingStation) + ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation) ) { try { await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); -- 2.34.1