cp

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package cp implements the OCPP Charge Point (client) side.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call[Req, Resp any](ctx context.Context, c *Client, m ocppj.Message[Req, Resp], req Req) (Resp, error)

Call sends a typed message from this charge point to the CSMS.

func CallAsync added in v0.1.3

func CallAsync[Req, Resp any](ctx context.Context, c *Client, m ocppj.Message[Req, Resp], req Req, cb func(Resp, error)) error

CallAsync sends a typed message to the CSMS without blocking and delivers the typed response (or error) to cb. With WithSerializedCalls the calls are queued FIFO and sent one outstanding at a time; otherwise they run concurrently. It returns synchronously only if the call could not be accepted (e.g. not connected, ocppj.ErrQueueFull). Unlike Call, this does not use the offline queue — it requires a live connection. See dispatcher.DoCallAsync.

func CallRaw

func CallRaw(ctx context.Context, c *Client, action string, payload []byte) ([]byte, error)

CallRaw sends an action with a raw JSON payload and returns the raw response. Used by tools like the simulator that operate on untyped messages.

func CallRawAsync added in v0.1.3

func CallRawAsync(ctx context.Context, c *Client, action string, payload []byte, cb func([]byte, error)) error

CallRawAsync is the untyped form of CallAsync.

func On

func On[Req, Resp any](c *Client, m ocppj.Message[Req, Resp], h func(ctx context.Context, req Req) (Resp, error)) error

On registers a typed handler for a message the CSMS sends to this charge point.

func OnSend added in v0.1.7

func OnSend[Req any](c *Client, m ocppj.SendMessage[Req], h func(ctx context.Context, req Req) error) error

OnSend registers a typed handler for an OCPP 2.1 SEND this charge point receives. SEND messages are unconfirmed: the handler returns only an error (logged, never sent back).

func Send added in v0.1.7

func Send[Req any](ctx context.Context, c *Client, m ocppj.SendMessage[Req], req Req) error

Send sends a typed OCPP 2.1 SEND from this charge point to the CSMS. It returns once the frame is written; no response is awaited.

Types

type Client

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

Client is an OCPP charge point.

func NewClient

func NewClient(cpID, csmsURL string, opts ...Option) *Client

NewClient creates a charge point client. Call Connect or Run to establish.

func (*Client) Close

func (c *Client) Close()

Close tears down the current connection.

func (*Client) Connect

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

Connect establishes a single connection (no auto-reconnect).

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected reports whether a live connection exists.

func (*Client) NegotiatedProtocol

func (c *Client) NegotiatedProtocol() string

NegotiatedProtocol returns the subprotocol chosen during the handshake.

func (*Client) Run

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

Run connects and maintains the connection with exponential backoff until ctx is cancelled. In-flight calls fail on disconnect (OCPP has no idempotency guarantee).

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Client.

func WithAsyncQueueSize added in v0.1.3

func WithAsyncQueueSize(n int) Option

WithAsyncQueueSize bounds the per-connection FIFO queue used by CallAsync when WithSerializedCalls is set (default 64). Enqueuing beyond it returns ocppj.ErrQueueFull.

func WithBasicAuth added in v0.1.2

func WithBasicAuth(username, password string) Option

WithBasicAuth sets HTTP Basic authentication for the WebSocket dial.

func WithCallTimeout

func WithCallTimeout(d time.Duration) Option

WithCallTimeout sets the per-call timeout.

func WithCompression added in v0.1.7

func WithCompression(m transport.CompressionMode) Option

WithCompression sets the RFC 7692 permessage-deflate mode for the dial. Defaults to transport.CompressionNoContextTakeover; pass transport.CompressionDisabled to opt out.

func WithHTTPHeader added in v0.1.2

func WithHTTPHeader(key, value string) Option

WithHTTPHeader appends an HTTP header to the WebSocket dial request.

func WithHeartbeatInterval

func WithHeartbeatInterval(d time.Duration) Option

WithHeartbeatInterval sets the OCPP Heartbeat interval.

func WithLenientSchema added in v0.1.4

func WithLenientSchema() Option

WithLenientSchema enables lenient JSON-Schema validation: structurally broken messages are rejected, while benign violations are logged and passed, and enum case mismatches are normalized to canonical values. Last-wins with WithStrictSchema / WithTolerantSchema.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the structured logger.

func WithOfflineQueue added in v0.1.2

func WithOfflineQueue(capacity int) Option

WithOfflineQueue enables a bounded FIFO queue for CP-originated calls while disconnected.

func WithOnConnect added in v0.1.2

func WithOnConnect(fn func()) Option

WithOnConnect registers a callback fired after a successful connection.

func WithOnDisconnect added in v0.1.2

func WithOnDisconnect(fn func(error)) Option

WithOnDisconnect registers a callback fired after the active connection drops.

func WithOnReconnect added in v0.1.2

func WithOnReconnect(fn func()) Option

WithOnReconnect registers a callback fired after Run re-establishes a dropped connection.

func WithRequireSignature added in v0.1.7

func WithRequireSignature(require bool) Option

WithRequireSignature rejects inbound signed messages that fail verification instead of unwrapping and processing them.

func WithRetryInFlightCalls added in v0.1.2

func WithRetryInFlightCalls() Option

WithRetryInFlightCalls makes the offline queue re-send a CALL that was already in-flight when the connection dropped, instead of failing it. Off by default: OCPP gives no idempotency guarantee, so an in-flight CALL the peer may already have received fails with ocppj.ErrConnClosed rather than risk double execution. Only affects connections using WithOfflineQueue.

func WithSchemaRegistry

func WithSchemaRegistry(r *schema.Registry) Option

WithSchemaRegistry sets the schema registry used for first-layer validation.

func WithSerializedCalls added in v0.1.2

func WithSerializedCalls() Option

WithSerializedCalls limits outbound OCPP Calls to one outstanding request.

func WithSigner added in v0.1.7

func WithSigner(s *signing.Signer) Option

WithSigner signs outbound CALL/SEND messages (OCPP 2.1 Signed Messages).

func WithStrictSchema

func WithStrictSchema(strict bool) Option

WithStrictSchema controls whether schema validation failures reject the message. Passing false turns schema validation off. If WithStrictSchema and WithTolerantSchema are both used, the last option in the list wins.

func WithSubProtocols

func WithSubProtocols(p ...string) Option

WithSubProtocols sets offered subprotocols.

func WithTLSConfig added in v0.1.2

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig sets the TLS configuration used by the WebSocket dial.

func WithTolerantSchema

func WithTolerantSchema() Option

WithTolerantSchema enables schema validation that logs validation failures but continues processing messages. If WithStrictSchema and WithTolerantSchema are both used, the last option in the list wins.

func WithVerifier added in v0.1.7

func WithVerifier(v *signing.Verifier) Option

WithVerifier verifies inbound signed CALL/SEND messages.

func WithWebSocketPingInterval added in v0.1.2

func WithWebSocketPingInterval(d time.Duration) Option

WithWebSocketPingInterval sets the transport ping interval.

func WithWebSocketPongWait added in v0.1.2

func WithWebSocketPongWait(d time.Duration) Option

WithWebSocketPongWait sets the transport pong timeout.

func WithWebSocketReadTimeout added in v0.1.6

func WithWebSocketReadTimeout(d time.Duration) Option

WithWebSocketReadTimeout sets the read idle timeout: the connection is closed if no inbound frame (data, ping, or pong) arrives within d. 0 disables it.

Jump to

Keyboard shortcuts

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