charger

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 22 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 charger simulates one charger: a single charge point ID with its own connection to the CSMS.

Index

Constants

View Source
const (
	Connecting = "connecting"
	Online     = "online"
	Offline    = "offline"
)

Connection states, as shown by "plugyard ps".

View Source
const (
	FailDownload = "download"
	FailInstall  = "install"
	FailUpload   = "upload"
)

What Fail can make fail.

View Source
const Stopped = "stopped"

Stopped is the connection state shown for a charger that is powered off.

Variables

Failures lists what Fail accepts.

Functions

This section is empty.

Types

type CSMS

type CSMS struct {
	// URL is the CSMS endpoint, ws:// or wss://. The charge point ID is
	// appended to its path.
	URL string
	// Password, if set, is sent with HTTP Basic authentication, with the
	// charge point ID as the user name.
	Password string
	// InsecureSkipVerify disables TLS certificate verification.
	InsecureSkipVerify bool
}

CSMS says how to reach the CSMS.

type Charger

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

Charger is one simulated charger. It is powered on once, with Run; a charger that has been powered off is replaced by a new one made from its saved state.

func New

func New(cfg Config) (*Charger, error)

New checks cfg and returns a charger that has not been powered on yet.

func (*Charger) ClearFault

func (c *Charger) ClearFault(connectorID int) (string, error)

ClearFault recovers a connector, or the charger as a whole, from its fault.

func (*Charger) Fail added in v0.1.2

func (c *Charger) Fail(what string) (string, error)

Fail makes the next firmware download, firmware installation or diagnostics upload fail, as a broken link or a bad file would. It stays set until that step has failed, or the charger is powered off.

func (*Charger) Fault

func (c *Charger) Fault(connectorID int, fault Fault) (string, error)

Fault makes a connector, or with connector 0 the charger as a whole, report a fault. A transaction running there ends.

func (*Charger) ID

func (c *Charger) ID() string

ID returns the charge point ID.

func (*Charger) Kill

func (c *Charger) Kill()

Kill cuts the charger's power: Run returns at once, the connection drops with no message, and nothing that happened since the last save is kept.

func (*Charger) Offline

func (c *Charger) Offline(d time.Duration) (string, error)

Offline takes the charger's network connection down. The charger keeps running and queues what it has to tell the CSMS. It stays away until Online, or for the given time when that is not zero.

func (*Charger) Online

func (c *Charger) Online() (string, error)

Online restores the network connection taken down by Offline.

func (*Charger) Plug

func (c *Charger) Plug(ctx context.Context, connectorID int, params ev.Params) (string, error)

Plug plugs an EV into a connector. It returns a description of what happened, or an error if the action could not be carried out.

func (*Charger) Reboot

func (c *Charger) Reboot() (string, error)

Reboot reboots the charger as a person at the charger would: transactions end, and the charger boots again.

func (*Charger) Run

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

Run powers the charger on and keeps it running until ctx is cancelled, which shuts it down in an orderly way, or until Kill cuts its power. While running, the charger reconnects by itself whenever the connection is lost. A charger can be powered on only once.

func (*Charger) Snapshot

func (c *Charger) Snapshot() Snapshot

Snapshot returns the charger's current state.

func (*Charger) State

func (c *Charger) State() State

State returns the state of a running charger as of now.

func (*Charger) Tap

func (c *Charger) Tap(ctx context.Context, connectorID int, tag string) (string, error)

Tap presents a tag at a connector: it stops the connector's transaction if the tag is entitled to, and otherwise asks the CSMS to authorize the tag and starts a transaction. A tag the CSMS rejects is an outcome, not an error.

func (*Charger) URL

func (c *Charger) URL() string

URL returns the full address the charger connects to: the CSMS URL with the charge point ID appended.

func (*Charger) Unplug

func (c *Charger) Unplug(connectorID int) (string, error)

Unplug removes the EV from a connector, ending its transaction if there is one.

type Config

