Separate OCPP command support check implementation per type
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 11 Aug 2022 20:54:24 +0000 (22:54 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 11 Aug 2022 20:54:24 +0000 (22:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index ad145f0b0f8b2cd3be8eb07ae087044484a9a7f1..2d6f84644e901cbc546ecf3f0eb3e59449277450 100644 (file)
@@ -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;
   }
 
index 7495ff0ceb69ddc30fffbe51fc5fe519d1159bb9..6624b9ca01d284cf0b9b1cae06c868f1f2649d66 100644 (file)
@@ -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
index 1c98bc37d4c6b25f2b897f07aea86fa0414014ab..aa0ca4f16bf5a7230364e9ceb2b025f8ebbf240b 100644 (file)
@@ -29,10 +29,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
     commandParams?: JsonType,
     params?: RequestParams
   ): Promise<Response> {
-    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
         );
index 3af8a1fc5b8d3dcdb6f3f5bbdfa01b7664848667..9093187d38a23c10f527932eca65702cc206acac 100644 (file)
@@ -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);