Config UI: split SHM HEMS into separate config (BC) (#23190)
This commit is contained in:
parent
018b07ce62
commit
15958df887
24 changed files with 407 additions and 114 deletions
|
|
@ -8,8 +8,6 @@ import (
|
|||
"github.com/evcc-io/evcc/core/site"
|
||||
"github.com/evcc-io/evcc/hems/eebus"
|
||||
"github.com/evcc-io/evcc/hems/relay"
|
||||
"github.com/evcc-io/evcc/hems/semp"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
)
|
||||
|
||||
// HEMS describes the HEMS system interface
|
||||
|
|
@ -18,14 +16,14 @@ type HEMS interface {
|
|||
}
|
||||
|
||||
// NewFromConfig creates new HEMS from config
|
||||
func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}, site site.API, httpd *server.HTTPd) (HEMS, error) {
|
||||
func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}, site site.API) (HEMS, error) {
|
||||
switch strings.ToLower(typ) {
|
||||
case "sma", "shm", "semp":
|
||||
return semp.New(other, site, httpd)
|
||||
return nil, errors.New("breaking change: Sunny Home Manager integration is always on. See https://github.com/evcc-io/evcc/releases and https://docs.evcc.io/en/docs/integrations/sma-sunny-home-manager")
|
||||
case "eebus":
|
||||
return eebus.New(ctx, other, site)
|
||||
return eebus.NewFromConfig(ctx, other, site)
|
||||
case "relay":
|
||||
return relay.New(ctx, other, site)
|
||||
return relay.NewFromConfig(ctx, other, site)
|
||||
default:
|
||||
return nil, errors.New("unknown hems: " + typ)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,8 @@ type Limits struct {
|
|||
FailsafeDurationMinimum time.Duration
|
||||
}
|
||||
|
||||
// New creates an EEBus HEMS from generic config
|
||||
func New(ctx context.Context, other map[string]interface{}, site site.API) (*EEBus, error) {
|
||||
// NewFromConfig creates an EEBus HEMS from generic config
|
||||
func NewFromConfig(ctx context.Context, other map[string]interface{}, site site.API) (*EEBus, error) {
|
||||
cc := struct {
|
||||
Ski string
|
||||
Limits `mapstructure:",squash"`
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ type Relay struct {
|
|||
interval time.Duration
|
||||
}
|
||||
|
||||
// New creates an Relay HEMS from generic config
|
||||
func New(ctx context.Context, other map[string]interface{}, site site.API) (*Relay, error) {
|
||||
// NewFromConfig creates an Relay HEMS from generic config
|
||||
func NewFromConfig(ctx context.Context, other map[string]interface{}, site site.API) (*Relay, error) {
|
||||
cc := struct {
|
||||
MaxPower float64
|
||||
Limit plugin.Config
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package semp
|
||||
package shm
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package semp
|
||||
package shm
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
|
@ -15,7 +15,6 @@ import (
|
|||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/core/loadpoint"
|
||||
"github.com/evcc-io/evcc/core/site"
|
||||
"github.com/evcc-io/evcc/server"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/machine"
|
||||
"github.com/google/uuid"
|
||||
|
|
@ -39,8 +38,6 @@ var serverName = "EVCC SEMP Server " + util.Version
|
|||
// SEMP is the SMA SEMP server
|
||||
type SEMP struct {
|
||||
log *util.Logger
|
||||
closeC chan struct{}
|
||||
doneC chan struct{}
|
||||
controllable bool
|
||||
vid string
|
||||
did []byte
|
||||
|
|
@ -50,65 +47,66 @@ type SEMP struct {
|
|||
site site.API
|
||||
}
|
||||
|
||||
// New generates SEMP Gateway listening at /semp endpoint
|
||||
func New(conf map[string]interface{}, site site.API, httpd *server.HTTPd) (*SEMP, error) {
|
||||
cc := struct {
|
||||
VendorID string
|
||||
DeviceID string
|
||||
AllowControl bool
|
||||
}{
|
||||
VendorID: "28081973",
|
||||
}
|
||||
type Config struct {
|
||||
AllowControl bool `json:"allowControl"`
|
||||
VendorId string `json:"vendorId"`
|
||||
DeviceId string `json:"deviceId"`
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(conf, &cc); err != nil {
|
||||
return nil, err
|
||||
// NewFromConfig creates a new SEMP instance from configuration and starts it
|
||||
func NewFromConfig(cfg Config, site site.API, addr string, router *mux.Router) error {
|
||||
vendorId := cfg.VendorId
|
||||
if vendorId == "" {
|
||||
vendorId = "28081973"
|
||||
} else if len(vendorId) != 8 {
|
||||
return fmt.Errorf("invalid vendor id: %v. Must be 8 characters HEX string", vendorId)
|
||||
}
|
||||
|
||||
uid, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(cc.VendorID) != 8 {
|
||||
return nil, fmt.Errorf("invalid vendor id: %v", cc.VendorID)
|
||||
return err
|
||||
}
|
||||
|
||||
var did []byte
|
||||
if cc.DeviceID == "" {
|
||||
if cfg.DeviceId == "" {
|
||||
if did, err = UniqueDeviceID(); err != nil {
|
||||
return nil, fmt.Errorf("creating device id: %w", err)
|
||||
return fmt.Errorf("creating device id: %w", err)
|
||||
}
|
||||
} else {
|
||||
if did, err = hex.DecodeString(cc.DeviceID); err != nil {
|
||||
return nil, fmt.Errorf("device id: %w", err)
|
||||
if did, err = hex.DecodeString(cfg.DeviceId); err != nil {
|
||||
return fmt.Errorf("device id: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(did) != 6 {
|
||||
return nil, fmt.Errorf("invalid device id: %v", cc.DeviceID)
|
||||
return fmt.Errorf("invalid device id: %v. Must be 12 characters HEX string", cfg.DeviceId)
|
||||
}
|
||||
|
||||
s := &SEMP{
|
||||
doneC: make(chan struct{}),
|
||||
log: util.NewLogger("semp"),
|
||||
site: site,
|
||||
uid: uid.String(),
|
||||
vid: cc.VendorID,
|
||||
vid: vendorId,
|
||||
did: did,
|
||||
controllable: cc.AllowControl,
|
||||
controllable: cfg.AllowControl,
|
||||
}
|
||||
|
||||
// find external port
|
||||
_, port, err := net.SplitHostPort(httpd.Addr)
|
||||
// TODO refactor network config
|
||||
_, port, err := net.SplitHostPort(addr)
|
||||
if err == nil {
|
||||
s.port, err = strconv.Atoi(port)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.hostURI = s.callbackURI()
|
||||
|
||||
s.handlers(httpd.Router())
|
||||
s.handlers(router)
|
||||
|
||||
return s, err
|
||||
go s.run()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SEMP) advertise(st, usn string) (*ssdp.Advertiser, error) {
|
||||
|
|
@ -116,13 +114,8 @@ func (s *SEMP) advertise(st, usn string) (*ssdp.Advertiser, error) {
|
|||
return ssdp.Advertise(st, usn, descriptor, serverName, maxAge)
|
||||
}
|
||||
|
||||
// Run executes the SEMP runtime
|
||||
func (s *SEMP) Run() {
|
||||
if s.closeC != nil {
|
||||
panic("already running")
|
||||
}
|
||||
s.closeC = make(chan struct{})
|
||||
|
||||
// run executes the SEMP runtime
|
||||
func (s *SEMP) run() {
|
||||
uid := "uuid:" + s.uid
|
||||
|
||||
var ads []*ssdp.Advertiser
|
||||
|
|
@ -139,40 +132,13 @@ func (s *SEMP) Run() {
|
|||
ads = append(ads, ad)
|
||||
}
|
||||
|
||||
ANNOUNCE:
|
||||
for tick := time.Tick(maxAge * time.Second / 2); ; {
|
||||
select {
|
||||
case <-tick:
|
||||
for _, ad := range ads {
|
||||
if err := ad.Alive(); err != nil {
|
||||
s.log.ERROR.Println(err)
|
||||
}
|
||||
for range time.Tick(maxAge * time.Second / 2) {
|
||||
for _, ad := range ads {
|
||||
if err := ad.Alive(); err != nil {
|
||||
s.log.ERROR.Println(err)
|
||||
}
|
||||
case <-s.closeC:
|
||||
break ANNOUNCE
|
||||
}
|
||||
}
|
||||
|
||||
for _, ad := range ads {
|
||||
if err := ad.Bye(); err != nil {
|
||||
s.log.ERROR.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
close(s.doneC)
|
||||
}
|
||||
|
||||
// Stop stops the SEMP runtime
|
||||
func (s *SEMP) Stop() {
|
||||
if s.closeC == nil {
|
||||
panic("not running")
|
||||
}
|
||||
close(s.closeC)
|
||||
}
|
||||
|
||||
// Done returns the done channel. The channel is closed after byebye has been sent.
|
||||
func (s *SEMP) Done() chan struct{} {
|
||||
return s.doneC
|
||||
}
|
||||
|
||||
func (s *SEMP) callbackURI() string {
|
||||
|
|
@ -189,7 +155,6 @@ func (s *SEMP) callbackURI() string {
|
|||
}
|
||||
|
||||
uri := fmt.Sprintf("http://%s:%d", ip, s.port)
|
||||
s.log.WARN.Printf("%s unspecified, using %s instead", sempBaseURLEnv, uri)
|
||||
|
||||
return uri
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue