Documentation
¶
Overview ¶
Package collector polls the devices of one connection, normalises their values, turns counters into energy deltas, nets grid energy and books everything into 15-minute buckets (spec 5, 6). One collector instance serves one connection.
Index ¶
- Constants
- Variables
- func Slot(t time.Time) int64
- func SlotStart(slot int64) time.Time
- type Accumulator
- func (a *Accumulator) AddEnergy(series int64, e Energy)
- func (a *Accumulator) AddGauge(series int64, v float64, at time.Time, weight time.Duration)
- func (a *Accumulator) Batch(todayStart int64) storage.Batch
- func (a *Accumulator) Empty() bool
- func (a *Accumulator) Restore(b storage.Batch)
- func (a *Accumulator) SetCounter(st storage.CounterState)
- type Collector
- type CounterConfig
- type Daily
- type Delta
- type Deps
- type Energy
- type Fetcher
- type Group
- type HTTPReader
- type Integrator
- type Lifetime
- type LiveCache
- type ModbusReader
- type Publisher
- type Reading
- type Settings
- type Source
- type Transport
Constants ¶
const ( EventReplaced = "replaced" // clear decrease: meter replaced, new base EventJump = "jump" // implausible jump confirmed by the next reading: new base EventOverflow = "overflow" // u32 wrap-around )
Counter events.
const ( // ResetConfirm is how long a drop must persist before it is booked as a reset. ResetConfirm = 15 * time.Minute // ResetOverdue is how long after the last reset a missing reset is reported. ResetOverdue = 25 * time.Hour )
Reset detection (spec 5.3). A reset is a data event, never triggered by time: device clocks drift and often lack NTP and DST. A drop counts once it persisted for ResetConfirm; the counter itself buffers the energy meanwhile, so nothing is lost by waiting.
const ( EventResetPartial = "reset of only part of the reset group (register stuck?)" EventResetMissing = "no reset for more than 25 h" )
Reset events, logged once per occurrence.
const FlushInterval = time.Minute
FlushInterval is the upsert interval of open buckets (spec 5.1).
const GapAfter = 5 * time.Minute
GapAfter: a poll more than the interval plus this after the last successful one is a gap poll (app not running, clock jump > 5 min; spec 5.2, 5.3).
Variables ¶
var ErrNotJSON = errors.New("collector: response is not JSON")
ErrNotJSON is returned for a response body that is not valid JSON.
Functions ¶
Types ¶
type Accumulator ¶
type Accumulator struct {
// contains filtered or unexported fields
}
Accumulator collects the open buckets of one collector between flushes. Gauges are partial aggregates, energy is the Wh added since the last flush; storage merges both. It is owned by the collector goroutine.
func NewAccumulator ¶
func NewAccumulator() *Accumulator
NewAccumulator returns an empty accumulator.
func (*Accumulator) AddEnergy ¶
func (a *Accumulator) AddEnergy(series int64, e Energy)
AddEnergy books e proportionally to the time each slot overlaps (From, To]; From == To books everything into the slot of To.
func (*Accumulator) AddGauge ¶
AddGauge adds a sample valid for weight (the device interval) to the slot of at.
func (*Accumulator) Batch ¶
func (a *Accumulator) Batch(todayStart int64) storage.Batch
Batch drains everything pending into one storage batch, sorted for stable writes.
func (*Accumulator) Empty ¶
func (a *Accumulator) Empty() bool
Empty reports whether nothing is pending.
func (*Accumulator) Restore ¶
func (a *Accumulator) Restore(b storage.Batch)
Restore puts a batch that failed to write back, so no energy is lost; newer pending values are kept on top of it.
func (*Accumulator) SetCounter ¶
func (a *Accumulator) SetCounter(st storage.CounterState)
SetCounter records the counter state to persist with the next flush.
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector polls the devices of one connection (health.Component).
type CounterConfig ¶
type CounterConfig struct {
Resolution float64 // profile resolution in Wh (0 = none)
Step float64 // Wh of one raw count of an integer register (0 = none)
MaxPower float64 // W; 0 disables the glitch guard
Wrap float64 // Wh value at which the raw counter wraps (u32); 0 = never
Float32 bool // raw value is an f32 in Unit; precision limits the resolution
UnitFactor float64 // Wh per raw unit (kWh: 1000)
}
CounterConfig describes one counter input.
func (CounterConfig) EffectiveResolution ¶
func (c CounterConfig) EffectiveResolution(v float64) float64
EffectiveResolution = max(profile resolution, type precision at v) (spec 5.3).
type Daily ¶
type Daily struct {
// contains filtered or unexported fields
}
Daily is one daily_reset counter of a reset group.
type Delta ¶
type Delta struct {
Wh float64
Gap bool // the poll followed a gap; spread over the gap's buckets
ResetSeen bool // a daily reset was booked
Held bool // the reading was held (glitch guard or waiting for the reset group)
Event string // one-time events for the log: replaced, jump, overflow
From time.Time // a confirmed reset: book from the poll before the drop
}
Delta is the energy a counter or integrator books for one poll.
type Deps ¶
type Deps struct {
Store storage.CollectorStore
Cache LiveCache
Bus Publisher
Reporter health.Reporter
Transport Transport
Clock util.Clock
Log zerolog.Logger
}
Deps are the collector's dependencies.
type Energy ¶
Energy is energy produced in (From, To] with its bucket flags and the raw counter at To.
type Fetcher ¶
type Fetcher struct {
// contains filtered or unexported fields
}
Fetcher reads the raw data of one device per poll: the Modbus requests or due HTTP endpoints its configured meters need, nothing else (keeps the bus load low). Slow endpoints (interval > 0) are cached between fetches.
func NewHTTPFetcher ¶
NewHTTPFetcher builds a fetcher for an HTTP device.
func NewModbusFetcher ¶
NewModbusFetcher builds a fetcher for a Modbus device.
func (*Fetcher) Fetch ¶
Fetch performs one poll. The poll instant is captured once at its start. Any failed request fails the whole poll, so the counters see a gap instead of partial data.
func (*Fetcher) FetchAll ¶
FetchAll reads everything the profile describes, every HTTP endpoint regardless of its interval (dumps and fixtures).
func (*Fetcher) Invalidate ¶
func (f *Fetcher) Invalidate()
Invalidate drops cached slow endpoints, e.g. after an ID/value length mismatch.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a reset group: all its counters reset together once per device day.
func NewGroup ¶
func NewGroup(members []*Daily, rollover time.Duration, loc *time.Location, last, lastReset time.Time, ) *Group
NewGroup builds a group. last is the last poll and lastReset the last confirmed reset as persisted (zero if unknown); rollover is the configured local reset time.
func (*Group) PeriodStart ¶
PeriodStart returns the start of the device day containing at: the last confirmed reset, or before the first one the configured rollover. Energy shown by a daily register belongs to the period since then and is never booked before it.
type HTTPReader ¶
HTTPReader fetches a device path (transport/http.Client).
type Integrator ¶
type Integrator struct {
// contains filtered or unexported fields
}
Integrator integrates power between two consecutive successful polls (trapezoid).
func (*Integrator) Reset ¶
func (i *Integrator) Reset()
Reset forgets the previous poll (after a failed poll).
type Lifetime ¶
type Lifetime struct {
// contains filtered or unexported fields
}
Lifetime tracks a monotonic counter (spec 5.3).
func NewLifetime ¶
func NewLifetime(cfg CounterConfig, base *float64, at time.Time) *Lifetime
NewLifetime returns a tracker; base restores the persisted raw value (nil = none).
type ModbusReader ¶
type ModbusReader interface {
Read(ctx context.Context, unit uint8, fn modbus.Function, start, count uint16) ([]uint16, error)
}
ModbusReader reads registers over the shared connection (transport/modbus.Conn).
type Settings ¶
type Settings struct {
Site *site.Site
Connection string // connection address key
Sources map[string]Source // per device ID
Location *time.Location // site time zone (TZ)
}
Settings selects the connection and its devices.