diff --git a/charger/zaptec.go b/charger/zaptec.go index a7ca69516..43fad7506 100644 --- a/charger/zaptec.go +++ b/charger/zaptec.go @@ -55,6 +55,9 @@ type Zaptec struct { priority bool passive bool lastStatus int + + session string // last seen SessionIdentifier + sessionStart time.Time // start of the current session } func init() { @@ -325,6 +328,33 @@ func (c *Zaptec) ChargedEnergy() (float64, error) { return res.ObservationByID(zaptec.TotalChargePowerSession).Float64() } +var _ api.ConnectionTimer = (*Zaptec)(nil) + +// ConnectionDuration implements the api.ConnectionTimer interface. +// Derived from SessionIdentifier: a session change drops the duration, so the loadpoint detects a cable swap as reconnect. +func (c *Zaptec) ConnectionDuration() (time.Duration, error) { + res, err := c.statusG.Get() + if err != nil { + return 0, err + } + + var session string + if o := res.ObservationByID(zaptec.SessionIdentifier); o != nil { + session = o.ValueAsString + } + + if session != c.session { + c.session = session + c.sessionStart = time.Now() + } + + if session == "" { + return 0, nil + } + + return time.Since(c.sessionStart), nil +} + var _ api.PhaseCurrents = (*Zaptec)(nil) // Currents implements the api.PhaseCurrents interface