Reorganize more sensibly types definition
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 1136c93b568721fbc2cac483f29232e36cb347ad..fa03c535e64dcb87ce08e5ea1c8ee308d2060b7b 100644 (file)
@@ -15,7 +15,7 @@ import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStat
 import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
 import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
-import OCPPError from '../OCPPError';
+import OCPPError from '../../../exception/OCPPError';
 import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
 import { URL } from 'url';
 import Utils from '../../../utils/Utils';
@@ -47,18 +47,22 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
 
   public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> {
     let result: Record<string, unknown>;
-    if (this.incomingRequestHandlers.has(commandName)) {
-      try {
-        // Call the method to build the result
-        result = await this.incomingRequestHandlers.get(commandName)(commandPayload);
-      } catch (error) {
-        // Log
-        logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
-        throw error;
+    if (this.chargingStation.isRegistered()) {
+      if (this.incomingRequestHandlers.has(commandName)) {
+        try {
+          // Call the method to build the result
+          result = await this.incomingRequestHandlers.get(commandName)(commandPayload);
+        } catch (error) {
+          // Log
+          logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
+          throw error;
+        }
+      } else {
+        // Throw exception
+        throw new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle request payload ${JSON.stringify(commandPayload, null, 2)}`, commandName);
       }
     } else {
-      // Throw exception
-      throw new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle request payload ${JSON.stringify(commandPayload, null, 2)}`, commandName);
+      throw new OCPPError(ErrorType.SECURITY_ERROR, `The charging station is not registered on the central server. ${commandName} cannot be not issued to handle request payload ${JSON.stringify(commandPayload, null, 2)}`, commandName);
     }
     // Send the built result
     await this.chargingStation.ocppRequestService.sendResult(messageId, result, commandName);