Skip to content

Commit

Permalink
Merge pull request #17 from connected-hil/feat/ocpp_201
Browse files Browse the repository at this point in the history
Feat/ocpp 201
  • Loading branch information
larte authored Apr 8, 2024
2 parents efccae1 + 08c3f62 commit 763d8ef
Show file tree
Hide file tree
Showing 361 changed files with 26,167 additions and 1,387 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Merge to main
on:
push:
branches: [main]
branches: [main, release]
pull_request:
branches: [main]

Expand All @@ -27,4 +27,4 @@ jobs:
run_install: true
- run: pnpm check-types
- run: pnpm run lint
- run: pnpm run test
- run: pnpm run test:coverage
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
commit: false
- run: |
pnpm build
pnpm publish --access public --publish-branch main --no-git-checks
pnpm custom:publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
112 changes: 80 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h3 align="center">OCPP tools</h3>

<p align="center">
OCPP-J v1.6 message and payload types with validation.
OCPP-J v1.6 and v2.0.1 message and payload types with validation.
<br />
<a href="docs/index.html"><strong>Explore the docs »</strong></a>
<br />
Expand All @@ -33,7 +33,12 @@
<li>
<a href="#usage">Usage</a>
<ul>
<li><a href="#validating-ocpp-json-messages">Validating OCPP JSON messages</a></li>
<li><a href="#validating-ocpp-json-messages">Typescript interfaces, types and utilities</a></li>
<li><a href="#parse-ocpp-rpc-messages">Parse OCPP RPC messages</a></li>
<li><a href="#construct-a-response">Construct a response</a></li>
<li><a href="#create-a-new-rpc">Create a new RPC</a></li>
<li><a href="#validating-ocpp-json-message-payloads">Validating OCPP JSON message payloads</a></li>
<li><a href="#get-validation-errors">Get validation errors</a></li>
<li><a href="#using-ocpp-interfaces">Using OCPP interfaces</a></li>
</ul>
</li>
Expand All @@ -48,7 +53,7 @@

**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.
**Note**: Things are changing, and backwards compatibility might be broken until all todo items are cleard.

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

Expand All @@ -72,54 +77,100 @@ npm install @cshil/ocpp-tools

This project includes

- All OCPP v1.6 payloads have [interfaces](src/generated/v16/types) generated from JSON schema files.
- Types for valid CALL [actions, request and repsonse types as well as error code types](src/generated/v16/types/index.ts).
- All OCPP v1.6 payloads have [interfaces](src/types/v16) generated from JSON schema files.
- All OCPP v2.0.1 payloads have [interfaces](src/types/v201/) generated from JSON schema files.
- Types for valid CALL actions, request and response types as well as error code types for [v1.6](src/types/v16/index.ts) and [v2.0.1](src/types/v201/index.ts)
- Utility classes for RPC requests for [CALL](src/message/ocpp-call.ts), [CALL_RESULT](src/message/ocpp-call-result.ts) and [CALL ERROR](src/message/ocpp-call-error.ts).
- Parsers for [OCPP RPC calls](src/validation/index.ts) and [OCPP message payloads](src/generated/v16/validators.ts).
- Parsers for [OCPP RPC calls](src/validation/index.ts) and OCPP message payload validation for [V1.6](src/validation/v16/index.ts) and [v2.0.1](src/validation/v201/index.ts).

### Validating OCPP JSON message payloads
### Parse a OCPP RPC message

```typescript
import { isValidHeartbeatRequestV16, HeartbeatV16 } from "@cshil/ocpp-tools";
import { ocppVersion, parseOCPPMessage, OCPPCallResult } from "@cshil/ocpp-tools";

const authorizeRequest = parseOCPPMessage(
"[2, \"message-abc123\", "Authorize", {\"idTag\": \"abc-def-123\""}]",
{
version: ocppVersion.ocpp16,
validateMessage: true,
validatePayload: true // payload is validated for CALL type RPC messages
}
)
console.log(authorizeRequest)
/*
OCPPCallV16 {
version: 'ocpp1.6',
messageId: 'message-abc123',
messageTypeId: 2,
action: 'Authorize',
payload: { idTag: 'abc-def-123' }
}
*/
```