type Config struct {
	// ID is the charge point ID.
	ID    string
	Model *model.Model
	CSMS  CSMS
	// Speed multiplies the rate of EV charging and energy accumulation.
	// Zero means 1. Protocol timers are not affected.
	Speed  float64
	Timing Timing
	// BeforeDial, if set, is called before every connection attempt and
	// may wait. A site uses it to pace the connections of its chargers.
	BeforeDial func(ctx context.Context) error
	// Log, if set, receives the charger's events and OCPP messages. It is
	// called from several goroutines and must not block.
	Log func(Entry)
	// Restore, if set, is the state saved when the charger last had power.
	Restore *State
	// Diagnostics, if set, returns the charger's log between two moments, as
	// the file GetDiagnostics asks for. A zero time leaves that end open.
	Diagnostics func(from, to time.Time) ([]byte, error)
	// AllowUploads lists the hosts diagnostics may be uploaded to besides
	// the machine the CSMS is on.
	AllowUploads []string
	// UploadsInsecureSkipVerify turns off the check of the certificate of
	// an https or ftps server diagnostics are uploaded to.
	UploadsInsecureSkipVerify bool
	// Save, if set, is given the charger's state whenever something worth
	// keeping has changed: at once for transactions and queued messages, and
	// every MeterSaveInterval seconds for meter readings.
	Save func(State) error
}

Config describes one charger.

type ConfigKey

type ConfigKey struct {
	Key      string `json:"key"`
	Value    string `json:"value"`
	Readonly bool   `json:"readonly"`
}

ConfigKey is one configuration key of a charger.

func Configuration

func Configuration(m *model.Model, st *State) []ConfigKey

Configuration lists the configuration keys of a charger of the given model with the given state, sorted by key. A nil state is a charger that has never run.

type ConnectorSnapshot

type ConnectorSnapshot struct {
	ID     int    `json:"id"`
	Status string `json:"status"`
	// TransactionID is zero when there is no transaction, or when the CSMS
	// has not confirmed it yet.
	TransactionID int `json:"transactionId,omitempty"`
	// ErrorCode is the fault the connector reports, or empty.
	ErrorCode string  `json:"errorCode,omitempty"`
	PowerW    float64 `json:"powerW"`
	MeterWh   float64 `json:"meterWh"`
	// SoC is the EV's state of charge from 0 to 1, or nil with no EV.
	SoC *float64 `json:"soc,omitempty"`
}

ConnectorSnapshot is the state of a connector at one moment.

type ConnectorState

type ConnectorState struct {
	ID      int     `json:"id"`
	MeterWh float64 `json:"meterWh"`
	// EV is the EV plugged in, or nil.
	EV        *ev.State `json:"ev,omitempty"`
	Finishing bool      `json:"finishing,omitempty"`
	// Fault is the fault the connector has, or nil.
	Fault *Fault `json:"fault,omitempty"`
	// Inoperative says the CSMS has made the connector unavailable.
	Inoperative bool `json:"inoperative,omitempty"`
	// Tx is the open transaction, or nil. A charger that starts with one was
	// not shut down in an orderly way.
	Tx *TxState `json:"tx,omitempty"`
}

ConnectorState is the saved state of one connector.

type Entry

type Entry struct {
	Time          time.Time
	ChargePointID string
	Kind          EntryKind
	Text          string
}

Entry is one line of a charger's log.

type EntryKind

type EntryKind int

EntryKind tells the kinds of log entry apart.

const (
	// Event is something that happened to the charger, in words.
	Event EntryKind = iota
	// MessageSent is a raw OCPP message sent to the CSMS.
	MessageSent
	// MessageReceived is a raw OCPP message received from the CSMS.
	MessageReceived
)

type Fault

type Fault struct {
	// Code is one of the OCPP 1.6 error codes, such as GroundFailure.
	Code string `json:"code"`
	// Info is free text about the fault.
	Info string `json:"info,omitempty"`
	// VendorCode is the vendor's own code for the fault.
	VendorCode string `json:"vendorCode,omitempty"`
}

Fault is something wrong with a connector, or with the charger as a whole.

type InstalledProfile

