node-ocpi Library Documentation
Overview
node-ocpi
is a Node.js library designed for the implementation of the Open Charge Point Interface (OCPI) protocol. It provides structured models and validation for various OCPI entities, making it ideal for developers building applications for EV charging stations and related services.
Features
- Models for key OCPI entities:
Location
, EVSE
, Connector
, CDR
, Command
, Transaction
, Feedback
, Meter
, Reservation
, Tariff
, User
. - Comprehensive validation for OCPI-compliant data structures.
- Supports a wide range of OCPI operations and functionalities.
Installation
You can install node-ocpi
using npm with the following command:
npm install https://github.com/hyndex/node-ocpi
This command fetches and installs the library directly from the specified GitHub repository.
Usage
Import the models you need from the node-ocpi
library as follows:
const { Location, EVSE, Connector, CDR, Command, Transaction, Feedback, Meter, Reservation, Tariff, User } = require('node-ocpi');
Model Usage Examples
Below are examples showing how to create instances of each model and perform data validation using objects as input:
Location
const locationData = {
id: 'loc1',
type: 'ON_STREET',
name: 'Main Street Charging Station',
address: '123 Main St',
city: 'Anytown',
postalCode: '12345',
country: 'USA',
coordinates: { latitude: 52.520008, longitude: 13.404954 },
};
const location = new Location(locationData);
location.validate();
EVSE
const evseData = {
uid: 'evse1',
locationId: 'loc1',
};
const evse = new EVSE(evseData);
evse.validate();
Connector
const connectorData = {
id: '1',
standard: 'IEC_62196_T2',
};
const connector = new Connector(connectorData);
connector.validate();
CDR
const cdrData = {
id: 'cdr1',
startDateTime: '2023-01-01T00:00:00Z',
};
const cdr = new CDR(cdrData);
cdr.validate();
... (Continue with other models in a similar fashion)
Using node-ocpi
as Middleware in Express.js
You can use node-ocpi
as middleware in Express.js applications to handle OCPI data:
const express = require('express');
const { Location, EVSE, Connector, CDR } = require('node-ocpi');
const app = express();
app.use(express.json());
app.post('/locations', (req, res) => {
try {
const location = new Location(req.body);
location.validate();
res.status(201).send(location);
} catch (error) {
res.status(400).send(error.message);
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, the node-ocpi
library is used to validate incoming data for a new charging location.
Contributing
Contributions to node-ocpi
are always welcome. To contribute:
- Fork the repository.
- Create a new branch for your feature.
- Implement your feature or bug fix.
- Write or adapt tests as needed.
- Update the documentation.
- Commit and push your changes.
- Submit a pull request.
License
node-ocpi
is released under the MIT License. See the LICENSE file for more details.