|
| 1 | + |
| 2 | +# Extrawest OCPP 2.0.1 package |
| 3 | + |
| 4 | +Typescript package implementing the JSON version of the Open Charge Point Protocol (OCPP). Currently OCPP 2.0.1 is |
| 5 | +supported. |
| 6 | + |
| 7 | +Open Charge Point Protocol (OCPP, <http://www.openchargealliance.org/>) is a communication protocol between multiple |
| 8 | +charging stations ("charge points") and a single management software ("central system"). |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +npm i @extrawest/node-ts-ocpp |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Central System |
| 19 | + |
| 20 | +```ts |
| 21 | +import { |
| 22 | + OcppServer, OcppClientConnection, BootNotificationRequest, BootNotificationResponse, |
| 23 | +} from 'ocpp-ts'; |
| 24 | + |
| 25 | +const centralSystemSimple = new OcppServer(); |
| 26 | +centralSystemSimple.listen(9220); |
| 27 | +centralSystemSimple.on('connection', (client: OcppClientConnection) => { |
| 28 | + console.log(`Client ${client.getCpId()} connected`); |
| 29 | + client.on('close', (code: number, reason: Buffer) => { |
| 30 | + console.log(`Client ${client.getCpId()} closed connection`, code, reason.toString()); |
| 31 | + }); |
| 32 | + |
| 33 | + client.on('BootNotification', (request: BootNotificationRequest, cb: (response: BootNotificationResponse) => void) => { |
| 34 | + const response: BootNotificationResponse = { |
| 35 | + status: 'Accepted', |
| 36 | + currentTime: new Date().toISOString(), |
| 37 | + interval: 60, |
| 38 | + }; |
| 39 | + cb(response); |
| 40 | + }); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### Charging Point |
| 45 | + |
| 46 | +```ts |
| 47 | +import { |
| 48 | + BootNotificationRequest, |
| 49 | + BootNotificationResponse, |
| 50 | + OcppClient, OcppError, |
| 51 | +} from 'ocpp-ts'; |
| 52 | + |
| 53 | +const chargingPointSimple = new OcppClient('CP1111'); |
| 54 | +chargingPointSimple.on('error', (err: Error) => { |
| 55 | + console.log(err.message); |
| 56 | +}); |
| 57 | +chargingPointSimple.on('close', () => { |
| 58 | + console.log('Connection closed'); |
| 59 | +}); |
| 60 | + |
| 61 | +chargingPointSimple.on('connect', async () => { |
| 62 | + const boot: BootNotificationRequest = { |
| 63 | + chargingStation: { |
| 64 | + model: "eParking", |
| 65 | + vendorName: "NECU-T2" |
| 66 | + }, |
| 67 | + reason: "Unknown" |
| 68 | + }; |
| 69 | + |
| 70 | + try { |
| 71 | + const bootResp: BootNotificationResponse = await chargingPointSimple.callRequest('BootNotification', boot); |
| 72 | + if (bootResp.status === 'Accepted') { |
| 73 | + console.log('Bootnotification accepted'); |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + if (e instanceof Error || e instanceof OcppError) { |
| 77 | + console.error(e.message); |
| 78 | + } |
| 79 | + } |
| 80 | +}); |
| 81 | +chargingPointSimple.connect('ws://localhost:9220/'); |
| 82 | +``` |
| 83 | + |
| 84 | +## Security |
| 85 | + |
| 86 | +Add required certificates for Central System, note from OCPP protocol: |
| 87 | + |
| 88 | +*As some Charge Point implementations are using embedded systems with limited computing |
| 89 | +resources, we impose an additional restriction on the TLS configuration on the server side:* |
| 90 | + |
| 91 | +* The TLS certificate SHALL be an RSA certificate with a size no greater than 2048 bytes |
| 92 | + |
| 93 | +```ts |
| 94 | +centralSystemSimple.on('authorization', (cbId: string, req: IncomingMessage, cb: (err?: Error) => void) => { |
| 95 | + console.log('authorization', cbId, req.headers.authorization); |
| 96 | + // validate authorization header |
| 97 | + // cb(new Error('Unathorized')); // Deny |
| 98 | + cb(); // Accept |
| 99 | +}); |
| 100 | +centralSystemSimple.listen(9220, { |
| 101 | + cert: fs.readFileSync('cert.pem'), |
| 102 | + key: fs.readFileSync('key.pem'), |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +If the central system requires authorization, an authorization header can be placed as the second parameter. |
| 107 | + |
| 108 | +```ts |
| 109 | +chargingPointSimple.connect('wss://eparking.fi/ocpp/', { |
| 110 | + Authorization: getBasicAuth(), |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
0 commit comments