Improve dumping vehicle identifiers and action config

This commit is contained in:
andig 2021-12-04 11:10:47 +01:00
parent f2b6deadb2
commit d54632530d
2 changed files with 15 additions and 8 deletions

View file

@ -1,9 +1,12 @@
package api
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/fatih/structs"
)
//go:generate mockgen -package mock -destination ../mock/mock_api.go github.com/evcc-io/evcc/api Charger,ChargeState,ChargePhases,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery
@ -53,13 +56,15 @@ type ActionConfig struct {
TargetSoC *int `mapstructure:"targetSoC,omitempty"` // Target SoC
}
// String implements Stringer
// String implements Stringer and returns the ActionConfig as comma-separated key:value string
func (a ActionConfig) String() string {
if data, err := json.Marshal(a); err != nil {
return fmt.Sprintf("%v\n", err)
} else {
return fmt.Sprintf("%s\n", data)
var s []string
for k, v := range structs.Map(a) {
if v != nil && !reflect.ValueOf(v).IsNil() {
s = append(s, fmt.Sprintf("%s:%v", k, v))
}
}
return strings.Join(s, ", ")
}
// Meter is able to provide current power in W

View file

@ -164,8 +164,10 @@ func (d *dumper) Dump(name string, v interface{}) {
if v, ok := v.(api.Vehicle); ok {
fmt.Fprintf(w, "Capacity:\t%dkWh\n", v.Capacity())
fmt.Fprintf(w, "Identifiers:\t%v\n", v.Identifiers())
fmt.Fprintf(w, "OnIdentified:\t%v\n", v.OnIdentified())
if len(v.Identifiers()) > 0 {
fmt.Fprintf(w, "Identifiers:\t%v\n", v.Identifiers())
fmt.Fprintf(w, "OnIdentified:\t%s\n", v.OnIdentified())
}
}
// Identity