From 55ca860343904455401c3f8f8b6f4287d217eee6 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 21 Jul 2024 11:25:33 +0200 Subject: [PATCH] chore: move EEbus to global config (#14945) --- api/globalconfig/types.go | 2 +- charger/eebus.go | 60 +++++++++--------------- charger/eebus_test.go | 2 +- cmd/configure/eebus.go | 2 +- cmd/eebus.go | 2 +- cmd/setup.go | 2 +- {charger => server}/eebus/certificate.go | 2 +- {charger => server}/eebus/eebus.go | 10 ++-- {charger => server}/eebus/eebus_test.go | 0 server/http.go | 2 +- 10 files changed, 35 insertions(+), 49 deletions(-) rename {charger => server}/eebus/certificate.go (96%) rename {charger => server}/eebus/eebus.go (96%) rename {charger => server}/eebus/eebus_test.go (100%) diff --git a/api/globalconfig/types.go b/api/globalconfig/types.go index a69765674..aaac7b0ea 100644 --- a/api/globalconfig/types.go +++ b/api/globalconfig/types.go @@ -6,9 +6,9 @@ import ( "strconv" "time" - "github.com/evcc-io/evcc/charger/eebus" "github.com/evcc-io/evcc/provider/mqtt" "github.com/evcc-io/evcc/push" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/util/config" "github.com/evcc-io/evcc/util/modbus" ) diff --git a/charger/eebus.go b/charger/eebus.go index 3daf7fa37..920de94f3 100644 --- a/charger/eebus.go +++ b/charger/eebus.go @@ -3,7 +3,6 @@ package charger import ( "errors" "fmt" - "os" "slices" "sync" "time" @@ -14,9 +13,9 @@ import ( spineapi "github.com/enbility/spine-go/api" "github.com/enbility/spine-go/model" "github.com/evcc-io/evcc/api" - "github.com/evcc-io/evcc/charger/eebus" "github.com/evcc-io/evcc/core/loadpoint" "github.com/evcc-io/evcc/provider" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/util" ) @@ -36,6 +35,7 @@ type EEBus struct { uc *eebus.UseCasesEVSE ev spineapi.EntityRemoteInterface + mux sync.RWMutex log *util.Logger lp loadpoint.API minMaxG func() (minMax, error) @@ -51,12 +51,11 @@ type EEBus struct { lastIsChargingCheck time.Time lastIsChargingResult bool - connected bool - connectedC chan bool - connectedTime time.Time + once sync.Once + connectC chan struct{} - muxEntity sync.Mutex - mux sync.Mutex + connected bool + connectedTime time.Time } func init() { @@ -93,11 +92,11 @@ func NewEEBus(ski string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, } c := &EEBus{ - ski: ski, - log: log, - connectedC: make(chan bool, 1), - current: 6, - vasVW: vasVW, + ski: ski, + log: log, + current: 6, + vasVW: vasVW, + connectC: make(chan struct{}), } c.uc = eebus.Instance.RegisterEVSE(ski, c) @@ -121,29 +120,24 @@ func NewEEBus(ski string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, // waitForConnection wait for initial connection and returns an error on failure func (c *EEBus) waitForConnection() error { - timeout := time.After(90 * time.Second) - for { - select { - case <-timeout: - return os.ErrDeadlineExceeded - case connected := <-c.connectedC: - if connected { - return nil - } - } + select { + case <-time.After(90 * time.Second): + return api.ErrTimeout + case <-c.connectC: + return nil } } func (c *EEBus) setEvEntity(entity spineapi.EntityRemoteInterface) { - c.muxEntity.Lock() - defer c.muxEntity.Unlock() + c.mux.Lock() + defer c.mux.Unlock() c.ev = entity } func (c *EEBus) evEntity() spineapi.EntityRemoteInterface { - c.muxEntity.Lock() - defer c.muxEntity.Unlock() + c.mux.RLock() + defer c.mux.RUnlock() return c.ev } @@ -152,16 +146,12 @@ func (c *EEBus) evEntity() spineapi.EntityRemoteInterface { func (c *EEBus) DeviceConnect() { c.log.TRACE.Println("connect ski:", c.ski) - - c.expectedEnableUnpluggedState = false c.setDefaultValues() c.setConnected(true) } func (c *EEBus) DeviceDisconnect() { c.log.TRACE.Println("disconnect ski:", c.ski) - - c.expectedEnableUnpluggedState = false c.setConnected(false) c.setDefaultValues() } @@ -174,6 +164,7 @@ func (c *EEBus) UseCaseEventCB(device spineapi.DeviceRemoteInterface, entity spi c.log.TRACE.Println("EV Connected") c.setEvEntity(entity) c.currentLimit = -1 + c.once.Do(func() { close(c.connectC) }) case evcc.EvDisconnected: c.log.TRACE.Println("EV Disconnected") c.setEvEntity(nil) @@ -183,8 +174,9 @@ func (c *EEBus) UseCaseEventCB(device spineapi.DeviceRemoteInterface, entity spi func (c *EEBus) setDefaultValues() { c.communicationStandard = evcc.EVCCCommunicationStandardUnknown - c.lastIsChargingCheck = time.Now().Add(-time.Hour * 1) + c.lastIsChargingCheck = time.Now().Add(-time.Hour) c.lastIsChargingResult = false + c.expectedEnableUnpluggedState = false } // set wether the EVSE is connected @@ -195,12 +187,6 @@ func (c *EEBus) setConnected(connected bool) { if connected && !c.connected { c.connectedTime = time.Now() } - - select { - case c.connectedC <- connected: - default: - } - c.connected = connected } diff --git a/charger/eebus_test.go b/charger/eebus_test.go index 8a11056de..bd058d786 100644 --- a/charger/eebus_test.go +++ b/charger/eebus_test.go @@ -6,7 +6,7 @@ import ( "github.com/enbility/eebus-go/usecases/mocks" spinemocks "github.com/enbility/spine-go/mocks" - "github.com/evcc-io/evcc/charger/eebus" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/util" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" diff --git a/cmd/configure/eebus.go b/cmd/configure/eebus.go index 0ae1e9b9f..5a48e322d 100644 --- a/cmd/configure/eebus.go +++ b/cmd/configure/eebus.go @@ -3,8 +3,8 @@ package configure import ( "fmt" - "github.com/evcc-io/evcc/charger/eebus" "github.com/evcc-io/evcc/cmd/shutdown" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/util" ) diff --git a/cmd/eebus.go b/cmd/eebus.go index 0e1f0a27a..30b2ddbc6 100644 --- a/cmd/eebus.go +++ b/cmd/eebus.go @@ -4,7 +4,7 @@ import ( "os" "text/template" - "github.com/evcc-io/evcc/charger/eebus" + "github.com/evcc-io/evcc/server/eebus" "github.com/go-sprout/sprout" "github.com/spf13/cobra" ) diff --git a/cmd/setup.go b/cmd/setup.go index 22084d9bd..63a2fcd44 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -18,7 +18,6 @@ import ( "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/api/globalconfig" "github.com/evcc-io/evcc/charger" - "github.com/evcc-io/evcc/charger/eebus" "github.com/evcc-io/evcc/cmd/shutdown" "github.com/evcc-io/evcc/core" "github.com/evcc-io/evcc/core/keys" @@ -31,6 +30,7 @@ import ( "github.com/evcc-io/evcc/server" "github.com/evcc-io/evcc/server/db" "github.com/evcc-io/evcc/server/db/settings" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/server/modbus" "github.com/evcc-io/evcc/server/oauth2redirect" "github.com/evcc-io/evcc/tariff" diff --git a/charger/eebus/certificate.go b/server/eebus/certificate.go similarity index 96% rename from charger/eebus/certificate.go rename to server/eebus/certificate.go index 7364aa7ab..32509c818 100644 --- a/charger/eebus/certificate.go +++ b/server/eebus/certificate.go @@ -15,7 +15,7 @@ import ( // CreateCertificate returns a newly created EEBUS compatible certificate func CreateCertificate() (tls.Certificate, error) { - return cert.CreateCertificate("", EEBUSBrandName, "DE", EEBUSDeviceCode) + return cert.CreateCertificate("", BrandName, "DE", DeviceCode) } // pemBlockForKey marshals private key into pem block diff --git a/charger/eebus/eebus.go b/server/eebus/eebus.go similarity index 96% rename from charger/eebus/eebus.go rename to server/eebus/eebus.go index d7fc7ad80..d37743e93 100644 --- a/charger/eebus/eebus.go +++ b/server/eebus/eebus.go @@ -27,9 +27,9 @@ import ( ) const ( - EEBUSBrandName string = "EVCC" - EEBUSModel string = "HEMS" - EEBUSDeviceCode string = "EVCC_HEMS_01" // used as common name in cert generation + BrandName string = "EVCC" + Model string = "HEMS" + DeviceCode string = "EVCC_HEMS_01" // used as common name in cert generation ) type Config struct { @@ -115,7 +115,7 @@ func NewServer(other Config) (*EEBus, error) { // TODO: get the voltage from the site configuration, err := eebusapi.NewConfiguration( - EEBUSBrandName, EEBUSBrandName, EEBUSModel, serial, + BrandName, BrandName, Model, serial, model.DeviceTypeTypeEnergyManagementSystem, []model.EntityTypeType{model.EntityTypeTypeCEM}, port, certificate, time.Second*4, @@ -125,7 +125,7 @@ func NewServer(other Config) (*EEBus, error) { } // for backward compatibility - configuration.SetAlternateMdnsServiceName(EEBUSDeviceCode) + configuration.SetAlternateMdnsServiceName(DeviceCode) configuration.SetAlternateIdentifier(serial) configuration.SetInterfaces(cc.Interfaces) diff --git a/charger/eebus/eebus_test.go b/server/eebus/eebus_test.go similarity index 100% rename from charger/eebus/eebus_test.go rename to server/eebus/eebus_test.go diff --git a/server/http.go b/server/http.go index 4a1b9ebe2..2573bac71 100644 --- a/server/http.go +++ b/server/http.go @@ -7,10 +7,10 @@ import ( eapi "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/api/globalconfig" - "github.com/evcc-io/evcc/charger/eebus" "github.com/evcc-io/evcc/core/keys" "github.com/evcc-io/evcc/core/site" "github.com/evcc-io/evcc/server/assets" + "github.com/evcc-io/evcc/server/eebus" "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/auth" "github.com/evcc-io/evcc/util/config"