const data = JSON.parse("{}"")
const result = isValidHeartbeatRequestV16(data) // => true
### Construct a response

```typescript
import { parseOCPPMessage, AuthorizeResponseV16, OCPPCall} from "@cshil/ocpp-tools";

const request = parseOCPPMessage(
"[2, \"abc123\", \"Authorize\", {\"idTag\": \"abc-123-abc\""}]",
)

const callResult = request.toCallResponse<AuthorizeResponseV16>({idTagInfo: { status: "Accepted"}})
console.info(callResult).toRPCObject)
// => [3, "abc123", { "status": "Accepted"}]

```

### Using OCPP interfaces
## Create a new RPC

using the general ocpp call:

```typescript
import { AuthorizeResponseV16 } from "@cshil/ocpp-tools";
import { OCPPCall, OCPPRequestTypeV16, ActionV16, ocppVersion } from "@cshil/ocpp-tools";

const message: AuthorizeResponseV16 = {
idTagInfo: { status: "Accepted" }
};
const call = new OCPPCall<OCPPRequestTypeV16, ActionV16>({ version: ocppVersion.ocpp16, action: "Authorize", payload: { idTag: "abv123"})
```
### Parsing full OCPP RPC messages
or using the versioned ocpp call:
```typescript
import { parseOCPPMessage, OCPPCallResult } from "@cshil/ocpp-tools";
import { OCPPCallV201 } from "@cshil/ocpp-tools";

const authorizeResponse = parseOCPPMessage(
"[3, \"abc123\", {\"status\": \"Accepted\""}]",
) as OCPPCallResult
const call = new OCPPCallV201({
action: "Authorize",
payload: { idToken: { idToken: "abv123", type: "Central" } },
});
```
### Construct a response to a RPC call
### Validating OCPP JSON message payloads
```typescript
import { parseOCPPMessage, AuthorizeResponseV16, OCPPCall} from "@cshil/ocpp-tools";
import { isValidHeartbeatRequestV16, HeartbeatV16 } from "@cshil/ocpp-tools";

const request = parseOCPPMessage(
"[2, \"abc123\", \"Authorize\", {\"idTag\": \"abc-123-abc\""}]",
) as OCPPCall
const data = JSON.parse("{}"")
const result = isValidHeartbeatRequestV16(data) // => true

const callResult = request.toCallResponse<AuthorizeResponseV16>({idTagInfo: { status: "Accepted"}})
console.info(callResult).toRPCObject)
// => [3, "abc123", { "status": "Accepted"}]
```
### Get validation errors
```typescript
import { validationErrors, schemas } from "@cshil/ocpp-tools";

const errors = validationErrors(schemas.v16.authorizeRequest, {});
// => ["#/required: must have required property 'idTag'"]
```
### Using OCPP interfaces
```typescript
import { AuthorizeResponseV16 } from "@cshil/ocpp-tools";

const message: AuthorizeResponseV16 = {
idTagInfo: { status: "Accepted" },
};
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand All @@ -129,7 +180,7 @@ console.info(callResult).toRPCObject)
## Roadmap
- [x] Include OCPP v1.6 schemas
- [ ] Include OCPP v2.0.1 schemas
- [x] Include OCPP v2.0.1 schemas
- [ ] Proper documentation
See the [open issues](https://github.com/connected-hil/ocpp-tools/issues) for a full list of proposed features (and known issues).
Expand Down Expand Up @@ -160,6 +211,3 @@ Don't forget to give the project a star! Thanks again!
Distributed under the MIT License. See `LICENSE.txt` for more information.
<p align="right">(<a href="#readme-top">back to top</a>)</p>

[logo]: https://raw.githubusercontent.com/connected-hil/ocpp-tools/main/images/logo.webp
[logo-url]: https://github.com/connected-hil/ocpp-tools/
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"name": "@cshil/ocpp-tools",
"version": "0.0.1",
"version": "1.1.0",
"description": "A package containing schemas, types and validations for OCPP protocol version 1.6.x",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"main": "./index.js",
"types": "./index.d.ts",
"files": ["*"],
"repository": {
"type": "git",
"url": "https://github.com/connected-hil/ocpp-tools.git"
Expand All @@ -17,13 +14,13 @@
"generate-docs": "typedoc --out docs/ --entryPointStrategy expand ./src ./images",
"check-types": "tsc --noEmit",
"generate": "pnpm ts-node scripts/compile-schemas-to-types.ts",
"build": "pnpm tsup",
"test": "jest"
"build": "rimraf dist; tsc --project tsconfig.build.json",
"custom:publish": "cp package.json dist && cd dist && pnpm publish --access public --publish-branch main --no-git-checks",
"custom:pack": "cp packge.json dist/ && cd dist && pnpm pack --pack-destination ../",
"test": "jest --no-coverage",
"test:coverage": "jest"
},
"keywords": [
"OCPP",
"schema"
],
"keywords": ["OCPP", "schema"],
"author": "l-arte <[email protected]>",
"license": "MIT",
"devDependencies": {
Expand All @@ -42,9 +39,9 @@
"jest": "^29.7.0",
"jest-sonar-reporter": "^2.0.0",
"json-schema-to-typescript": "^13.1.2",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typedoc": "^0.25.12",
"typescript": "5.3.3",
"typescript-eslint": "^7.4.0"
Expand Down
Loading

0 comments on commit 763d8ef

Please sign in to comment.