index overview

Sets up a central system, that can communicate with charge points


Table of contents


Central System

CentralSystem (class)

Represents the central system, can communicate with charge points

Signature

export declare class CentralSystem {
  constructor(
    port: number,
    cpHandler: RequestHandler<ChargePointAction, RequestMetadata>,
    options: CentralSystemOptions = {}
  )
}

Example

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

// port and request handler as arguments
const centralSystem = new CentralSystem(3000, (req, { chargePointId }) => {
  switch (req.action) {
    case 'Heartbeat':
      // returns a successful response
      // (we pass the action and ocpp version so typescript knows which fields are needed)
      return {
        action: req.action,
        ocppVersion: req.ocppVersion,
        currentTime: new Date().toISOString(),
      }
  }
  throw new Error('message not supported')
})

getConnectedChargePoints (method)

Signature

public getConnectedChargePoints(): string[]

addConnectionListener (method)

Signature

public addConnectionListener(listener: ConnectionListener)

close (method)

Signature

public async close(code?: number): Promise<void>

closeConnection (method)

Signature

public async closeConnection(chargePointId: string, code?: number): Promise<void>

sendRequest (method)

Signature

sendRequest<V extends OCPPVersion, T extends CentralSystemAction>(args: CSSendRequestArgs<T, V>): EitherAsync<OCPPRequestError, Response<T, V>>

utils

CSSendRequestArgs (type alias)

Signature

export type CSSendRequestArgs<T extends CentralSystemAction<V>, V extends OCPPVersion> =
  | {
      ocppVersion: 'v1.6-json'
      chargePointId: string
      payload: Omit<Request<T, V>, 'action' | 'ocppVersion'>
      action: T
    }
  | {
      ocppVersion: 'v1.5-soap'
      chargePointUrl: string
      chargePointId: string
      payload: Omit<Request<T, V>, 'action' | 'ocppVersion'>
      action: T
    }

CentralSystemOptions (type alias)

Signature

export type CentralSystemOptions = {
  /** if the chargepoint sends an invalid request(in ocpp v1.6), we can still forward it to the handler */
  rejectInvalidRequests?: boolean
  /** default is 0.0.0.0 */
  host?: string
  /**
   * can be used to log exactly what the chargepoint sends to this central system without any processing
   * @example
   * onRawSocketData: (data) => console.log(data.toString('ascii'))
   **/
  onRawSocketData?: (data: Buffer) => void
  onRawSoapData?: (type: 'replied' | 'received', data: string) => void
  onRawWebsocketData?: (data: WebSocket.Data, metadata: Omit<RequestMetadata, 'validationError'>) => void

  onWebsocketRequestResponse?: WebsocketRequestResponseListener
  onWebsocketError?: (error: Error, metadata: Omit<RequestMetadata, 'validationError'>) => void
  /** in milliseconds */
  websocketPingInterval?: number
  websocketRequestTimeout?: number

  /** can be used to authorize websockets before the socket formation */
  websocketAuthorizer?: (metadata: RequestMetadata) => Promise<boolean> | boolean
}

RequestMetadata (type alias)

Signature

export type RequestMetadata = {
  chargePointId: string
  httpRequest: IncomingMessage
  validationError?: ValidationError
}

WebsocketRequestResponseListener (type alias)

Signature

export type WebsocketRequestResponseListener = (
  initiator: 'chargepoint' | 'central-system',
  type: 'request' | 'response',
  data: OCPPJMessage,
  metadata: Omit<RequestMetadata, 'validationError'>
) => void