Cleanup OCPP message sending and error code
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 14 Sep 2021 21:39:26 +0000 (23:39 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 14 Sep 2021 21:39:26 +0000 (23:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/OCPPError.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/exception/BaseError.ts [new file with mode: 0644]

index cd449b10f8cbfc93838bb9b617c053a6a9108b4b..cdb75027986bf1413b0571bc5d908f4bed0217b5 100644 (file)
@@ -2,9 +2,10 @@
 
 import { IncomingRequestCommand, RequestCommand } from '../../types/ocpp/Requests';
 
+import BaseError from '../../exception/BaseError';
 import { ErrorType } from '../../types/ocpp/ErrorType';
 
-export default class OCPPError extends Error {
+export default class OCPPError extends BaseError {
   code: ErrorType | IncomingRequestCommand;
   command?: RequestCommand | IncomingRequestCommand;
   details?: Record<string, unknown>;
@@ -12,14 +13,8 @@ export default class OCPPError extends Error {
   constructor(code: ErrorType | IncomingRequestCommand, message: string, command?: RequestCommand | IncomingRequestCommand, details?: Record<string, unknown>) {
     super(message);
 
-    this.name = new.target.name;
     this.code = code ?? ErrorType.GENERIC_ERROR;
-    this.message = message ?? '';
     this.command = command;
     this.details = details ?? {};
-
-    Object.setPrototypeOf(this, new.target.prototype);
-
-    Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : (this.stack = (new Error()).stack);
   }
 }
index 172a61a2b3a225b8887ae516d86bdd33911e909f..c35f78de254e8080743aa81f11253877cfd6ca1c 100644 (file)
@@ -28,27 +28,8 @@ export default abstract class OCPPRequestService {
     // eslint-disable-next-line @typescript-eslint/no-this-alias
     const self = this;
     // Send a message through wsConnection
-    return new Promise((resolve: (value?: any | PromiseLike<any>) => void, reject: (reason?: any) => void) => {
-      let messageToSend: string;
-      // Type of message
-      switch (messageType) {
-        // Request
-        case MessageType.CALL_MESSAGE:
-          // Build request
-          this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record<string, unknown>]);
-          messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
-          break;
-        // Response
-        case MessageType.CALL_RESULT_MESSAGE:
-          // Build response
-          messageToSend = JSON.stringify([messageType, messageId, commandParams]);
-          break;
-        // Error Message
-        case MessageType.CALL_ERROR_MESSAGE:
-          // Build Error Message
-          messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]);
-          break;
-      }
+    return new Promise((resolve, reject) => {
+      const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback);
       if (this.chargingStation.getEnableStatistics()) {
         this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
       }
@@ -67,7 +48,7 @@ export default abstract class OCPPRequestService {
       // Response?
       if (messageType === MessageType.CALL_RESULT_MESSAGE) {
         // Yes: send Ok
-        resolve();
+        resolve(commandName);
       } else if (messageType === MessageType.CALL_ERROR_MESSAGE) {
         // Send timeout
         setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_ERROR_TIMEOUT);
@@ -112,6 +93,31 @@ export default abstract class OCPPRequestService {
     throw error;
   }
 
+  private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
+      responseCallback: (payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>) => Promise<void>, rejectCallback: (error: OCPPError) => void): string {
+    let messageToSend: string;
+    // Type of message
+    switch (messageType) {
+      // Request
+      case MessageType.CALL_MESSAGE:
+        // Build request
+        this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record<string, unknown>]);
+        messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
+        break;
+      // Response
+      case MessageType.CALL_RESULT_MESSAGE:
+        // Build response
+        messageToSend = JSON.stringify([messageType, messageId, commandParams]);
+        break;
+      // Error Message
+      case MessageType.CALL_ERROR_MESSAGE:
+        // Build Error Message
+        messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]);
+        break;
+    }
+    return messageToSend;
+  }
+
   public abstract sendHeartbeat(): Promise<void>;
   public abstract sendBootNotification(chargePointModel: string, chargePointVendor: string, chargeBoxSerialNumber?: string, firmwareVersion?: string, chargePointSerialNumber?: string, iccid?: string, imsi?: string, meterSerialNumber?: string, meterType?: string): Promise<BootNotificationResponse>;
   public abstract sendStatusNotification(connectorId: number, status: ChargePointStatus, errorCode?: ChargePointErrorCode): Promise<void>;
diff --git a/src/exception/BaseError.ts b/src/exception/BaseError.ts
new file mode 100644 (file)
index 0000000..e1a82c0
--- /dev/null
@@ -0,0 +1,8 @@
+export default class BaseError extends Error {
+  public constructor(message?: string) {
+    super(message);
+    this.name = new.target.name;
+    Object.setPrototypeOf(this, new.target.prototype);
+    Error.captureStackTrace ? (Error.captureStackTrace(this, this.constructor)) : (this.stack = (new Error()).stack);
+  }
+}