141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package core
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/evcc-io/evcc/api"
|
|
"github.com/evcc-io/evcc/core/keys"
|
|
"github.com/evcc-io/evcc/core/loadpoint"
|
|
)
|
|
|
|
func batteryModeModified(mode api.BatteryMode) bool {
|
|
return mode != api.BatteryUnknown && mode != api.BatteryNormal
|
|
}
|
|
|
|
func (site *Site) batteryConfigured() bool {
|
|
return len(site.batteryMeters) > 0
|
|
}
|
|
|
|
// setBatteryMode sets the battery mode
|
|
func (site *Site) setBatteryMode(batMode api.BatteryMode) {
|
|
site.batteryMode = batMode
|
|
site.publish(keys.BatteryMode, batMode)
|
|
}
|
|
|
|
// SetBatteryMode sets the battery mode
|
|
func (site *Site) SetBatteryMode(batMode api.BatteryMode) {
|
|
site.Lock()
|
|
defer site.Unlock()
|
|
|
|
site.log.DEBUG.Println("set battery mode:", batMode)
|
|
|
|
if site.batteryMode != batMode {
|
|
site.setBatteryMode(batMode)
|
|
}
|
|
|
|
if site.batteryModeExternal == api.BatteryUnknown {
|
|
site.batteryModeExternalTimer = time.Time{}
|
|
}
|
|
}
|
|
|
|
func (site *Site) updateBatteryMode(batteryGridChargeActive bool, rate api.Rate) {
|
|
if batteryMode := site.requiredBatteryMode(batteryGridChargeActive, rate); batteryMode != api.BatteryUnknown {
|
|
if err := site.applyBatteryMode(batteryMode); err == nil {
|
|
site.SetBatteryMode(batteryMode)
|
|
} else {
|
|
site.log.ERROR.Println("battery mode:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// requiredBatteryMode determines required battery mode based on grid charge and rate
|
|
func (site *Site) requiredBatteryMode(batteryGridChargeActive bool, rate api.Rate) api.BatteryMode {
|
|
var res api.BatteryMode
|
|
batMode := site.GetBatteryMode()
|
|
extMode := site.GetBatteryModeExternal()
|
|
|
|
var extModeReset bool
|
|
if extMode == api.BatteryUnknown {
|
|
site.Lock()
|
|
extModeReset = !site.batteryModeExternalTimer.IsZero()
|
|
site.Unlock()
|
|
}
|
|
|
|
mapper := func(s api.BatteryMode) api.BatteryMode {
|
|
return map[bool]api.BatteryMode{false: s, true: api.BatteryUnknown}[batMode == s]
|
|
}
|
|
|
|
switch {
|
|
case !site.batteryConfigured():
|
|
res = api.BatteryUnknown
|
|
case extModeReset:
|
|
// require normal mode to leave external control
|
|
res = api.BatteryNormal
|
|
case extMode != api.BatteryUnknown:
|
|
// require external mode only once
|
|
if extMode != batMode {
|
|
res = extMode
|
|
}
|
|
case batteryGridChargeActive:
|
|
res = mapper(api.BatteryCharge)
|
|
case site.dischargeControlActive(rate):
|
|
res = mapper(api.BatteryHold)
|
|
case batteryModeModified(batMode):
|
|
res = api.BatteryNormal
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// applyBatteryMode applies the mode to each battery
|
|
func (site *Site) applyBatteryMode(mode api.BatteryMode) error {
|
|
for _, dev := range site.batteryMeters {
|
|
meter := dev.Instance()
|
|
if _, ok := meter.(api.Meter); !ok {
|
|
panic("not a meter: battery")
|
|
}
|
|
|
|
if batCtrl, ok := meter.(api.BatteryController); ok {
|
|
if err := batCtrl.SetBatteryMode(mode); err != nil && !errors.Is(err, api.ErrNotAvailable) {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (site *Site) tariffRates(usage api.TariffUsage) (api.Rates, error) {
|
|
tariff := site.GetTariff(usage)
|
|
if tariff == nil || tariff.Type() == api.TariffTypePriceStatic {
|
|
return nil, nil
|
|
}
|
|
|
|
return tariff.Rates()
|
|
}
|
|
|
|
func (site *Site) smartCostActive(lp loadpoint.API, rate api.Rate) bool {
|
|
limit := lp.GetSmartCostLimit()
|
|
return limit != nil && !rate.IsZero() && rate.Value <= *limit
|
|
}
|
|
|
|
func (site *Site) batteryGridChargeActive(rate api.Rate) bool {
|
|
limit := site.GetBatteryGridChargeLimit()
|
|
return limit != nil && !rate.IsZero() && rate.Value <= *limit
|
|
}
|
|
|
|
func (site *Site) dischargeControlActive(rate api.Rate) bool {
|
|
if !site.GetBatteryDischargeControl() {
|
|
return false
|
|
}
|
|
|
|
for _, lp := range site.Loadpoints() {
|
|
smartCostActive := site.smartCostActive(lp, rate)
|
|
if lp.GetStatus() == api.StatusC && (smartCostActive || lp.IsFastChargingActive()) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|