-
-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
242 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package loadpoint | ||
|
||
import ( | ||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/util" | ||
) | ||
|
||
type StaticConfig struct { | ||
// static config | ||
Charger string `json:"charger,omitempty"` | ||
Meter string `json:"meter,omitempty"` | ||
Circuit string `json:"circuit,omitempty"` | ||
Vehicle string `json:"vehicle,omitempty"` | ||
} | ||
|
||
type DynamicConfig struct { | ||
// dynamic config | ||
Title string `json:"title"` | ||
Mode string `json:"mode"` | ||
Priority int `json:"priority"` | ||
Phases int `json:"phases"` | ||
MinCurrent float64 `json:"minCurrent"` | ||
MaxCurrent float64 `json:"maxCurrent"` | ||
SmartCostLimit *float64 `json:"smartCostLimit"` | ||
|
||
Thresholds ThresholdsConfig `json:"thresholds"` | ||
Soc SocConfig `json:"soc"` | ||
} | ||
|
||
func SplitConfig(payload map[string]any) (DynamicConfig, map[string]any, error) { | ||
// split static and dynamic config via mapstructure | ||
var cc struct { | ||
DynamicConfig `mapstructure:",squash"` | ||
Other map[string]any `mapstructure:",remain"` | ||
} | ||
|
||
if err := util.DecodeOther(payload, &cc); err != nil { | ||
return DynamicConfig{}, nil, err | ||
} | ||
|
||
// TODO: proper handling of id/name | ||
delete(cc.Other, "id") | ||
delete(cc.Other, "name") | ||
|
||
return cc.DynamicConfig, cc.Other, nil | ||
} | ||
|
||
func (payload DynamicConfig) Apply(lp API) error { | ||
lp.SetTitle(payload.Title) | ||
lp.SetPriority(payload.Priority) | ||
lp.SetSmartCostLimit(payload.SmartCostLimit) | ||
lp.SetThresholds(payload.Thresholds) | ||
|
||
// TODO mode warning | ||
lp.SetSocConfig(payload.Soc) | ||
|
||
mode, err := api.ChargeModeString(payload.Mode) | ||
if err == nil { | ||
lp.SetMode(mode) | ||
} | ||
|
||
if err == nil { | ||
err = lp.SetPhases(payload.Phases) | ||
} | ||
|
||
if err == nil { | ||
err = lp.SetMinCurrent(payload.MinCurrent) | ||
} | ||
|
||
if err == nil { | ||
err = lp.SetMaxCurrent(payload.MaxCurrent) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package settings | ||
|
||
import ( | ||
"encoding/json" | ||
"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 { | ||
return s.conf.Named().Other[key] | ||
} | ||
|
||
func (s *ConfigSettings) set(key string, val any) { | ||
data := s.conf.Named().Other | ||
data[key] = val | ||
if err := s.conf.Update(data); err != nil { | ||
s.log.ERROR.Println(err) | ||
} | ||
} | ||
|
||
func (s *ConfigSettings) SetString(key string, val string) { | ||
if s == nil { | ||
return | ||
} | ||
s.set(key, val) | ||
} | ||
|
||
func (s *ConfigSettings) SetInt(key string, val int64) { | ||
if s == nil { | ||
return | ||
} | ||
s.set(key, val) | ||
} | ||
|
||
func (s *ConfigSettings) SetFloat(key string, val float64) { | ||
if s == nil { | ||
return | ||
} | ||
s.set(key, val) | ||
} | ||
|
||
func (s *ConfigSettings) SetTime(key string, val time.Time) { | ||
if s == nil { | ||
return | ||
} | ||
s.set(key, val) | ||
} | ||
|
||
func (s *ConfigSettings) SetBool(key string, val bool) { | ||
if s == nil { | ||
return | ||
} | ||
s.set(key, val) | ||
} | ||
|
||
func (s *ConfigSettings) SetJson(key string, val any) error { | ||
if s == nil { | ||
return nil | ||
} | ||
s.set(key, val) | ||
return nil | ||
} | ||
|
||
func (s *ConfigSettings) String(key string) (string, error) { | ||
if s == nil { | ||
return "", nil | ||
} | ||
return cast.ToStringE(s.get(key)) | ||
} | ||
|
||
func (s *ConfigSettings) Int(key string) (int64, error) { | ||
if s == nil { | ||
return 0, nil | ||
} | ||
return cast.ToInt64E(s.get(key)) | ||
} | ||
|
||
func (s *ConfigSettings) Float(key string) (float64, error) { | ||
if s == nil { | ||
return 0, nil | ||
} | ||
return cast.ToFloat64E(s.get(key)) | ||
} | ||
|
||
func (s *ConfigSettings) Time(key string) (time.Time, error) { | ||
if s == nil { | ||
return time.Time{}, nil | ||
} | ||
return cast.ToTimeE(s.get(key)) | ||
} | ||
|
||
func (s *ConfigSettings) Bool(key string) (bool, error) { | ||
if s == nil { | ||
return false, nil | ||
} | ||
return cast.ToBoolE(s.get(key)) | ||
} | ||
|
||
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.