From 0e94de9012a897c17b5da02a4cae6a4f58bafb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Luiz=20Nery=20de=20Miranda?= Date: Fri, 1 Sep 2023 01:59:52 -0700 Subject: [PATCH 1/2] Emit call, call_result, and call_error events --- src/OcppClientConnection.ts | 14 +++++++++++++- src/impl/Protocol.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/OcppClientConnection.ts b/src/OcppClientConnection.ts index 3510cdf..bf0c616 100644 --- a/src/OcppClientConnection.ts +++ b/src/OcppClientConnection.ts @@ -312,6 +312,18 @@ export class OcppClientConnection extends Client { cb: (response: UrnOCPPCp220203StatusNotificationResponse) => void ) => void ): this; + on( + event: "call", + listener: (target: "cs" | "csms", message: string) => void + ): this; + on( + event: "call_result", + listener: (target: "cs" | "csms", message: string) => void + ): this; + on( + event: "call_error", + listener: (target: "cs" | "csms", message: string) => void + ): this; on( event: "close", listener: (code: number, reason: Buffer) => void @@ -501,4 +513,4 @@ export class OcppClientConnection extends Client { } } -export default OcppClientConnection; \ No newline at end of file +export default OcppClientConnection; diff --git a/src/impl/Protocol.ts b/src/impl/Protocol.ts index 029079c..ad6a7ba 100644 --- a/src/impl/Protocol.ts +++ b/src/impl/Protocol.ts @@ -39,6 +39,11 @@ export class Protocol { const [messageType, ...rest] = JSON.parse(message); if (messageType === CALL_MESSAGE && rest.length === 3) { + this.eventEmitter.emit( + "call", + "csms", + message, + ); const [messageId, action, payload] = rest; this.onCall( messageId, @@ -46,12 +51,22 @@ export class Protocol { payload ); } else if (messageType === CALLRESULT_MESSAGE && rest.length === 2) { + this.eventEmitter.emit( + "call_result", + "csms", + message + ); const [messageId, payload] = rest; this.onCallResult( messageId, payload ); } else if (messageType === CALLERROR_MESSAGE && rest.length === 3) { + this.eventEmitter.emit( + "call_error", + "csms", + message, + ); const [messageId, errorCode, errorDescription, errorDetails] = rest; this.onCallError( messageId, @@ -81,6 +96,13 @@ export class Protocol { messageId, request, payload]); + + this.eventEmitter.emit( + "call", + "cs", + result, + ); + this.socket.send(result); this.pendingCalls[messageId] = { resolve, @@ -117,6 +139,13 @@ export class Protocol { error.code, error.message, error.details || {}]); + + this.eventEmitter.emit( + "call_error", + "cs", + result + ); + this.socket.send(result); } catch (e) { console.error(e); @@ -238,6 +267,13 @@ export class Protocol { CALLRESULT_MESSAGE, messageId, responsePayload]); + + this.eventEmitter.emit( + "call_result", + "cs", + result + ); + this.socket.send(result); } catch (e) { if (e instanceof SyntaxError) { From 61ec5de36e29777fb9b4ea441a5950e597586d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Luiz=20Nery=20de=20Miranda?= Date: Sat, 9 Sep 2023 15:53:01 -0700 Subject: [PATCH 2/2] Improve error log by adding the exception as the detail of the OcppError --- src/impl/Protocol.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/impl/Protocol.ts b/src/impl/Protocol.ts index ad6a7ba..3e32be6 100644 --- a/src/impl/Protocol.ts +++ b/src/impl/Protocol.ts @@ -249,6 +249,7 @@ export class Protocol { new OcppError( ERROR_INTERNALERROR, "An internal error occurred and the receiver was not able to process the requested Action", + e, ), ); }