@extrawest/node-ts-ocpp
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Maintaner Maintenance GitHub release GitHub tag

Test coverage

Statements Branches Functions Lines

Extrawest OCPP 2.0.1 package

Based on great package sepych/ocpp-ts for OCPP 1.6 version.

Typescript package implementing the JSON version of the Open Charge Point Protocol (OCPP). Currently OCPP 2.0.1 is supported.

Open Charge Point Protocol (OCPP, http://www.openchargealliance.org/) is a communication protocol between multiple charging stations ("charge points") and a single management software ("central system").

Installation

npm i @extrawest/node-ts-ocpp

Usage

Central System

import {
    OcppServer, OcppClientConnection, BootNotificationRequest, BootNotificationResponse,
} from 'ocpp-ts';

const centralSystemSimple = new OcppServer();
centralSystemSimple.listen(9220);
centralSystemSimple.on('connection', (client: OcppClientConnection) => {
    console.log(`Client ${client.getCpId()} connected`);
    client.on('close', (code: number, reason: Buffer) => {
        console.log(`Client ${client.getCpId()} closed connection`, code, reason.toString());
    });

    client.on('BootNotification', (request: BootNotificationRequest, cb: (response: BootNotificationResponse) => void) => {
        const response: BootNotificationResponse = {
            status: 'Accepted',
            currentTime: new Date().toISOString(),
            interval: 60,
        };
        cb(response);
    });
});

Charging Point

import {
    BootNotificationRequest,
    BootNotificationResponse,
    OcppClient, OcppError,
} from 'ocpp-ts';

const chargingPointSimple = new OcppClient('CP1111');
chargingPointSimple.on('error', (err: Error) => {
    console.log(err.message);
});
chargingPointSimple.on('close', () => {
    console.log('Connection closed');
});

chargingPointSimple.on('connect', async () => {
    const boot: BootNotificationRequest = {
        chargingStation: {
            model: "someModel",
            vendorName: "someVendor"
        },
        reason: "Unknown"
    };

    try {
        const bootResp: BootNotificationResponse = await chargingPointSimple.callRequest('BootNotification', boot);
        if (bootResp.status === 'Accepted') {
            console.log('Bootnotification accepted');
        }
    } catch (e) {
        if (e instanceof Error || e instanceof OcppError) {
            console.error(e.message);
        }
    }
});
chargingPointSimple.connect('ws://localhost:9220/');

Security

Add required certificates for Central System, note from OCPP protocol:

As some Charge Point implementations are using embedded systems with limited computing resources, we impose an additional restriction on the TLS configuration on the server side:

  • The TLS certificate SHALL be an RSA certificate with a size no greater than 2048 bytes
centralSystemSimple.on('authorization', (cbId: string, req: IncomingMessage, cb: (err?: Error) => void) => {
    console.log('authorization', cbId, req.headers.authorization);
    // validate authorization header
    // cb(new Error('Unathorized')); // Deny
    cb(); // Accept
});
centralSystemSimple.listen(9220, {
    cert: fs.readFileSync('cert.pem'),
    key: fs.readFileSync('key.pem'),
});

If the central system requires authorization, an authorization header can be placed as the second parameter.

chargingPointSimple.connect('wss://somecs.com/ocpp/', {
    Authorization: getBasicAuth(),
});

Created by Extrawest Node.js Team Extrawest.com, 2023

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.3
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.3
    1
  • 1.0.2
    1
  • 1.0.1
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i @extrawest/node-ts-ocpp

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

2.18 MB

Total Files

713

Last publish

Collaborators

  • nariman.mamutov
  • danylo-shamaiev-ew
  • andrii-kapustin-ew
  • dmytro-batazov-ew
  • serhii_vershynin
  • oleksandr.fedotov
  • anatolii-krotov-ew
  • dmytro_podolianskyi