Skip to content

Commit

Permalink
Improve config validation (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 6, 2020
1 parent 280c4f3 commit 377717b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 8 additions & 2 deletions charger/phoenix-emcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package charger

import (
"fmt"
"net"
"time"

"github.com/andig/evcc/api"
Expand All @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion charger/phoenix-evcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions charger/wallbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package charger
import (
"encoding/binary"
"fmt"
"net"
"time"

"github.com/andig/evcc/api"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions charger/wallbe_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 377717b

Please sign in to comment.