api

package
v0.3.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: 29 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 api serves plugyard's HTTP API: the charger actions of the command line, and reading chargers, for callers that have no shell on the machine (see docs/adr/0006).

What the API offers is kept narrow on purpose. It acts on chargers and reads them. It never takes a file path, starts or removes a site or a charger, changes where a charger connects to, updates or removes plugyard, or manages its own tokens: those stay on the command line.

Index

Constants

View Source
const (
	DefaultWaitTimeout = 30 * time.Second
	MaxWaitTimeout     = 5 * time.Minute
	DefaultLogTail     = 200
	MaxLogTail         = 5000
)

The limits of a wait and of reading the log, so that one call neither holds a connection for ever nor answers with a whole log.

View Source
const DefaultListen = "127.0.0.1:7070"

DefaultListen is where the API listens when nothing else is said: on this machine only.

View Source
const MCPPath = "/mcp"

MCPPath is where the API serves the Model Context Protocol, for AI agents: the same reading and the same actions as the calls under /v1, as tools.

Variables

Actions lists what the API can do to a charger. They are the actions of "plugyard exec".

View Source
var ErrAlreadyRunning = errors.New("the API is already running")

ErrAlreadyRunning is returned by Serve when an API already runs on this machine for this user.

View Source
var Spec []byte

Spec is the description of the API as an OpenAPI document.

Functions

func CreateToken

func CreateToken(dataDir, name, site string, readOnly bool) (secret string, err error)

CreateToken makes a token and returns its secret, which is not kept.

func RemoveToken

func RemoveToken(dataDir, name string) (bool, error)

RemoveToken removes the token of that name. It reports whether there was one.

func Shutdown

func Shutdown(ctx context.Context, dataDir string) error

Shutdown asks the API running for this user to stop, and waits until it has.

Types

type Action

type Action struct {
	// Name is the action, taken from the address of the request.
	Name string `json:"-"`
	// Connector is the connector the action is about. Nil means the
	// default: connector 1.
	Connector *int `json:"connector,omitempty"`

	Tag        string `json:"tag,omitempty"`        // tap
	Code       string `json:"code,omitempty"`       // fault
	Info       string `json:"info,omitempty"`       // fault
	VendorCode string `json:"vendorCode,omitempty"` // fault
	For        string `json:"for,omitempty"`        // offline
	Step       string `json:"step,omitempty"`       // fail

	EV        string `json:"ev,omitempty"` // plug: an EV preset of the site
	Battery   string `json:"battery,omitempty"`
	SoC       string `json:"soc,omitempty"`
	MaxPower  string `json:"maxPower,omitempty"`
	TargetSoC string `json:"targetSoc,omitempty"`
	Curve     string `json:"curve,omitempty"`
}

Action is an action on chargers with what goes with it, as the body of a request carries it. Which fields apply depends on the action.

type Backend

type Backend interface {
	// Chargers lists the chargers of a site, or of every site when site
	// is empty.
	Chargers(ctx context.Context, site string) ([]control.ChargerState, error)
	// Inspect returns everything about one charger, as "plugyard inspect
	// --format json" prints it.
	Inspect(ctx context.Context, site, charger string) (any, error)
	// Exec runs an action on the chargers a charger argument names.
	Exec(ctx context.Context, site, charger string, action Action) ([]control.ExecResult, error)
	// Wait waits until the chargers a charger argument names are in a
	// state, and says where each was when that was so or the time was
	// up.
	Wait(ctx context.Context, site, charger string, wait Wait) ([]Waited, error)
	// Logs returns lines of the log of the chargers a charger argument
	// names, oldest first.
	Logs(ctx context.Context, site, charger string, query LogQuery) ([]LogLine, error)
}

Backend is what the API needs done. The command line implements it, so that a call to the API does what the command of the same name does.

type Config

type Config struct {
	// DataDir is plugyard's data directory, where the tokens, the log of
	// calls and the socket of the API are kept.
	DataDir string
	// Listen is the address to listen on, such as 127.0.0.1:7070.
	Listen string
	// TLSCert and TLSKey, when both are set, are the files of a
	// certificate and its key, and the API is served over HTTPS.
	TLSCert, TLSKey string
	Backend         Backend
	// Version is the version of plugyard, shown by Info.
	Version string
	// Out, if set, is shown every call as it comes in.
	Out io.Writer
}

Config says how to serve the API.

type Entry

