From 46a6dd22d7c757998b07bda06a367b3698d1f329 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 29 Dec 2025 19:25:20 +0100 Subject: [PATCH] EEBus meter: add energy guard limitation of power production (#26247) --- meter/eebus.go | 128 +++++++++++++++++++++++++++++++++++------- server/eebus/eebus.go | 5 +- 2 files changed, 113 insertions(+), 20 deletions(-) diff --git a/meter/eebus.go b/meter/eebus.go index 3e965b0e0..b9ede09f6 100644 --- a/meter/eebus.go +++ b/meter/eebus.go @@ -3,13 +3,16 @@ package meter import ( "context" "errors" + "fmt" "slices" + "strings" "sync" "time" eebusapi "github.com/enbility/eebus-go/api" ucapi "github.com/enbility/eebus-go/usecases/api" "github.com/enbility/eebus-go/usecases/eg/lpc" + "github.com/enbility/eebus-go/usecases/eg/lpp" "github.com/enbility/eebus-go/usecases/ma/mgcp" "github.com/enbility/eebus-go/usecases/ma/mpc" spineapi "github.com/enbility/spine-go/api" @@ -20,10 +23,10 @@ import ( "github.com/evcc-io/evcc/util/templates" ) -// EEBus is an EEBus meter implementation supporting MGCP, MPC, and LPC use cases +// EEBus is an EEBus meter implementation supporting MGCP, MPC, LPC and LPP use cases // Uses MGCP (Monitoring of Grid Connection Point) only when usage="grid" // Uses MPC (Monitoring & Power Consumption) for all other cases (default) -// Additionally supports LPC (Limitation of Power Consumption) +// Additionally supports LPC (Limitation of Power Consumption) and LPP (Limitation of Power Production) type EEBus struct { log *util.Logger @@ -37,12 +40,11 @@ type EEBus struct { currents *util.Value[[]float64] voltages *util.Value[[]float64] - // TODO use util.Value mu sync.Mutex - consumptionLimit *ucapi.LoadLimit + consumptionLimit ucapi.LoadLimit + productionLimit ucapi.LoadLimit egLpcEntity spineapi.EntityRemoteInterface - // failsafeLimit float64 - // failsafeDuration time.Duration + egLppEntity spineapi.EntityRemoteInterface } type measurements interface { @@ -125,6 +127,9 @@ func NewEEBus(ctx context.Context, ski, ip string, usage *templates.Usage, timeo for _, s := range c.eg.EgLPCInterface.RemoteEntitiesScenarios() { c.log.DEBUG.Printf("ski %s EG LPC scenarios: %v", s.Entity.Device().Ski(), s.Scenarios) } + for _, s := range c.eg.EgLPPInterface.RemoteEntitiesScenarios() { + c.log.DEBUG.Printf("ski %s EG LPP scenarios: %v", s.Entity.Device().Ski(), s.Scenarios) + } return c, nil } @@ -144,11 +149,17 @@ func (c *EEBus) UseCaseEvent(_ spineapi.DeviceRemoteInterface, entity spineapi.E case mpc.DataUpdateVoltagePerPhase, mgcp.DataUpdateVoltagePerPhase: c.maDataUpdateVoltagePerPhase(entity) - // Energy Guard + // Energy Guard - LPC case lpc.UseCaseSupportUpdate: c.egLpcUseCaseSupportUpdate(entity) case lpc.DataUpdateLimit: c.egLpcDataUpdateLimit(entity) + + // Energy Guard - LPP + case lpp.UseCaseSupportUpdate: + c.egLppUseCaseSupportUpdate(entity) + case lpp.DataUpdateLimit: + c.egLppDataUpdateLimit(entity) } } @@ -235,7 +246,7 @@ func (c *EEBus) Voltages() (float64, float64, float64, error) { } // -// Energy Guard +// Energy Guard - LPC // func (c *EEBus) egLpcUseCaseSupportUpdate(entity spineapi.EntityRemoteInterface) { @@ -255,7 +266,7 @@ func (c *EEBus) egLpcDataUpdateLimit(entity spineapi.EntityRemoteInterface) { c.mu.Lock() defer c.mu.Unlock() - c.consumptionLimit = &limit + c.consumptionLimit = limit } var _ api.Dimmer = (*EEBus)(nil) @@ -266,7 +277,7 @@ func (c *EEBus) Dimmed() (bool, error) { defer c.mu.Unlock() // Check if limit is active and has a valid power value - return c.consumptionLimit != nil && c.consumptionLimit.IsActive && c.consumptionLimit.Value > 0, nil + return c.consumptionLimit.IsActive && c.consumptionLimit.Value > 0, nil } // Dim implements the api.Dimmer interface @@ -290,20 +301,99 @@ func (c *EEBus) Dim(dim bool) error { } if !slices.Contains(c.eg.EgLPCInterface.AvailableScenariosForEntity(c.egLpcEntity), 1) { - return errors.New("scenario 1 not supported") + return errors.New("eg lpc: scenario 1 not supported") } _, err := c.eg.EgLPCInterface.WriteConsumptionLimit(c.egLpcEntity, ucapi.LoadLimit{ Value: value, IsActive: dim, - }, func(result model.ResultDataType) { - if result.ErrorNumber != nil { - c.log.ERROR.Println("ErrorNumber", *result.ErrorNumber) - } - if result.Description != nil { - c.log.ERROR.Println("Description", *result.Description) - } - }) + }, c.callbackResult("consumption limit")) return err } + +// +// Energy Guard - LPP +// + +func (c *EEBus) egLppUseCaseSupportUpdate(entity spineapi.EntityRemoteInterface) { + c.mu.Lock() + defer c.mu.Unlock() + + c.egLppEntity = entity +} + +func (c *EEBus) egLppDataUpdateLimit(entity spineapi.EntityRemoteInterface) { + limit, err := c.eg.EgLPPInterface.ProductionLimit(entity) + if err != nil { + c.log.ERROR.Println("EG LPP ProductionLimit:", err) + return + } + + c.mu.Lock() + defer c.mu.Unlock() + + c.productionLimit = limit +} + +var _ api.Curtailer = (*EEBus)(nil) + +// Curtailed implements the api.Curtailer interface +func (c *EEBus) Curtailed() (bool, error) { + c.mu.Lock() + defer c.mu.Unlock() + + // Check if limit is active and has a valid power value + return c.productionLimit.IsActive && c.productionLimit.Value > 0, nil +} + +// Curtail implements the api.Curtailer interface +func (c *EEBus) Curtail(curtail bool) error { + // Sets or removes the production power limit + + // TODO: change api.Curtailer to make limit configurable + // For now, we use a fixed safe limit of 0W + limit := 0.0 + + var value float64 + if curtail { + value = limit + } + + c.mu.Lock() + defer c.mu.Unlock() + + if c.egLppEntity == nil { + return api.ErrNotAvailable + } + + if !slices.Contains(c.eg.EgLPPInterface.AvailableScenariosForEntity(c.egLppEntity), 1) { + return errors.New("eg lpp: scenario 1 not supported") + } + + _, err := c.eg.EgLPPInterface.WriteProductionLimit(c.egLppEntity, ucapi.LoadLimit{ + Value: value, + IsActive: curtail, + }, c.callbackResult("production limit")) + + return err +} + +func (c *EEBus) callbackResult(typ string) func(result model.ResultDataType) { + return func(result model.ResultDataType) { + sb := new(strings.Builder) + + if result.ErrorNumber != nil { + fmt.Fprint(sb, *result.ErrorNumber) + } + if result.Description != nil { + if sb.Len() > 0 { + fmt.Print(sb, ":") + } + fmt.Print(sb, *result.Description) + } + if sb.Len() > 0 { + c.log.ERROR.Printf("%s: %s", typ, sb.String()) + } + } +} diff --git a/server/eebus/eebus.go b/server/eebus/eebus.go index b2517bc09..7d8382915 100644 --- a/server/eebus/eebus.go +++ b/server/eebus/eebus.go @@ -23,6 +23,7 @@ import ( csplc "github.com/enbility/eebus-go/usecases/cs/lpc" cslpp "github.com/enbility/eebus-go/usecases/cs/lpp" eglpc "github.com/enbility/eebus-go/usecases/eg/lpc" + eglpp "github.com/enbility/eebus-go/usecases/eg/lpp" "github.com/enbility/eebus-go/usecases/ma/mgcp" "github.com/enbility/eebus-go/usecases/ma/mpc" shipapi "github.com/enbility/ship-go/api" @@ -64,6 +65,7 @@ type MonitoringAppliance struct { // Energy Guard type EnergyGuard struct { ucapi.EgLPCInterface + ucapi.EgLPPInterface } type EEBus struct { @@ -189,6 +191,7 @@ func NewServer(other Config) (*EEBus, error) { // energy guard c.eg = EnergyGuard{ EgLPCInterface: eglpc.NewLPC(localEntity, c.ucCallback), + EgLPPInterface: eglpp.NewLPP(localEntity, c.ucCallback), } } @@ -199,7 +202,7 @@ func NewServer(other Config) (*EEBus, error) { c.cem.OscEV, c.cem.EvSoc, c.cs.CsLPCInterface, c.cs.CsLPPInterface, c.ma.MaMGCPInterface, c.ma.MaMPCInterface, - c.eg.EgLPCInterface, + c.eg.EgLPCInterface, c.eg.EgLPPInterface, } { c.service.AddUseCase(uc) }