Documentation
¶
Overview ¶
Package profile holds the declarative device profiles (spec 4): schema, loader for built-in and local profiles, validation, decoder and status maps. Everything vendor-specific lives in the YAML; this package only interprets it.
Index ¶
- Constants
- Variables
- func Categories() []string
- func ChannelRef(ref string) (block, channel string, ok bool)
- func IsPath(ref string) bool
- func Languages() []string
- func Lookup(doc any, path string) (any, bool)
- func NormalUnit(u string) string
- func Roles() []string
- func ValidPath(s string) bool
- func Validate(p *Profile) []string
- type Addrs
- type Block
- type Channel
- type Endpoint
- type Field
- type Fixture
- type Golden
- type GoldenSample
- type HTTPOptions
- type Interval
- type Loaded
- type Loader
- type Map
- type MapEntry
- type ModbusOptions
- type Needs
- type ProblemsError
- type Profile
- func (p *Profile) Channels(block string, raw Raw) map[string]Value
- func (p *Profile) DescribeStatus(name string, code uint64) StatusValue
- func (p *Profile) Encode(values map[string]float64) (Sample, error)
- func (p *Profile) Endpoints(n Needs, due func(Endpoint) bool) []Endpoint
- func (p *Profile) NeedsFor(blocks []string) Needs
- func (p *Profile) Requests(mode string, n Needs) []Request
- func (p *Profile) Sharers(of string) [][2]string
- func (p *Profile) Statuses(raw Raw) map[string]StatusValue
- type Raw
- type ReadOptions
- type Request
- type Sample
- type SignFrom
- type Status
- type StatusValue
- type Text
- type Value
- type ValueID
Constants ¶
const ( TransportModbus = "modbus" TransportHTTP = "http" )
Transports.
const ( ReadRange = "range" ReadDirect = "direct" )
Read modes (spec 4.3).
const ( RolePV = "pv" RoleGrid = "grid" RoleBattery = "battery" RoleConsumer = "consumer" RoleBackup = "backup" RoleInverter = "inverter" RolePhase = "phase" )
Roles of blocks (spec 2 and the follow-up table of 4.2).
const ( CounterNone = "" CounterLifetime = "lifetime" CounterDailyReset = "daily_reset" )
Counter kinds (spec 5.3).
const ( LevelOK = "ok" LevelNotice = "notice" LevelFault = "fault" )
Status levels.
const DefaultMaxRegisters = 125
DefaultMaxRegisters is the Modbus limit per request unless the profile lowers it.
Variables ¶
var ErrMissing = errors.New("value missing")
ErrMissing marks a value absent from the raw data.
var ErrUnknownProfile = errors.New("unknown profile")
ErrUnknownProfile is returned for a reference that is neither a path nor a built-in ID.
var IDPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*/[a-z0-9][a-z0-9-]*$`)
IDPattern is the pattern of built-in profile IDs (<vendor>/<model>, lower case).
Functions ¶
func ChannelRef ¶
ChannelRef splits "block.channel".
func NormalUnit ¶
energyUnits maps energy units to their factor in Wh. NormalUnit is the unit of a channel after decoding: energy in Wh, power in W.
Types ¶
type Addrs ¶
type Addrs []int
Addrs is one register address or a list of them (status bitfields over several words).
type Block ¶
type Block struct {
Role string `yaml:"role"`
RolesAllowed []string `yaml:"roles_allowed"`
Category string `yaml:"category"`
Channels map[string]Channel `yaml:"channels"`
}
Block is one meter the device can provide.
type Channel ¶
type Channel struct {
Field `yaml:",inline"`
Sum []string `yaml:"sum"`
Product []string `yaml:"product"`
ValueID *ValueID `yaml:"value_id"`
Derive string `yaml:"derive"`
Of string `yaml:"of"`
By string `yaml:"by"`
Scale *float64 `yaml:"scale"`
Offset float64 `yaml:"offset"`
Invert bool `yaml:"invert"`
SignFrom *SignFrom `yaml:"sign_from"`
Unit string `yaml:"unit"`
Counter string `yaml:"counter"`
ResetGroup string `yaml:"reset_group"`
Resolution float64 `yaml:"resolution"`
History *bool `yaml:"history"`
Values map[int]string `yaml:"values"` // type enum: code → state name
RenamedFrom string `yaml:"renamed_from"`
}
Channel is one normalised quantity of a block.
func (Channel) Historised ¶
Historised reports whether the channel is stored by default (spec 7.2): power, soc and energy channels unless history: false; others only with history: true.
type Endpoint ¶
type Endpoint struct {
ID string `yaml:"id"`
URL string `yaml:"url"`
Interval Interval `yaml:"interval"`
}
Endpoint is one GET URL; Interval > 0 polls it less often than the device.
type Field ¶
type Field struct {
Endpoint string `yaml:"endpoint"`
Path string `yaml:"path"`
Addr Addrs `yaml:"addr"`
Type string `yaml:"type"`
Length int `yaml:"length"` // registers of a string
}
Field locates one raw value: a Modbus address or a JSON path, plus its type.
type Fixture ¶
type Fixture struct {
Profile string `json:"profile"`
Version int `json:"version"` // profile version the fixture was made with
Source string `json:"source"` // dump | documentation
Interval string `json:"interval,omitempty"` // poll interval of a dump
Samples []Sample `json:"samples"`
Created time.Time `json:"created,omitzero"`
}
Fixture is a recorded or documentation-based sequence of raw polls of one device. It is the output format of "pvmon dump" and the input of golden and replay tests.
type Golden ¶
type Golden struct {
Samples []GoldenSample `json:"samples"`
}
Golden holds the expected normalised values per sample of a fixture: channels as "block.channel" → value (numbers, or strings for enum/string channels) and status as name → level.
type GoldenSample ¶
type GoldenSample struct {
Channels map[string]any `json:"channels"`
Status map[string]string `json:"status"`
}
GoldenSample is the expectation for one sample.
type HTTPOptions ¶
type HTTPOptions struct {
Auth string `yaml:"auth"` // "" | basic | digest
Endpoints []Endpoint `yaml:"endpoints"`
}
HTTPOptions lists the endpoints of an HTTP device.
type Loaded ¶
type Loaded struct {
*Profile
Path string
Overrides bool // a local file replacing the built-in profile with the same ID
}
Loaded is a profile resolved for a device. Path is empty for built-in profiles.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader resolves profile references: built-in IDs from the embedded set, or local files by path (spec 4.5). It holds no cache of local files; they are read on every Load.
type ModbusOptions ¶
type ModbusOptions struct {
Function string `yaml:"function"` // input (04) | holding (03)
ByteOrder string `yaml:"byte_order"` // big | little
WordOrder string `yaml:"word_order"` // big | little
}
ModbusOptions selects function code and byte/word order.
type Needs ¶
Needs is the set of channels one poll of a device has to decode: block → channel names. A nil Needs means everything the profile describes (dumps, fixtures).
type ProblemsError ¶
ProblemsError lists every problem of a profile, each prefixed with its path.
func (*ProblemsError) Error ¶
func (e *ProblemsError) Error() string
type Profile ¶
type Profile struct {
Schema int `yaml:"schema"`
ID string `yaml:"profile"`
Version int `yaml:"version"`
Vendor string `yaml:"vendor"`
Models []string `yaml:"models"`
Transport string `yaml:"transport"`
Verified bool `yaml:"verified"`
Modbus *ModbusOptions `yaml:"modbus"`
Read *ReadOptions `yaml:"read"`
HTTP *HTTPOptions `yaml:"http"`
MaxPower float64 `yaml:"max_power"`
Netting string `yaml:"netting"`
Blocks map[string]Block `yaml:"blocks"`
Status map[string]Status `yaml:"status"`
Maps map[string]Map `yaml:"maps"`
}
Profile is one parsed device profile (spec 4.2).
func (*Profile) Channels ¶
Channels decodes and normalises every channel of a block. Derive (share) channels are left out: they are computed from energy deltas by the collector.
func (*Profile) DescribeStatus ¶
func (p *Profile) DescribeStatus(name string, code uint64) StatusValue
DescribeStatus evaluates a stored code of the status entry name (level and texts); an unknown name is invalid.
func (*Profile) Encode ¶
Encode builds raw poll data from normalised channel values keyed "block.channel"; it is the inverse of the decoder for simulations and tests. Only channels with a value are written (blocks may share registers, e.g. one meter seen as grid or as pv); registers of the profile without a value stay 0. Composite channels (product, derive) are skipped, a sum is written to its first path and value_id channels get matching ID and value arrays.
func (*Profile) Endpoints ¶
Endpoints returns the HTTP endpoints a poll with the given needs (nil: everything) reads; slow endpoints (Interval > 0) are included only when due reports true for them.
func (*Profile) NeedsFor ¶
NeedsFor returns the channels the given blocks need: all their channels, the inputs of product channels and, for a share channel, the shared counter and the weight channel of every block sharing it (a split is only right with all weights).
func (*Profile) Requests ¶
Requests returns the Modbus reads of one poll for the given read mode and needs. Range mode reads, within each profile range, only the spans holding needed registers, joining spans less than mergeGap apart; direct mode reads each needed field on its own (spec 4.3). Nil needs read everything the profile describes (whole ranges), as for dumps.
type ReadOptions ¶
type ReadOptions struct {
Mode string `yaml:"mode"`
MaxRegisters int `yaml:"max_registers"`
Ranges [][2]int `yaml:"ranges"`
}
ReadOptions describes how registers are fetched.
func (*ReadOptions) Limit ¶
func (r *ReadOptions) Limit() int
Limit returns the effective max registers per request.
type Sample ¶
type Sample struct {
At time.Time `json:"at"`
Words map[int]uint16 `json:"words,omitempty"`
JSON map[string]json.RawMessage `json:"json,omitempty"`
Err string `json:"err,omitempty"`
}
Sample is one poll: Modbus words by address or JSON bodies by endpoint ID. Err is set for a failed poll so replays see gaps.
type Status ¶
type Status struct {
Field `yaml:",inline"`
Map string `yaml:"map"`
Level string `yaml:"level"` // bitfield without map: any set bit has this level
NonEmpty string `yaml:"nonempty"` // type list: non-empty → level
False string `yaml:"false"` // type bool: false → level
NonZero string `yaml:"nonzero"` // any number: non-zero → level
Text Text `yaml:"text"`
Label Text `yaml:"label"` // display name on the device page; default the key
}
Status is one status register or JSON value mapped to ok / notice / fault.
type StatusValue ¶
type StatusValue struct {
Code uint64
Level string
Texts []Text // texts of the code or of every set bit
Valid bool
}
StatusValue is the evaluated state of one status entry.