Skip to content

Commit

Permalink
Merge pull request #15 from connected-hil/feat/make-more-typings
Browse files Browse the repository at this point in the history
feat: Adds generic message type for OCPPCall.toCallResponse
  • Loading branch information
larte authored Mar 25, 2024
2 parents 2f1f891 + ce0f54a commit b9831af
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 103 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<p align="right">(<a href="#readme-top">back to top</a>)</p>

## Getting Started
Expand Down Expand Up @@ -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<AuthorizeResponseV16>({idTagInfo: { status: "Accepted"}})
console.info(callResult).toRPCObject)
// => [3, "abc123", { "status": "Accepted"}]

```
Expand Down
2 changes: 1 addition & 1 deletion src/message/ocpp-call-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface iOCPPCallError {
}

export class OCPPCallError {
public messageTypeId: OCPPMessageType;
public messageTypeId: OCPPMessageType.CALL_ERROR;
public messageId: string;

public errorCode: OCPPErrorCodeV16;
Expand Down
27 changes: 17 additions & 10 deletions src/message/ocpp-call-result.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>]
return [
OCPPMessageType.CALL_RESULT,
this.messageId,
this.payload as Record<string, unknown>,
];
}

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;
}
}
6 changes: 4 additions & 2 deletions src/message/ocpp-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends OCPPResponseTypeV16>(
payload: T
): OCPPCallResult {
return new OCPPCallResult({
messageId: this.messageId,
payload,
Expand Down
86 changes: 0 additions & 86 deletions test-report.xml

This file was deleted.

14 changes: 11 additions & 3 deletions tests/ocpp-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
input: string;
expected: Partial<T>;
Expand Down Expand Up @@ -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<AuthorizeResponseV16>({
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)(
Expand Down

0 comments on commit b9831af

Please sign in to comment.