refactor(simulator): remove unneeded type casting
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 5b260cb6e7d8208a14ea7dea6ffbf72352b96313..ac5aaa93c451d8c877c9d93b0345550b74dd37e8 100644 (file)
@@ -573,7 +573,7 @@ export class ChargingStation {
         this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
         this.templateFileWatcher?.close();
         this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
-        this.bootNotificationResponse = undefined;
+        delete this.bootNotificationResponse;
         this.started = false;
         parentPort?.postMessage(MessageChannelUtils.buildStoppedMessage(this));
         this.stopping = false;
@@ -652,18 +652,6 @@ export class ChargingStation {
     if (params?.terminateOpened) {
       this.terminateWSConnection();
     }
-    const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
-    let protocol: string;
-    switch (ocppVersion) {
-      case OCPPVersion.VERSION_16:
-      case OCPPVersion.VERSION_20:
-      case OCPPVersion.VERSION_201:
-        protocol = `ocpp${ocppVersion}`;
-        break;
-      default:
-        this.handleUnsupportedVersion(ocppVersion);
-        break;
-    }
 
     if (this.isWebSocketConnectionOpened() === true) {
       logger.warn(
@@ -676,7 +664,11 @@ export class ChargingStation {
       `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
     );
 
-    this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options);
+    this.wsConnection = new WebSocket(
+      this.wsConnectionUrl,
+      `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
+      options
+    );
 
     // Handle WebSocket message
     this.wsConnection.on(
@@ -995,7 +987,7 @@ export class ChargingStation {
     this.stationInfo = this.getStationInfo();
     this.saveStationInfo();
     // Avoid duplication of connectors related information in RAM
-    this.stationInfo?.Connectors && delete this.stationInfo.Connectors;
+    delete this.stationInfo?.Connectors;
     this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
     if (this.getEnableStatistics() === true) {
       this.performanceStatistics = PerformanceStatistics.getInstance(
@@ -1011,27 +1003,7 @@ export class ChargingStation {
     // OCPP configuration
     this.ocppConfiguration = this.getOcppConfiguration();
     this.initializeOcppConfiguration();
-    const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
-    switch (ocppVersion) {
-      case OCPPVersion.VERSION_16:
-        this.ocppIncomingRequestService =
-          OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
-        this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
-          OCPP16ResponseService.getInstance<OCPP16ResponseService>()
-        );
-        break;
-      case OCPPVersion.VERSION_20:
-      case OCPPVersion.VERSION_201:
-        this.ocppIncomingRequestService =
-          OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>();
-        this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>(
-          OCPP20ResponseService.getInstance<OCPP20ResponseService>()
-        );
-        break;
-      default:
-        this.handleUnsupportedVersion(ocppVersion);
-        break;
-    }
+    this.initializeOcppServices();
     if (this.stationInfo?.autoRegister === true) {
       this.bootNotificationResponse = {
         currentTime: new Date(),
@@ -1059,6 +1031,30 @@ export class ChargingStation {
     }
   }
 
+  private initializeOcppServices(): void {
+    const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
+    switch (ocppVersion) {
+      case OCPPVersion.VERSION_16:
+        this.ocppIncomingRequestService =
+          OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
+        this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
+          OCPP16ResponseService.getInstance<OCPP16ResponseService>()
+        );
+        break;
+      case OCPPVersion.VERSION_20:
+      case OCPPVersion.VERSION_201:
+        this.ocppIncomingRequestService =
+          OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>();
+        this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>(
+          OCPP20ResponseService.getInstance<OCPP20ResponseService>()
+        );
+        break;
+      default:
+        this.handleUnsupportedVersion(ocppVersion);
+        break;
+    }
+  }
+
   private initializeOcppConfiguration(): void {
     if (
       !ChargingStationConfigurationUtils.getConfigurationKey(
@@ -1321,7 +1317,7 @@ export class ChargingStation {
           this.templateFile
         } with connector ${connectorId} status configuration defined, undefine it`
       );
-      connectorStatus.status = undefined;
+      delete connectorStatus.status;
     }
   }
 
@@ -1503,107 +1499,107 @@ export class ChargingStation {
     parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
   }
 
+  private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined {
+    const cachedRequest = this.requests.get(messageId);
+    if (Array.isArray(cachedRequest) === true) {
+      return cachedRequest;
+    }
+    throw new OCPPError(
+      ErrorType.PROTOCOL_ERROR,
+      `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
+        messageType
+      )} is not an array`,
+      undefined,
+      cachedRequest as JsonType
+    );
+  }
+
+  private async handleIncomingMessage(request: IncomingRequest): Promise<void> {
+    const [messageType, messageId, commandName, commandPayload] = request;
+    if (this.getEnableStatistics() === true) {
+      this.performanceStatistics?.addRequestStatistic(commandName, messageType);
+    }
+    logger.debug(
+      `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
+        request
+      )}`
+    );
+    // Process the message
+    await this.ocppIncomingRequestService.incomingRequestHandler(
+      this,
+      messageId,
+      commandName,
+      commandPayload
+    );
+  }
+
+  private handleResponseMessage(response: Response): void {
+    const [messageType, messageId, commandPayload] = response;
+    if (this.requests.has(messageId) === false) {
+      // Error
+      throw new OCPPError(
+        ErrorType.INTERNAL_ERROR,
+        `Response for unknown message id ${messageId}`,
+        undefined,
+        commandPayload
+      );
+    }
+    // Respond
+    const [responseCallback, , requestCommandName, requestPayload] = this.getCachedRequest(
+      messageType,
+      messageId
+    );
+    logger.debug(
+      `${this.logPrefix()} << Command '${
+        requestCommandName ?? Constants.UNKNOWN_COMMAND
+      }' received response payload: ${JSON.stringify(response)}`
+    );
+    responseCallback(commandPayload, requestPayload);
+  }
+
+  private handleErrorMessage(errorResponse: ErrorResponse): void {
+    const [messageType, messageId, errorType, errorMessage, errorDetails] = errorResponse;
+    if (this.requests.has(messageId) === false) {
+      // Error
+      throw new OCPPError(
+        ErrorType.INTERNAL_ERROR,
+        `Error response for unknown message id ${messageId}`,
+        undefined,
+        { errorType, errorMessage, errorDetails }
+      );
+    }
+    const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId);
+    logger.debug(
+      `${this.logPrefix()} << Command '${
+        requestCommandName ?? Constants.UNKNOWN_COMMAND
+      }' received error response payload: ${JSON.stringify(errorResponse)}`
+    );
+    errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
+  }
+
   private async onMessage(data: RawData): Promise<void> {
+    let request: IncomingRequest | Response | ErrorResponse;
     let messageType: number;
-    let messageId: string;
-    let commandName: IncomingRequestCommand;
-    let commandPayload: JsonType;
-    let errorType: ErrorType;
-    let errorMessage: string;
-    let errorDetails: JsonType;
-    let responseCallback: ResponseCallback;
-    let errorCallback: ErrorCallback;
-    let requestCommandName: RequestCommand | IncomingRequestCommand;
-    let requestPayload: JsonType;
-    let cachedRequest: CachedRequest;
     let errMsg: string;
     try {
-      const request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse;
+      request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse;
       if (Array.isArray(request) === true) {
-        [messageType, messageId] = request;
+        [messageType] = request;
         // Check the type of message
         switch (messageType) {
           // Incoming Message
           case MessageType.CALL_MESSAGE:
-            [, , commandName, commandPayload] = request as IncomingRequest;
-            if (this.getEnableStatistics() === true) {
-              this.performanceStatistics?.addRequestStatistic(commandName, messageType);
-            }
-            logger.debug(
-              `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
-                request
-              )}`
-            );
-            // Process the message
-            await this.ocppIncomingRequestService.incomingRequestHandler(
-              this,
-              messageId,
-              commandName,
-              commandPayload
-            );
+            await this.handleIncomingMessage(request as IncomingRequest);
             break;
-          // Outcome Message
+          // Response Message
           case MessageType.CALL_RESULT_MESSAGE:
-            [, , commandPayload] = request as Response;
-            if (this.requests.has(messageId) === false) {
-              // Error
-              throw new OCPPError(
-                ErrorType.INTERNAL_ERROR,
-                `Response for unknown message id ${messageId}`,
-                undefined,
-                commandPayload
-              );
-            }
-            // Respond
-            cachedRequest = this.requests.get(messageId);
-            if (Array.isArray(cachedRequest) === true) {
-              [responseCallback, errorCallback, requestCommandName, requestPayload] = cachedRequest;
-            } else {
-              throw new OCPPError(
-                ErrorType.PROTOCOL_ERROR,
-                `Cached request for message id ${messageId} response is not an array`,
-                undefined,
-                cachedRequest as unknown as JsonType
-              );
-            }
-            logger.debug(
-              `${this.logPrefix()} << Command '${
-                requestCommandName ?? Constants.UNKNOWN_COMMAND
-              }' received response payload: ${JSON.stringify(request)}`
-            );
-            responseCallback(commandPayload, requestPayload);
+            this.handleResponseMessage(request as Response);
             break;
           // Error Message
           case MessageType.CALL_ERROR_MESSAGE:
-            [, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
-            if (this.requests.has(messageId) === false) {
-              // Error
-              throw new OCPPError(
-                ErrorType.INTERNAL_ERROR,
-                `Error response for unknown message id ${messageId}`,
-                undefined,
-                { errorType, errorMessage, errorDetails }
-              );
-            }
-            cachedRequest = this.requests.get(messageId);
-            if (Array.isArray(cachedRequest) === true) {
-              [, errorCallback, requestCommandName] = cachedRequest;
-            } else {
-              throw new OCPPError(
-                ErrorType.PROTOCOL_ERROR,
-                `Cached request for message id ${messageId} error response is not an array`,
-                undefined,
-                cachedRequest as unknown as JsonType
-              );
-            }
-            logger.debug(
-              `${this.logPrefix()} << Command '${
-                requestCommandName ?? Constants.UNKNOWN_COMMAND
-              }' received error response payload: ${JSON.stringify(request)}`
-            );
-            errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
+            this.handleErrorMessage(request as ErrorResponse);
             break;
-          // Error
+          // Unknown Message
           default:
             // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
             errMsg = `Wrong message type ${messageType}`;
@@ -1617,38 +1613,20 @@ export class ChargingStation {
         });
       }
     } catch (error) {
-      // Log
-      logger.error(
-        `${this.logPrefix()} Incoming OCPP command '${
-          commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
-        }' message '${data.toString()}'${
-          messageType !== MessageType.CALL_MESSAGE
-            ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
-            : ''
-        } processing error:`,
-        error
-      );
-      if (error instanceof OCPPError === false) {
-        logger.warn(
-          `${this.logPrefix()} Error thrown at incoming OCPP command '${
-            commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
-          }' message '${data.toString()}' handling is not an OCPPError:`,
-          error
-        );
-      }
+      let commandName: IncomingRequestCommand;
+      let requestCommandName: RequestCommand | IncomingRequestCommand;
+      let errorCallback: ErrorCallback;
+      const [, messageId] = request;
       switch (messageType) {
         case MessageType.CALL_MESSAGE:
+          [, , commandName] = request as IncomingRequest;
           // Send error
-          await this.ocppRequestService.sendError(
-            this,
-            messageId,
-            error as OCPPError,
-            commandName ?? requestCommandName ?? null
-          );
+          await this.ocppRequestService.sendError(this, messageId, error as OCPPError, commandName);
           break;
         case MessageType.CALL_RESULT_MESSAGE:
         case MessageType.CALL_ERROR_MESSAGE:
-          if (errorCallback) {
+          if (this.requests.has(messageId) === true) {
+            [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId);
             // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
             errorCallback(error as OCPPError, false);
           } else {
@@ -1657,6 +1635,24 @@ export class ChargingStation {
           }
           break;
       }
+      if (error instanceof OCPPError === false) {
+        logger.warn(
+          `${this.logPrefix()} Error thrown at incoming OCPP command '${
+            commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
+          }' message '${data.toString()}' handling is not an OCPPError:`,
+          error
+        );
+      }
+      logger.error(
+        `${this.logPrefix()} Incoming OCPP command '${
+          commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
+        }' message '${data.toString()}'${
+          messageType !== MessageType.CALL_MESSAGE
+            ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
+            : ''
+        } processing error:`,
+        error
+      );
     }
   }
 
@@ -1882,7 +1878,7 @@ export class ChargingStation {
             ConnectorStatusEnum.Unavailable
           )
         );
-        this.getConnectorStatus(connectorId).status = undefined;
+        delete this.getConnectorStatus(connectorId)?.status;
       }
     }
   }
@@ -1937,24 +1933,22 @@ export class ChargingStation {
     const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls();
     if (Utils.isNotEmptyArray(supervisionUrls)) {
       switch (Configuration.getSupervisionUrlDistribution()) {
-        case SupervisionUrlDistribution.ROUND_ROBIN:
-          // FIXME
-          this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
-          break;
         case SupervisionUrlDistribution.RANDOM:
           this.configuredSupervisionUrlIndex = Math.floor(
             Utils.secureRandom() * supervisionUrls.length
           );
           break;
+        case SupervisionUrlDistribution.ROUND_ROBIN:
         case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY:
-          this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
-          break;
         default:
-          logger.error(
-            `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
-              SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
-            }`
-          );
+          Object.values(SupervisionUrlDistribution).includes(
+            Configuration.getSupervisionUrlDistribution()
+          ) === false &&
+            logger.error(
+              `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
+                SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
+              }`
+            );
           this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
           break;
       }