type Entry struct {
	Time time.Time `json:"time"`
	// Token is the name of the token the call came with, "local" for a
	// call that needed none, and "-" for one that was turned away before
	// its token was known.
	Token string `json:"token"`
	// Remote is the address the call came from. Behind a reverse proxy
	// that is the proxy.
	Remote string `json:"remote"`
	Method string `json:"method"`
	Path   string `json:"path"`
	// Status is the HTTP status the call was answered with.
	Status int `json:"status"`
	// Action is the action asked for, or "list" and "inspect" for reading.
	Action string `json:"action,omitempty"`
	// Chargers are the charge point IDs the call was about.
	Chargers []string `json:"chargers,omitempty"`
	// Message says how the call ended when that needs words.
	Message string `json:"message,omitempty"`
}

Entry is the record of one call to the API: who did what to which charger, and how it ended. A token itself is never part of it.

func ReadLog

func ReadLog(dataDir string) ([]Entry, error)

ReadLog returns the calls recorded in the data directory, oldest first.

func (Entry) Line

func (e Entry) Line() string

Line returns the entry as one line of text.

type Error

type Error struct {
	Status  int
	Message string
}

Error is a failure the caller is told about with an HTTP status of its own. Any other error of a Backend is answered with 500.

func Errorf

func Errorf(status int, format string, args ...any) *Error

Errorf makes an Error.

func (*Error) Error

func (e *Error) Error() string

type Info

type Info struct {
	PID    int    `json:"pid"`
	Listen string `json:"listen"`
	TLS    bool   `json:"tls"`
	// TLSCert and TLSKey are the files HTTPS is served with, as full
	// paths. They are told over the socket of this machine only.
	TLSCert string    `json:"tlsCert,omitempty"`
	TLSKey  string    `json:"tlsKey,omitempty"`
	Since   time.Time `json:"since"`
	Version string    `json:"version"`
}

Info says what a running API is.

func Probe

func Probe(ctx context.Context, dataDir string) (Info, error)

Probe asks the API running for this user what it is. It fails when none is running.

func (Info) URL

func (i Info) URL() string

URL returns the address of the API as a caller writes it.

type LogLine

type LogLine struct {
	Time          string `json:"time"`
	ChargePointID string `json:"chargePointId"`
	// Kind is "event", "sent" or "received".
	Kind string `json:"kind"`
	Text string `json:"text"`
	// Message is the OCPP message of a line that is one.
	Message json.RawMessage `json:"message,omitempty"`
}

LogLine is one line of the log of a charger, as "plugyard logs --format json" prints it.

type LogQuery

type LogQuery struct {
	// Since is how long ago, such as 10m, or a time. Empty means from
	// the start of the log.
	Since string
	// Tail is how many lines to answer with at most: the newest.
	Tail int
	// Events leaves the OCPP messages out.
	Events bool
}

LogQuery says which lines of the log to read, as the flags of "plugyard logs" do.

type Server

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

Server is a running API.

func Serve

func Serve(cfg Config) (*Server, error)

Serve starts the API. It refuses to listen beyond this machine while no token exists, since anyone who can reach it could then act on chargers.

func (*Server) Close

func (s *Server) Close()

Close stops the API.

func (*Server) Done

func (s *Server) Done() <-chan struct{}

Done is closed when the API no longer serves, whatever the reason.

func (*Server) Info

func (s *Server) Info() Info

Info says what this API is.

func (*Server) Local

func (s *Server) Local() bool

Local reports whether the API can be reached from this machine only.

func (*Server) Stopped

func (s *Server) Stopped() <-chan struct{}

Stopped is closed when "plugyard api down" has asked the API to stop.

type Token

type Token struct {
	Name string `json:"name"`
	// Hash is the SHA-256 of the secret, in hex.
	Hash string `json:"hash"`
	// Site limits the token to the chargers of one site. Empty means
	// every site.
	Site string `json:"site,omitempty"`
	// ReadOnly makes a token that can look and not act.
	ReadOnly bool      `json:"readOnly,omitempty"`
	Created  time.Time `json:"created"`
}

Token is the right to call the API, given to a team or a tool by name. The secret itself is shown once, when the token is made; what is kept is its hash.

func LoadTokens

func LoadTokens(dataDir string) ([]Token, error)

LoadTokens returns the tokens kept in the data directory, sorted by name.

type Wait

type Wait struct {
	// State is a connector status or a connection state, in any case.
	State string
	// Connector is the connector a status is that of. Nil means the
	// default: connector 1.
	Connector *int
	// Timeout is how long to wait at most.
	Timeout time.Duration
}

Wait says what to wait for: a state of chargers, as "plugyard wait" takes it.

type Waited

type Waited struct {
	Site          string `json:"site"`
	ChargePointID string `json:"chargePointId"`
	// Connector is the connector the state is that of. It is absent for
	// a state of the whole charger.
	Connector int `json:"connector,omitempty"`
	// State is the state the charger or its connector was in.
	State string `json:"state"`
	// Reached says whether that is the state waited for.
	Reached bool `json:"reached"`
}

Waited says where one charger was when a wait ended.

Jump to

Keyboard shortcuts

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