Config UI: add loadpoints (BC) (#18137)

This commit is contained in:
Michael Geers 2025-01-31 12:26:23 +01:00 • committed by GitHub
parent d7fcdf663f
commit 864c2cf599
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
92 changed files with 4643 additions and 1116 deletions

111
core/settings/config.go Normal file
View file

@ -0,0 +1,111 @@
package settings
import (
"encoding/json"
"errors"
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cast"
)
var _ Settings = (*ConfigSettings)(nil)
type ConfigSettings struct {
log *util.Logger
conf *config.Config
}
func NewConfigSettingsAdapter(log *util.Logger, conf *config.Config) *ConfigSettings {
return &ConfigSettings{log, conf}
}
func (s *ConfigSettings) get(key string) (any, error) {
val := s.conf.Named().Other[key]
if val == nil {
return nil, errors.New("not found")
}
return val, nil
}
func (s *ConfigSettings) set(key string, val any) {
data := s.conf.Named().Other
data[key] = val
if err := s.conf.PartialUpdate(data); err != nil {
s.log.ERROR.Println(err)
}
}
func (s *ConfigSettings) SetString(key string, val string) {
s.set(key, val)
}
func (s *ConfigSettings) SetInt(key string, val int64) {
s.set(key, val)
}
func (s *ConfigSettings) SetFloat(key string, val float64) {
s.set(key, val)
}
func (s *ConfigSettings) SetTime(key string, val time.Time) {
s.set(key, val)
}
func (s *ConfigSettings) SetBool(key string, val bool) {
s.set(key, val)
}
func (s *ConfigSettings) SetJson(key string, val any) error {
s.set(key, val)
return nil
}
func (s *ConfigSettings) String(key string) (string, error) {
val, err := s.get(key)
if err != nil {
return "", err
}
return cast.ToStringE(val)
}
func (s *ConfigSettings) Int(key string) (int64, error) {
val, err := s.get(key)
if err != nil {
return 0, err
}
return cast.ToInt64E(val)
}
func (s *ConfigSettings) Float(key string) (float64, error) {
val, err := s.get(key)
if err != nil {
return 0, err
}
return cast.ToFloat64E(val)
}
func (s *ConfigSettings) Time(key string) (time.Time, error) {
val, err := s.get(key)
if err != nil {
return time.Time{}, err
}
return cast.ToTimeE(val)
}
func (s *ConfigSettings) Bool(key string) (bool, error) {
val, err := s.get(key)
if err != nil {
return false, err
}
return cast.ToBoolE(val)
}
func (s *ConfigSettings) Json(key string, res any) error {
str, err := s.String(key)
if str == "" || err != nil {
return err
}
return json.Unmarshal([]byte(str), &res)
}

65
core/settings/database.go Normal file
View file

@ -0,0 +1,65 @@
package settings
import (
"time"
db "github.com/evcc-io/evcc/server/db/settings"
)
var _ Settings = (*dbSettings)(nil)
type dbSettings struct {
Key string
}
func NewDatabaseSettingsAdapter(key string) Settings {
return &dbSettings{key}
}
func (s *dbSettings) SetString(key string, val string) {
db.SetString(s.Key+key, val)
}
func (s *dbSettings) SetInt(key string, val int64) {
db.SetInt(s.Key+key, val)
}
func (s *dbSettings) SetFloat(key string, val float64) {
db.SetFloat(s.Key+key, val)
}
func (s *dbSettings) SetTime(key string, val time.Time) {
db.SetTime(s.Key+key, val)
}
func (s *dbSettings) SetBool(key string, val bool) {
db.SetBool(s.Key+key, val)
}
func (s *dbSettings) SetJson(key string, val any) error {
return db.SetJson(s.Key+key, val)
}
func (s *dbSettings) String(key string) (string, error) {
return db.String(s.Key + key)
}
func (s *dbSettings) Int(key string) (int64, error) {
return db.Int(s.Key + key)
}
func (s *dbSettings) Float(key string) (float64, error) {
return db.Float(s.Key + key)
}
func (s *dbSettings) Time(key string) (time.Time, error) {
return db.Time(s.Key + key)
}
func (s *dbSettings) Bool(key string) (bool, error) {
return db.Bool(s.Key + key)
}
func (s *dbSettings) Json(key string, res any) error {
return db.Json(s.Key+key, &res)
}

18
core/settings/settings.go Normal file
View file

@ -0,0 +1,18 @@
package settings
import "time"
type Settings interface {
SetString(key string, val string)
SetInt(key string, val int64)
SetFloat(key string, val float64)
SetTime(key string, val time.Time)
SetJson(key string, val any) error
SetBool(key string, val bool)
String(key string) (string, error)
Int(key string) (int64, error)
Float(key string) (float64, error)
Time(key string) (time.Time, error)
Bool(key string) (bool, error)
Json(key string, res any) error
}