Fix type conversion to string.
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index ce036f7a1dc60b5841e08745548976ce31166098..dce900c758c224982e6fa2703c4ac8e1b53c06b3 100644 (file)
@@ -714,8 +714,8 @@ export default class ChargingStation {
   }
 
   async onMessage(messageEvent: MessageEvent): Promise<void> {
-    let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, '', ''];
-    let responseCallback: (payload?, requestPayload?) => void;
+    let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, {}, {}];
+    let responseCallback: (payload?: Record<string, unknown> | string, requestPayload?: Record<string, unknown>) => void;
     let rejectCallback: (error: OCPPError) => void;
     let requestPayload: Record<string, unknown>;
     let errMsg: string;
@@ -760,7 +760,7 @@ export default class ChargingStation {
             throw new Error(`Error request for message id ${messageId} is not iterable`);
           }
           delete this._requests[messageId];
-          rejectCallback(new OCPPError(commandName, commandPayload, errorDetails));
+          rejectCallback(new OCPPError(commandName, commandPayload.toString(), errorDetails));
           break;
         // Error
         default:
@@ -837,14 +837,12 @@ export default class ChargingStation {
     }
   }
 
-  async sendError(messageId: string, err: Error | OCPPError, commandName: RequestCommand | IncomingRequestCommand): Promise<unknown> {
-    // Check exception type: only OCPP error are accepted
-    const error = err instanceof OCPPError ? err : new OCPPError(ErrorType.INTERNAL_ERROR, err.message, err.stack && err.stack);
+  async sendError(messageId: string, error: OCPPError, commandName: RequestCommand | IncomingRequestCommand): Promise<unknown> {
     // Send error
     return this.sendMessage(messageId, error, MessageType.CALL_ERROR_MESSAGE, commandName);
   }
 
-  async sendMessage(messageId: string, commandParams, messageType: MessageType = MessageType.CALL_RESULT_MESSAGE, commandName: RequestCommand | IncomingRequestCommand): Promise<any> {
+  async sendMessage(messageId: string, commandParams: any, messageType: MessageType = MessageType.CALL_RESULT_MESSAGE, commandName: RequestCommand | IncomingRequestCommand): Promise<any> {
     // eslint-disable-next-line @typescript-eslint/no-this-alias
     const self = this;
     // Send a message through wsConnection
@@ -876,7 +874,7 @@ export default class ChargingStation {
         }
         // Yes: Send Message
         this._wsConnection.send(messageToSend);
-      } else {
+      } else if (commandName !== RequestCommand.BOOT_NOTIFICATION) {
         let dups = false;
         // Handle dups in buffer
         for (const message of this._messageQueue) {
@@ -903,7 +901,7 @@ export default class ChargingStation {
       }
 
       // Function that will receive the request's response
-      async function responseCallback(payload, requestPayload): Promise<void> {
+      async function responseCallback(payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
         if (self.getEnableStatistics()) {
           self._statistics.addMessage(commandName, messageType);
         }
@@ -927,7 +925,7 @@ export default class ChargingStation {
     });
   }
 
-  async handleResponse(commandName: RequestCommand, payload, requestPayload): Promise<void> {
+  async handleResponse(commandName: RequestCommand, payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
     const responseCallbackFn = 'handleResponse' + commandName;
     if (typeof this[responseCallbackFn] === 'function') {
       await this[responseCallbackFn](payload, requestPayload);
@@ -1038,7 +1036,7 @@ export default class ChargingStation {
     logger.debug(this._logPrefix() + ' Heartbeat response received: %j to Heartbeat request: %j', payload, requestPayload);
   }
 
-  async handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload): Promise<void> {
+  async handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> {
     let response;
     // Call
     if (typeof this['handleRequest' + commandName] === 'function') {