chargepoint

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package chargepoint is the charging station side of OCPP-J as a library role: it dials a central system as <url>/<identity> with the versions it offers, authenticates (security profiles 1 to 3), routes the central system's CALLs to handlers by action, checks every request and answer against the OCA schemas and, run with Run, reconnects with the randomised, doubling back-off of the 2.x OCPPCommCtrlr variables.

It holds no charger behaviour (connectors, transactions, meter values); handlers and the application's own calls supply it. ocpp-sim's simulated charge point is built on Dial.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConnected = errors.New("not connected to the central system")

ErrNotConnected is returned by Call while there is no connection.

Functions

func EndpointURL

func EndpointURL(base, id string) string

EndpointURL is base with the identity appended as an escaped path segment.

Types

type Backoff

type Backoff struct {
	Minimum     time.Duration // default 1 s
	RandomRange time.Duration // default 1 s
	RepeatTimes int           // default 5
}

Backoff is the reconnection back-off, read the way the OCPP 2.x OCPPCommCtrlr variables RetryBackOffWaitMinimum, RetryBackOffRandomRange and RetryBackOffRepeatTimes are: the first wait is Minimum plus a random part of RandomRange, and each later wait doubles the previous one, for at most RepeatTimes doublings; then it stays there.

func (Backoff) Wait

func (b Backoff) Wait(n int) time.Duration

Wait is the back-off before reconnection attempt n (0 for the first).

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a charging station's connection to its central system.

func New

func New(cfg Config) *Client

New makes a Client; Connect or Run opens it.

func (*Client) Call

func (c *Client) Call(ctx context.Context, action string, req any) (json.RawMessage, error)

Call sends a CALL and returns the central system's answer, checked as csms.Station.Call checks it: a request outside the schema is returned as an *ocpp.RPCError unsent, an answer outside it with an *ocpp.ResultError, a CALLERROR as an *ocpp.CallError.

func (*Client) CallInto

func (c *Client) CallInto(ctx context.Context, action string, req, out any) error

CallInto is Call decoding the answer into out.

func (*Client) Close

func (c *Client) Close(reason string)

Close drops the current connection; Run then reconnects.

func (*Client) Conn

func (c *Client) Conn() *ocpp.Conn

Conn is the current RPC connection, or nil.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect dials once and serves the connection in the background until it ends or ctx is done.

func (*Client) Handle

func (c *Client) Handle(action string, h HandlerFunc)

Handle routes the central system's action, in every version, to h.

func (*Client) HandleVersion

func (c *Client) HandleVersion(version, action string, h HandlerFunc)

HandleVersion routes action in version to h, ahead of Handle.

func (*Client) ID

func (c *Client) ID() string

ID is the charging station identity.

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run connects and reconnects with the back-off until ctx is done.

func (*Client) Send

func (c *Client) Send(ctx context.Context, action string, payload any) error

Send sends a 2.1 SEND message (never answered), checked against its schema first.

func (*Client) Version

func (c *Client) Version() string

Version is the version of the current connection ("" when there is none).

func (*Client) WaitConnected

func (c *Client) WaitConnected(ctx context.Context) error

WaitConnected waits until a connection is open.

type Config

type Config struct {
	// URL is the central system's endpoint without the identity, e.g.
	// wss://csms.example/ocpp; the identity is appended as a path segment.
	URL string
	// ID is the charging station identity (1 to 48 characters).
	ID string
	// Versions offered, in order of preference (default 2.1, 2.0.1, 1.6).
	Versions []string
	// Password for HTTP basic authentication (security profiles 1 and 2).
	Password string
	// TLS for wss (profiles 2 and 3; a client certificate for 3).
	TLS *tls.Config
	// Header holds further upgrade request headers.
	Header http.Header
	// Timeout of our CALLs and of each handler (default 30 s).
	Timeout time.Duration
	// Ping is the WebSocket ping interval (0: none).
	Ping time.Duration
	// SkipValidation turns off the schema checks.
	SkipValidation bool
	// OnInvalid, when set, decides about a central system request that
	// fails the schema check: nil hands it to the handler anyway, an error
	// (normally verr) refuses it. Unset, such requests are refused.
	OnInvalid func(c *Client, action string, payload json.RawMessage, verr *ocpp.RPCError) error
	// Backoff between reconnection attempts in Run.
	Backoff Backoff
	// OnConnect runs, on its own goroutine, after every connection (send
	// BootNotification there); OnDisconnect after it ends.
	OnConnect    func(ctx context.Context, c *Client)
	OnDisconnect func(c *Client, err error)
	// OnResultError is told about a 2.1 CALLRESULTERROR the central system
	// sent about one of our answers.
	OnResultError func(id, code, desc string)
}

Config describes a charging station's connection.

type Dialled

type Dialled struct {
	WS      *websocket.Conn
	Version string
}

Dialled is an open WebSocket to a central system and the version it chose.

func Dial

func Dial(ctx context.Context, cfg Config) (Dialled, error)

Dial opens the WebSocket of cfg: the identity appended to the URL, the versions offered, basic authentication, TLS. The version is the one the central system chose; a choice outside the offer is an error.

type HandlerFunc

type HandlerFunc func(ctx context.Context, c *Client, payload json.RawMessage) (any, error)

HandlerFunc answers a CALL of the central system. Return an *ocpp.RPCError to choose the CALLERROR, any other error for an InternalError.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL