ocppj

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ocppj implements OCPP-J: OCPP messages framed as JSON arrays over a WebSocket connection. It knows nothing about individual OCPP actions, so every OCPP version that uses this framing can sit on top of it.

Index

Constants

View Source
const (
	ErrNotImplemented                = "NotImplemented"
	ErrNotSupported                  = "NotSupported"
	ErrInternalError                 = "InternalError"
	ErrProtocolError                 = "ProtocolError"
	ErrSecurityError                 = "SecurityError"
	ErrFormationViolation            = "FormationViolation"
	ErrPropertyConstraintViolation   = "PropertyConstraintViolation"
	ErrOccurrenceConstraintViolation = "OccurrenceConstraintViolation"
	ErrTypeConstraintViolation       = "TypeConstraintViolation"
	ErrGenericError                  = "GenericError"
)

Error codes defined by OCPP-J 1.6.

Variables

View Source
var ErrClosed = errors.New("ocppj: connection closed")

ErrClosed is returned by Call when the connection is closed before the answer arrives.

Functions

This section is empty.

Types

type CallError

type CallError struct {
	Code        string
	Description string
	Details     json.RawMessage
}

CallError is an error answer to a call. It is returned by Conn.Call when the other side answers with an error, and returned by a Handler to answer with one.

func (*CallError) Error

func (e *CallError) Error() string

type Conn

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

Conn is one OCPP-J connection. Both ends of a connection use the same type: each may call the other and must answer the other's calls.

func Dial

func Dial(ctx context.Context, url string, opts DialOptions) (*Conn, error)

Dial opens a connection to url.

func NewConn

func NewConn(ws *websocket.Conn, opts Options) *Conn

NewConn wraps an established WebSocket connection and starts reading from it.

func (*Conn) Call

func (c *Conn) Call(ctx context.Context, action string, payload, result any) error

Call sends a call and waits for its answer, which is decoded into result unless result is nil. If the other side answers with an error, Call returns a *CallError.

func (*Conn) Close

func (c *Conn) Close() error

Close ends the connection with a normal WebSocket close handshake.

func (*Conn) Done

func (c *Conn) Done() <-chan struct{}

Done is closed when the connection has ended, for any reason.

func (*Conn) Drop

func (c *Conn) Drop()

Drop ends the connection abruptly, without a close handshake, as a power cut or a network failure would.

func (*Conn) Err

func (c *Conn) Err() error

Err reports why the connection ended. It is only meaningful after Done is closed.

func (*Conn) RemoteAddr added in v0.1.4

func (c *Conn) RemoteAddr() netip.Addr

RemoteAddr returns the address a dialed connection goes to. It is the zero value for a connection that was accepted, or when it is not known.

func (*Conn) Subprotocol

func (c *Conn) Subprotocol() string

Subprotocol returns the WebSocket subprotocol both sides agreed on, or "" if none was agreed.

type DialOptions

type DialOptions struct {
	Options
	// Subprotocol is the WebSocket subprotocol to request, such as "ocpp1.6".
	Subprotocol string
	// Username and Password, when Password is set, are sent as HTTP Basic
	// authentication.
	Username string
	Password string
	// InsecureSkipVerify disables TLS certificate verification.
	InsecureSkipVerify bool
}

DialOptions configure Dial.

type Direction

type Direction int

Direction says which way a frame travelled, seen from this end.

const (
	Sent Direction = iota
	Received
)

type Handler

type Handler func(ctx context.Context, action string, payload json.RawMessage) (any, *CallError)

Handler answers a call from the other side. It returns the result payload, or a CallError to answer with an error. A nil payload is sent as {}.

type Message

type Message struct {
	Type MessageType
	ID   string

	Action  string          // Call
	Payload json.RawMessage // Call and CallResult

	ErrorCode        string          // CallError
	ErrorDescription string          // CallError
	ErrorDetails     json.RawMessage // CallError
}

Message is one decoded OCPP-J frame.

Call:       [2, "<id>", "<action>", {payload}]
CallResult: [3, "<id>", {payload}]
CallError:  [4, "<id>", "<code>", "<description>", {details}]

func Decode

func Decode(data []byte) (Message, error)

Decode reads one OCPP-J frame.

func (Message) Encode

func (m Message) Encode() ([]byte, error)

Encode returns the wire form of m.

type MessageType

type MessageType int

MessageType is the first element of an OCPP-J frame.

const (
	TypeCall       MessageType = 2
	TypeCallResult MessageType = 3
	TypeCallError  MessageType = 4
)

type Options

type Options struct {
	// Handler answers incoming calls. Without one, every call is answered
	// with NotImplemented.
	Handler Handler
	// OnFrame, if set, is given every frame sent and received, for logging.
	// It must not block.
	OnFrame func(dir Direction, frame []byte)
}

Options configure a Conn.

Jump to

Keyboard shortcuts

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