type InstalledProfile struct {
	// ConnectorID is the connector the profile is on, where 0 stands for the
	// charger as a whole.
	ConnectorID int `json:"connectorId"`
	// Installed is when the profile arrived. A schedule that names no start
	// of its own starts then.
	Installed time.Time           `json:"installed"`
	Profile   v16.ChargingProfile `json:"profile"`
}

InstalledProfile is a charging profile the CSMS installed on a connector.

type QueuedMessage

type QueuedMessage struct {
	Action  string          `json:"action"`
	Payload json.RawMessage `json:"payload"`
	// TxRef, when not zero, names the transaction whose ID the message needs
	// and the CSMS has not given yet.
	TxRef int `json:"txRef,omitempty"`
}

QueuedMessage is a message still owed to the CSMS.

type Snapshot

type Snapshot struct {
	ChargePointID string              `json:"chargePointId"`
	Connection    string              `json:"connection"`
	Connectors    []ConnectorSnapshot `json:"connectors"`
	// MessagesSent and MessagesReceived count the OCPP messages since the
	// charger was powered on.
	MessagesSent     int64 `json:"messagesSent"`
	MessagesReceived int64 `json:"messagesReceived"`
}

Snapshot is the state of a charger at one moment.

type State

type State struct {
	// SavedAt is the moment the state was taken. After a power cut it is the
	// last moment the charger is known to have been delivering energy.
	SavedAt time.Time `json:"savedAt"`
	// Config holds the configuration keys the CSMS changed. Keys it did not
	// change follow the model.
	Config     map[string]string `json:"config,omitempty"`
	Connectors []ConnectorState  `json:"connectors"`
	// Outbox holds the messages still owed to the CSMS, in order.
	Outbox    []QueuedMessage `json:"outbox,omitempty"`
	NextTxRef int             `json:"nextTxRef,omitempty"`
	// Fault and Inoperative describe the charger as a whole, which OCPP
	// addresses as connector 0.
	Fault       *Fault `json:"fault,omitempty"`
	Inoperative bool   `json:"inoperative,omitempty"`
	// AuthCache holds what the CSMS said about tags, when the authorization
	// cache is on.
	AuthCache map[string]v16.IDTagInfo `json:"authCache,omitempty"`
	// Profiles holds the charging profiles the CSMS installed.
	Profiles []InstalledProfile `json:"profiles,omitempty"`
	// Firmware is the firmware a firmware update installed. It is empty
	// while the charger runs the firmware of its model.
	Firmware string `json:"firmware,omitempty"`
}

State is what a charger remembers while it is powered off: everything needed to come back as the same charger.

func (*State) Snapshot

func (s *State) Snapshot(chargePointID string, m *model.Model) Snapshot

Snapshot describes a powered-off charger from its saved state: each connector shows the status it will report when the charger starts. A nil state describes a charger that has never run.

type Timing

type Timing struct {
	// CallTimeout is how long to wait for the CSMS to answer a message.
	CallTimeout time.Duration
	// ReconnectMin and ReconnectMax bound the wait between connection
	// attempts, which doubles after each failure.
	ReconnectMin time.Duration
	ReconnectMax time.Duration
	// Tick is how often the EVs on the connectors are advanced.
	Tick time.Duration
	// Reboot is how long the charger is away when it reboots.
	Reboot time.Duration
	// Download and Install are how long a firmware download and a firmware
	// installation take at speed 1.
	Download time.Duration
	Install  time.Duration
	// UploadTimeout is how long one attempt to upload diagnostics may take.
	UploadTimeout time.Duration
}

Timing holds the charger's own timeouts. Zero values select the defaults.

type TxState

type TxState struct {
	// Ref identifies the transaction inside the charger until the CSMS has
	// given it an ID.
	Ref         int             `json:"ref"`
	ID          int             `json:"id,omitempty"`
	IDTag       string          `json:"idTag"`
	ParentIDTag string          `json:"parentIdTag,omitempty"`
	Begin       *v16.MeterValue `json:"begin,omitempty"`
	EVFull      bool            `json:"evFull,omitempty"`
	// StartedAt is when the transaction started.
	StartedAt time.Time `json:"startedAt,omitzero"`
}

TxState is the saved state of an open transaction.

Jump to

Keyboard shortcuts

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