From ce0f54aa319618e2abb22f61bdc9e08e69c6410f Mon Sep 17 00:00:00 2001 From: Lauri Arte Date: Mon, 25 Mar 2024 12:26:42 +0200 Subject: [PATCH] feat: Adds generic message type for OCPPCall.toCallResponse --- README.md | 7 ++- src/message/ocpp-call-error.ts | 2 +- src/message/ocpp-call-result.ts | 27 +++++++---- src/message/ocpp-call.ts | 6 ++- test-report.xml | 86 --------------------------------- tests/ocpp-messages.test.ts | 14 ++++-- 6 files changed, 39 insertions(+), 103 deletions(-) delete mode 100644 test-report.xml diff --git a/README.md b/README.md index 60cc23e..8349ba7 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ **OCPP tools** is a collection of Open Charge Point Protocol message schemas, validation functions, utility types and typed interfaces for Typescript. Most of the code is generated using the OCPP payload JSON schema files. +**Note**: Things are changing, and backwards compatibility might be broken until a v1 release. +

(back to top)

## Getting Started @@ -109,11 +111,14 @@ const authorizeResponse = parseOCPPMessage( ### Construct a response to a RPC call ```typescript +import { parseOCPPMessage, AuthorizeResponseV16, OCPPCall} from "@cshil/ocpp-tools"; + const request = parseOCPPMessage( "[2, \"abc123\", \"Authorize\", {\"idTag\": \"abc-123-abc\""}]", ) as OCPPCall -console.info(request.toCallResponse({status: "Accepted"}).toRPCObject) +const callResult = request.toCallResponse({idTagInfo: { status: "Accepted"}}) +console.info(callResult).toRPCObject) // => [3, "abc123", { "status": "Accepted"}] ``` diff --git a/src/message/ocpp-call-error.ts b/src/message/ocpp-call-error.ts index 273f0ee..d358c93 100644 --- a/src/message/ocpp-call-error.ts +++ b/src/message/ocpp-call-error.ts @@ -11,7 +11,7 @@ interface iOCPPCallError { } export class OCPPCallError { - public messageTypeId: OCPPMessageType; + public messageTypeId: OCPPMessageType.CALL_ERROR; public messageId: string; public errorCode: OCPPErrorCodeV16; diff --git a/src/message/ocpp-call-result.ts b/src/message/ocpp-call-result.ts index b37d5ad..d0ac05a 100644 --- a/src/message/ocpp-call-result.ts +++ b/src/message/ocpp-call-result.ts @@ -1,23 +1,30 @@ import { OCPPMessageType } from "./types"; -import { OCPPResponseTypeV16 } from "./../generated/v16" +import { OCPPResponseTypeV16 } from "./../generated/v16"; import { OCPPCallResultV16 } from "src/generated/v16/types/ocpp-call-result"; - - export class OCPPCallResult { - public messageTypeId: OCPPMessageType + public messageTypeId: OCPPMessageType.CALL_RESULT; public messageId: string; - public payload: OCPPResponseTypeV16; public toRPCObject(): OCPPCallResultV16 { - return [OCPPMessageType.CALL_RESULT, this.messageId, this.payload as Record] + return [ + OCPPMessageType.CALL_RESULT, + this.messageId, + this.payload as Record, + ]; } - public constructor({messageId, payload}: { messageId: string, payload: OCPPResponseTypeV16}) { - this.messageTypeId = OCPPMessageType.CALL_RESULT - this.messageId = messageId - this.payload = payload + public constructor({ + messageId, + payload, + }: { + messageId: string; + payload: OCPPResponseTypeV16; + }) { + this.messageTypeId = OCPPMessageType.CALL_RESULT; + this.messageId = messageId; + this.payload = payload; } } diff --git a/src/message/ocpp-call.ts b/src/message/ocpp-call.ts index 74ef8a8..ec472f2 100644 --- a/src/message/ocpp-call.ts +++ b/src/message/ocpp-call.ts @@ -9,14 +9,16 @@ import { OCPPCallResult } from "./ocpp-call-result"; import { OCPPCallV16 } from "src/generated/v16/types/ocpp-call"; export class OCPPCall { - public messageTypeId: OCPPMessageType; + public messageTypeId: OCPPMessageType.CALL; public messageId: string; public action: ActionV16; public payload: OCPPRequestTypeV16; - public toCallResponse(payload: OCPPResponseTypeV16): OCPPCallResult { + public toCallResponse( + payload: T + ): OCPPCallResult { return new OCPPCallResult({ messageId: this.messageId, payload, diff --git a/test-report.xml b/test-report.xml deleted file mode 100644 index de266e5..0000000 --- a/test-report.xml +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/tests/ocpp-messages.test.ts b/tests/ocpp-messages.test.ts index 48a6fc8..8b02c1a 100644 --- a/tests/ocpp-messages.test.ts +++ b/tests/ocpp-messages.test.ts @@ -4,7 +4,7 @@ import { OCPPCall } from "src/message/ocpp-call"; import { OCPPMessageType, ocppVersion } from "src/message/types"; import { OCPPCallResult } from "src/message/ocpp-call-result"; import { OCPPCallError } from "src/message/ocpp-call-error"; -import { OCPPErrorCodeV16 } from "src/generated/v16"; +import { AuthorizeResponseV16, OCPPErrorCodeV16 } from "src/generated/v16"; type TestCase = { input: string; expected: Partial; @@ -98,9 +98,17 @@ describe("OCPP CALL", () => { const input = exampleRequests[0].input; const m = parseOCPPMessage(input) as OCPPCall; - const result = m.toCallResponse({ status: "Accepted" }).toRPCObject(); + const result = m + .toCallResponse({ + idTagInfo: { status: "Accepted" }, + }) + .toRPCObject(); - expect(result).toEqual([3, JSON.parse(input)[1], { status: "Accepted" }]); + expect(result).toEqual([ + 3, + JSON.parse(input)[1], + { idTagInfo: { status: "Accepted" } }, + ]); }); test.each(exampleRequests)(