Type OCPP requests
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Jan 2021 21:35:19 +0000 (22:35 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Jan 2021 21:35:19 +0000 (22:35 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/OcppError.ts
src/types/ocpp/Requests.ts

index ce036f7a1dc60b5841e08745548976ce31166098..cdf87cddc903cb173abd3492dfacf1bfdf8057db 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;
@@ -746,7 +746,7 @@ export default class ChargingStation {
             throw new Error(`Response request for unknown message id ${messageId}`);
           }
           delete this._requests[messageId];
-          responseCallback(commandName, requestPayload);
+          responseCallback(commandName.toString(), requestPayload);
           break;
         // Error Message
         case MessageType.CALL_ERROR_MESSAGE:
@@ -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
@@ -927,7 +925,7 @@ export default class ChargingStation {
     });
   }
 
-  async handleResponse(commandName: RequestCommand, payload, requestPayload): Promise<void> {
+  async handleResponse(commandName: RequestCommand, payload: Record<string, unknown>, 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> | string): Promise<void> {
     let response;
     // Call
     if (typeof this['handleRequest' + commandName] === 'function') {
index d39cdef1c8f5baa039c7941d51cfb724ce8569df..ae86c0a160138657b193aa413258349874570ca5 100644 (file)
@@ -2,7 +2,7 @@ import { ErrorType } from '../types/ocpp/ErrorType';
 
 export default class OCPPError extends Error {
   code: string;
-  details: any;
+  details?: any;
 
   constructor(code: string, message: string, details?: any) {
     super(message);
index 486436196285289a336e6b6929d3a5eb0279d987..7ce4979e84352c97c9d29c5b7120399f5486f503 100644 (file)
@@ -6,6 +6,6 @@ export default interface Requests {
   [id: string]: Request;
 }
 
-export type Request = [(payload?, requestPayload?) => void, (error?: OCPPError) => void, Record<string, unknown>];
+export type Request = [(payload?: Record<string, unknown>, requestPayload?: Record<string, unknown>) => void, (error?: OCPPError) => void, Record<string, unknown>];
 
-export type IncomingRequest = [MessageType, string, IncomingRequestCommand, string, string];
+export type IncomingRequest = [MessageType, string, IncomingRequestCommand, Record<string, unknown> | string, Record<string, unknown>];