Improve config validation (#102)

This commit is contained in:
andig 2020-05-06 11:33:38 +02:00 • committed by GitHub
parent 280c4f32f0
commit 377717b65c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package charger
import (
"fmt"
"net"
"time"
"github.com/andig/evcc/api"
@ -17,7 +18,7 @@ const (
)
// PhoenixEMCP is an api.ChargeController implementation for Phoenix EM-CP-PP-ETH wallboxes.
// It uses Modbus TCP to communicate with the wallbox at modbus client id 255.
// It uses Modbus TCP to communicate with the wallbox at modbus client id 180.
type PhoenixEMCP struct {
log *util.Logger
client modbus.Client
@ -32,8 +33,13 @@ func NewPhoenixEMCPFromConfig(log *util.Logger, other map[string]interface{}) ap
}{}
util.DecodeOther(log, other, &cc)
if _, _, err := net.SplitHostPort(cc.URI); err != nil {
log.FATAL.Printf("config: missing or invalid phoenix EM-CP uri: %s", cc.URI)
}
if cc.ID == 0 {
log.FATAL.Fatal("config: missing slave id")
cc.ID = 180
log.WARN.Printf("config: missing phoenix EM-CP slave id, assuming default %d", cc.ID)
}
return NewPhoenixEMCP(cc.URI, cc.ID)

View file

@ -30,7 +30,8 @@ func NewPhoenixEVCCFromConfig(log *util.Logger, other map[string]interface{}) ap
util.DecodeOther(log, other, &cc)
if cc.ID == 0 {
log.FATAL.Fatal("config: missing slave id")
cc.ID = 255
log.WARN.Printf("config: missing phoenix EV-CC slave id, assuming default %d", cc.ID)
}
return NewPhoenixEVCC(cc.URI, cc.Device, cc.Comset, cc.Baudrate, cc.ID)

View file

@ -3,6 +3,7 @@ package charger
import (
"encoding/binary"
"fmt"
"net"
"time"
"github.com/andig/evcc/api"
@ -42,6 +43,11 @@ func NewWallbeFromConfig(log *util.Logger, other map[string]interface{}) *Wallbe
}{}
util.DecodeOther(log, other, &cc)
if _, _, err := net.SplitHostPort(cc.URI); err != nil {
cc.URI = "192.168.0.8:502"
log.WARN.Printf("config: missing or invalid wallbe uri, using default %s", cc.URI)
}
wb := NewWallbe(cc.URI)
if cc.Legacy {
@ -53,10 +59,6 @@ func NewWallbeFromConfig(log *util.Logger, other map[string]interface{}) *Wallbe
// NewWallbe creates a Wallbe charger
func NewWallbe(conn string) *Wallbe {
if conn == "" {
conn = "192.168.0.8:502"
}
handler := modbus.NewTCPClientHandler(conn)
client := modbus.NewClient(handler)

View file

@ -1,15 +1,19 @@
package charger
import "testing"
import (
"testing"
"github.com/andig/evcc/util"
)
func TestWallbe(t *testing.T) {
wb := NewWallbeFromConfig(nil, nil)
wb := NewWallbeFromConfig(util.NewLogger(""), nil)
if wb.factor != 10 {
t.Errorf("invalid factor: %d", wb.factor)
}
wb = NewWallbeFromConfig(nil, map[string]interface{}{"legacy": true})
wb = NewWallbeFromConfig(util.NewLogger(""), map[string]interface{}{"legacy": true})
if wb.factor != 1 {
t.Errorf("invalid factor: %d", wb.factor)