csms

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: 12 Imported by: ? 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 csms is the central system side of OCPP-J as a library role: an http.Handler that accepts charging stations of 1.6, 2.0.1 and 2.1 at <path>/<identity>, authenticates them (security profiles 1 to 3 through a hook), routes their CALLs and 2.1 SENDs to handlers by action, checks every request and every answer against the OCA schemas, keeps a registry of connected stations and calls them.

It holds no charging business logic (authorization lists, tariffs, smart charging); handlers supply it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Identity

func Identity(r *http.Request) (string, error)

Identity is the charging station identity of a request to <path>/<identity>: the last path segment, unescaped.

Types

type Config

type Config struct {
	// Versions offered, in order of preference (default 2.1, 2.0.1, 1.6).
	// Variant spellings of each (ocpp.Subprotocols) are accepted too.
	Versions []string
	// NoSubprotocol is the version assumed for a station that offers no
	// subprotocol; empty refuses such a station.
	NoSubprotocol string
	// Authenticate admits a station before the WebSocket upgrade: check the
	// basic authentication password (profiles 1 and 2; ocpp.BasicAuthPassword,
	// ocpp.PasswordOK) or r.TLS (profile 3; ocpp.ClientCertNames). A
	// non-nil error refuses the station with HTTP 401. Nil admits everyone.
	Authenticate func(r *http.Request, identity string) error
	// 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 of requests and answers
	// (for a station whose deviations the application handles itself).
	SkipValidation bool
	// OnInvalid, when set, decides about a station's request that fails
	// the schema check: return nil to hand it to the handler anyway (a
	// deviation the application tolerates; see ocpp.Deviations) or an
	// error to refuse it, normally verr. Unset, such requests are refused.
	OnInvalid func(st *Station, action string, payload json.RawMessage, verr *ocpp.RPCError) error
	// CheckSerial closes the connection of a 2.x station using a TLS client
	// certificate whose BootNotification serial number differs from the
	// certificate's common name (security profile 3).
	CheckSerial bool
	// OnConnect and OnDisconnect follow the registry.
	OnConnect    func(st *Station)
	OnDisconnect func(st *Station, err error)
	// AcceptOptions are passed to websocket.Accept (Subprotocols is set by
	// the Server); for example compression or origin checks.
	AcceptOptions websocket.AcceptOptions
}

Config sets up a Server. The zero value accepts 2.1, 2.0.1 and 1.6 from anyone, which suits tests only: set Authenticate in production.

type HandlerFunc

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

HandlerFunc answers a CALL (or takes a 2.1 SEND, whose answer is dropped; ocpp.IsSend(ctx) tells them apart) from station st. Return an *ocpp.RPCError to choose the CALLERROR, any other error for an InternalError.

type Server

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

Server is a central system. Register handlers before serving.

func New

func New(cfg Config) *Server

New makes a Server.

func (*Server) Handle

func (s *Server) Handle(action string, h HandlerFunc)

Handle routes action, in every version, to h.

func (*Server) HandleVersion

func (s *Server) HandleVersion(version, action string, h HandlerFunc)

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

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP accepts one charging station and serves it until it leaves.

func (*Server) Station

func (s *Server) Station(identity string) *Station

Station is a connected charging station, or nil.

func (*Server) Stations

func (s *Server) Stations() []*Station

Stations lists the connected charging stations.

type Station

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

Station is one connected charging station.

func (*Station) Call

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

Call sends a CALL and returns the station's answer. Unless validation is off, the request is checked before it is sent (a failure is returned as an *ocpp.RPCError and nothing is sent) and the answer after: an answer outside the schema is returned with an *ocpp.ResultError (on 2.1 the station is also told with a CALLRESULTERROR). A CALLERROR comes back as an *ocpp.CallError.

func (*Station) CallInto

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

CallInto is Call decoding the answer into out.

func (*Station) Close

func (st *Station) Close(reason string)

Close disconnects the station.

func (*Station) Conn

func (st *Station) Conn() *ocpp.Conn

Conn is the RPC connection.

func (*Station) Done

func (st *Station) Done() <-chan struct{}

Done is closed when the station disconnects.

func (*Station) Get

func (st *Station) Get(key any) any

Get returns a value kept with Set.

func (*Station) Header

func (st *Station) Header() http.Header

Header is the upgrade request's header.

func (*Station) ID

func (st *Station) ID() string

ID is the station's identity.

func (*Station) Send

func (st *Station) Send(ctx context.Context, action string, payload any) error

Send sends a 2.1 SEND message (never answered).

func (*Station) Set

func (st *Station) Set(key, value any)

Set keeps an application value with the station.

func (*Station) String

func (st *Station) String() string

String names the station for logs.

func (*Station) TLS

func (st *Station) TLS() *tls.ConnectionState

TLS is the TLS state of the connection (nil over plain WebSocket).

func (*Station) Version

func (st *Station) Version() string

Version is the negotiated OCPP version.

Jump to

Keyboard shortcuts

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