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
- Variables
- func CreateToken(dataDir, name, site string, readOnly bool) (secret string, err error)
- func RemoveToken(dataDir, name string) (bool, error)
- func Shutdown(ctx context.Context, dataDir string) error
- type Action
- type Backend
- type Config
- type Entry
- type Error
- type Info
- type LogLine
- type LogQuery
- type Server
- type Token
- type Wait
- type Waited
Constants ¶
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.
const DefaultListen = "127.0.0.1:7070"
DefaultListen is where the API listens when nothing else is said: on this machine only.
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 ¶
var Actions = []string{ control.ActionPlug, control.ActionUnplug, control.ActionTap, control.ActionFault, control.ActionClear, control.ActionOffline, control.ActionOnline, control.ActionReboot, control.ActionFail, }
Actions lists what the API can do to a charger. They are the actions of "plugyard exec".
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.
var Spec []byte
Spec is the description of the API as an OpenAPI document.
Functions ¶
func CreateToken ¶
CreateToken makes a token and returns its secret, which is not kept.
func RemoveToken ¶
RemoveToken removes the token of that name. It reports whether there was one.
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.
type Error ¶
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.
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.
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 ¶
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) Done ¶
func (s *Server) Done() <-chan struct{}
Done is closed when the API no longer serves, whatever the reason.
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 ¶
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.