HEMS: add "smartgrid" session logging (#25907)

This commit is contained in:
andig 2025-12-12 18:29:31 +01:00 • committed by GitHub
parent 73f468e4c5
commit 381573b0eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 915 additions and 215 deletions

View file

@ -11,9 +11,11 @@ import (
"github.com/evcc-io/evcc/core/circuit"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/shared"
"github.com/evcc-io/evcc/hems/smartgrid"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/server/eebus"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
)
type EEBus struct {
@ -28,6 +30,7 @@ type EEBus struct {
root api.Circuit
passthrough func(bool) error
smartgridID uint
status status
statusUpdated time.Time
@ -246,6 +249,39 @@ func (c *EEBus) setStatusAndLimit(status status, limit float64) {
c.statusUpdated = time.Now()
c.setLimit(limit)
if err := c.updateSession(limit); err != nil {
c.log.ERROR.Printf("smartgrid session: %v", err)
}
}
// TODO keep in sync across HEMS implementations
func (c *EEBus) updateSession(limit float64) error {
// start session
if limit > 0 && c.smartgridID == 0 {
var power *float64
if p := c.root.GetChargePower(); p > 0 {
power = lo.ToPtr(p)
}
sid, err := smartgrid.StartManage(smartgrid.Dim, power, limit)
if err != nil {
return err
}
c.smartgridID = sid
}
// stop session
if limit == 0 && c.smartgridID != 0 {
if err := smartgrid.StopManage(c.smartgridID); err != nil {
return err
}
c.smartgridID = 0
}
return nil
}
func (c *EEBus) setLimit(limit float64) {

View file

@ -10,8 +10,10 @@ import (
"github.com/evcc-io/evcc/core/circuit"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/shared"
"github.com/evcc-io/evcc/hems/smartgrid"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
)
type Relay struct {
@ -19,6 +21,8 @@ type Relay struct {
root api.Circuit
passthrough func(bool) error
smartgridID uint
limit func() (bool, error)
maxPower float64
interval time.Duration
@ -98,21 +102,62 @@ func (c *Relay) Run() {
}
func (c *Relay) run() error {
limit, err := c.limit()
limited, err := c.limit()
if err != nil {
return err
}
var power float64
if limit {
power = c.maxPower
var limit float64
if limited {
limit = c.maxPower
}
c.root.Dim(limit)
c.root.SetMaxPower(power)
if err := c.setLimited(limit); err != nil {
return err
}
if err := c.updateSession(limit); err != nil {
return fmt.Errorf("smartgrid session: %v", err)
}
return nil
}
// TODO keep in sync across HEMS implementations
func (c *Relay) updateSession(limit float64) error {
// start session
if limit > 0 && c.smartgridID == 0 {
var power *float64
if p := c.root.GetChargePower(); p > 0 {
power = lo.ToPtr(p)
}
sid, err := smartgrid.StartManage(smartgrid.Dim, power, limit)
if err != nil {
return err
}
c.smartgridID = sid
}
// stop session
if limit == 0 && c.smartgridID != 0 {
if err := smartgrid.StopManage(c.smartgridID); err != nil {
return err
}
c.smartgridID = 0
}
return nil
}
func (c *Relay) setLimited(limit float64) error {
c.root.Dim(limit > 0)
c.root.SetMaxPower(limit)
if c.passthrough != nil {
if err := c.passthrough(limit); err != nil {
if err := c.passthrough(limit > 0); err != nil {
return fmt.Errorf("passthrough failed: %w", err)
}
}

View file

@ -0,0 +1,32 @@
package smartgrid
import (
"time"
"github.com/evcc-io/evcc/server/db"
)
func init() {
db.Register(func() error {
return db.Instance.AutoMigrate(new(GridSession))
})
}
func StartManage(typ Type, grid *float64, limit float64) (uint, error) {
gs := GridSession{
Created: time.Now(),
Type: typ,
GridPower: grid,
LimitPower: limit,
}
tx := db.Instance.Save(&gs)
return gs.ID, tx.Error
}
func StopManage(id uint) error {
return db.Instance.Where(&GridSession{
ID: id,
}).Updates(&GridSession{
Finished: time.Now(),
}).Error
}

34
hems/smartgrid/types.go Normal file
View file

@ -0,0 +1,34 @@
package smartgrid
import (
"context"
"io"
"time"
csvutil "github.com/evcc-io/evcc/util/csv"
)
type GridSession struct {
ID uint `json:"id" csv:"-" gorm:"primarykey"`
Created time.Time `json:"created,omitzero"`
Finished time.Time `json:"finished,omitzero"`
Type Type `json:"type"`
GridPower *float64 `json:"grid,omitempty"`
LimitPower float64 `json:"limit"`
}
type Type string
const (
Dim Type = "consumption"
Curtail Type = "feedin"
)
type GridSessions []GridSession
// WriteCsv implements the api.CsvWriter interface
func (t *GridSessions) WriteCsv(ctx context.Context, w io.Writer) error {
return csvutil.WriteStructSlice(ctx, w, t, csvutil.Config{
I18nPrefix: "config.hems.csv",
})
}