index overview


Table of contents


Charge Point

ChargePoint (class)

Represents a connection to the central system

Signature

export declare class ChargePoint {
  constructor(
    readonly id: string,
    private readonly requestHandler: RequestHandler<
      CentralSystemAction<'v1.6-json'>,
      ValidationError | undefined,
      'v1.6-json'
    >,
    private readonly csUrl: string
  )
}

Example

import { ChargePoint } from '@voltbras/ts-ocpp';

const chargePointId = '123';
const centralSystemUrl = 'ws://central-system.com/ocpp';
const chargePoint = new ChargePoint(
 chargePointId,
 // request handler
 req => {
   if (req.action === 'RemoteStartTransaction')
     return {
       action: req.action,
       ocppVersion: req.ocppVersion,
       status: 'Accepted'
     };
   throw new Error('no handler defined')
 }),
 centralSystemUrl
);

connect (method)

Signature

async connect(): Promise<Connection<CentralSystemAction<'v1.6-json'>>>

sendRequest (method)

Signature

sendRequest<T extends ChargePointAction>(args: CPSendRequestArgs<T, 'v1.6-json'>): EitherAsync<OCPPRequestError, Response<T>>

Example

import { ChargePoint } from '@voltbras/ts-ocpp'

async function communicate(chargePoint: ChargePoint) {
  const response = await chargePoint.sendRequest({ action: 'Heartbeat', ocppVersion: 'v1.6-json', payload: {} })
  // it can be used in a functional way
  response.map((payload) => payload.currentTime)
  // or can be used in the standard JS way(will throw if there was an error)
  const unsafeResponse = response.unsafeCoerce()
}

close (method)

Signature

close()

utils

CPSendRequestArgs (type alias)

Signature

export type CPSendRequestArgs<T extends ChargePointAction<V>, V extends OCPPVersion> = {
  ocppVersion: 'v1.6-json'
  payload: Omit<Request<T, V>, 'action' | 'ocppVersion'>
  action: T
}