connected-hil / ocpp-tools

Open charge point protocol tools. Schema validation, Typescript types, and other helpful utilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


OCPP tools

OCPP-J v1.6 and v2.0.1 message and payload types with validation.
Explore the docs »

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. License

About The Project

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 all todo items are cleard.

(back to top)

Getting Started

Add ocpp-tools to your project using any package manager, or by cloning this repository.

Installation

npm install @cshil/ocpp-tools

(back to top)

Usage

Typescript interfaces, types and utilities

This project includes

  • All OCPP v1.6 payloads have interfaces generated from JSON schema files.
  • All OCPP v2.0.1 payloads have interfaces generated from JSON schema files.
  • Types for valid CALL actions, request and response types as well as error code types for v1.6 and v2.0.1
  • Utility classes for RPC requests for CALL, CALL_RESULT and CALL ERROR.
  • Parsers for OCPP RPC calls and OCPP message payload validation for V1.6 and v2.0.1.

Parse a OCPP RPC message

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' }
}
*/

Construct a response

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"}]

Create a new RPC

using the general ocpp call:

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

const call = new OCPPCall<OCPPRequestTypeV16, ActionV16>({ version: ocppVersion.ocpp16, action: "Authorize", payload: { idTag: "abv123"})

or using the versioned ocpp call:

import { OCPPCallV201 } from "@cshil/ocpp-tools";

const call = new OCPPCallV201({
  action: "Authorize",
  payload: { idToken: { idToken: "abv123", type: "Central" } },
});

Validating OCPP JSON message payloads

import { isValidHeartbeatRequestV16, HeartbeatV16 } from "@cshil/ocpp-tools";

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

Get validation errors

import { validationErrors, schemas } from "@cshil/ocpp-tools";

const errors = validationErrors(schemas.v16.authorizeRequest, {});
// => ["#/required: must have required property 'idTag'"]

Using OCPP interfaces

import { AuthorizeResponseV16 } from "@cshil/ocpp-tools";

const message: AuthorizeResponseV16 = {
  idTagInfo: { status: "Accepted" },
};

(back to top)

Roadmap

  • Include OCPP v1.6 schemas
  • Include OCPP v2.0.1 schemas
  • Proper documentation

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

About

Open charge point protocol tools. Schema validation, Typescript types, and other helpful utilities.

License:MIT License


Languages

Language:TypeScript 99.9%Language:JavaScript 0.1%