UI Server: dedupe some code in helpers
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
index 29ea6f393288efd0afaf539b2b8d4351340da56a..a0f09bb8336d76ce442b88e26056829a0e63c12e 100644 (file)
@@ -140,6 +140,8 @@ export default class OCPP16RequestService extends OCPPRequestService {
         ) as JSONSchemaType<OCPP16StopTransactionRequest>,
       ],
     ]);
+    this.buildRequestPayload.bind(this);
+    this.validatePayload.bind(this);
   }
 
   public async requestHandler<Request extends JsonType, Response extends JsonType>(
@@ -154,18 +156,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
         commandName,
         commandParams
       );
-      if (this.jsonSchemas.has(commandName)) {
-        this.validateRequestPayload(
-          chargingStation,
-          commandName,
-          this.jsonSchemas.get(commandName),
-          requestPayload
-        );
-      } else {
-        logger.warn(
-          `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation`
-        );
-      }
+      this.validatePayload(chargingStation, commandName, requestPayload);
       return (await this.sendMessage(
         chargingStation,
         Utils.generateUUID(),
@@ -176,7 +167,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
     }
     throw new OCPPError(
       ErrorType.NOT_SUPPORTED,
-      `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`,
+      `Unsupported OCPP command '${commandName}'`,
       commandName,
       commandParams
     );
@@ -225,15 +216,6 @@ export default class OCPP16RequestService extends OCPPRequestService {
       case OCPP16RequestCommand.HEARTBEAT:
         return {} as unknown as Request;
       case OCPP16RequestCommand.METER_VALUES:
-        // Sanity check
-        if (!Array.isArray(commandParams?.meterValue)) {
-          throw new OCPPError(
-            ErrorType.TYPE_CONSTRAINT_VIOLATION,
-            `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue PDU field`,
-            commandName,
-            commandParams
-          );
-        }
         return {
           connectorId: commandParams?.connectorId,
           transactionId: commandParams?.transactionId,
@@ -281,10 +263,29 @@ 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}'`,
+          `Unsupported OCPP command '${commandName}'`,
           commandName,
           commandParams
         );
     }
   }
+
+  private validatePayload<Request extends JsonType>(
+    chargingStation: ChargingStation,
+    commandName: OCPP16RequestCommand,
+    requestPayload: Request
+  ): boolean {
+    if (this.jsonSchemas.has(commandName)) {
+      return this.validateRequestPayload(
+        chargingStation,
+        commandName,
+        this.jsonSchemas.get(commandName),
+        requestPayload
+      );
+    }
+    logger.warn(
+      `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
+    );
+    return false;
+  }
 }