Add default scheme and port for http, websockets and modbus

This commit is contained in:
andig 2020-12-19 12:42:46 +01:00
parent 2b20e81b92
commit b30bb4e77e
10 changed files with 71 additions and 9 deletions

View file

@ -1,4 +1,4 @@
name: CI
name: Build
on:
push:

View file

@ -68,11 +68,12 @@ func NewEVSEWifiFromConfig(other map[string]interface{}) (api.Charger, error) {
Power, Energy, Currents bool
}
}{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
evse, err := NewEVSEWifi(cc.URI)
evse, err := NewEVSEWifi(util.DefaultScheme(cc.URI, "http"))
if err != nil {
return evse, err
}

View file

@ -75,7 +75,7 @@ func NewMobileConnectFromConfig(other map[string]interface{}) (api.Charger, erro
return nil, err
}
return NewMobileConnect(cc.URI, cc.Password)
return NewMobileConnect(util.DefaultScheme(cc.URI, "https"), cc.Password)
}
// NewMobileConnect creates MCC charger

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.13
require (
github.com/PuerkitoBio/goquery v1.6.0
github.com/andig/evcc-config v0.0.0-20201116095052-80cf13789787
github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/avast/retry-go v3.0.0+incompatible
github.com/benbjohnson/clock v1.0.3

4
go.sum
View file

@ -17,8 +17,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/alvaroloes/enumer v1.1.2 h1:5khqHB33TZy1GWCO/lZwcroBFh7u+0j40T83VUbfAMY=
github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo=
github.com/andig/evcc v0.0.0-20200727161511-d58eb15f2dc9/go.mod h1:8HONEC6cC2s4k0u3QL7GIjrYOZYTOKiiXybw0FIJL0A=
github.com/andig/evcc-config v0.0.0-20201116095052-80cf13789787 h1:u7S+iqONU2pGpt042HgHkUiPMeejWYwAAUVz5BpdjfE=
github.com/andig/evcc-config v0.0.0-20201116095052-80cf13789787/go.mod h1:N0hIjIy+5E2AR1fF7Tg2IzBlblBrnFvCCaDGAaHzbWk=
github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875 h1:hCl4v1ZjhvfHFakIRLZiJfuraXSM0BN/4rou/yZmTCc=
github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875/go.mod h1:N0hIjIy+5E2AR1fF7Tg2IzBlblBrnFvCCaDGAaHzbWk=
github.com/andig/gosunspec v0.0.0-20200429133549-3cf6a82fed9c h1:AMtX56iHlNYVxMID7fe9efuVtaxgtdjyMeolg7q87IE=
github.com/andig/gosunspec v0.0.0-20200429133549-3cf6a82fed9c/go.mod h1:YkshK8WMzYn1iXAZzHUO75gIqhMSan2ctgBVtBkRIyA=
github.com/andig/viper v1.6.3-0.20201123175942-a5af09afab5b h1:n3O7DTcqZzpl8/6zVCCkHYbc/zyTfXQCk4xaTAkQ5aE=

View file

@ -65,9 +65,14 @@ func NewHTTPProviderFromConfig(other map[string]interface{}) (IntProvider, error
log := util.NewLogger("http")
url := util.DefaultScheme(cc.URI, "http")
if url != cc.URI {
log.WARN.Printf("missing scheme for %s, assuming http", cc.URI)
}
p := &HTTP{
Helper: request.NewHelper(log),
url: cc.URI,
url: url,
method: cc.Method,
headers: cc.Headers,
body: cc.Body,

View file

@ -44,18 +44,26 @@ func NewSocketProviderFromConfig(other map[string]interface{}) (IntProvider, err
Insecure bool
Auth Auth
Timeout time.Duration
}{Headers: make(map[string]string)}
}{
Headers: make(map[string]string),
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger("ws")
url := util.DefaultScheme(cc.URI, "ws")
if url != cc.URI {
log.WARN.Printf("missing scheme for %s, assuming ws", cc.URI)
}
p := &Socket{
log: log,
Helper: request.NewHelper(log),
mux: util.NewWaiter(cc.Timeout, func() { log.TRACE.Println("wait for initial value") }),
url: cc.URI,
url: url,
headers: cc.Headers,
scale: cc.Scale,
}

View file

@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/andig/evcc/util"
"github.com/volkszaehler/mbmd/meters"
"github.com/volkszaehler/mbmd/meters/rs485"
"github.com/volkszaehler/mbmd/meters/sunspec"
@ -146,6 +147,8 @@ func NewConnection(uri, device, comset string, baudrate int, rtu bool, slaveID u
}
if uri != "" {
uri = util.DefaultPort(uri, 502)
if rtu {
conn = registeredConnection(uri, meters.NewRTUOverTCP(uri))
} else {

View file

@ -3,6 +3,7 @@ package util
import (
"fmt"
"net"
"net/url"
)
// DefaultPort appends given port to connection if not specified
@ -14,6 +15,28 @@ func DefaultPort(conn string, port int) string {
return conn
}
// DefaultScheme prepends given scheme to uri if not specified
func DefaultScheme(uri string, scheme string) string {
u, err := url.Parse(uri)
if err != nil {
return uri
}
switch {
case u.Scheme == "":
// scheme missing
u.Scheme = scheme
case u.Opaque != "":
// host:port format is parsed as scheme:opaque (https://golang.org/pkg/net/url/#URL)
if u, err = url.Parse(fmt.Sprintf("%s://%s", scheme, uri)); err != nil {
return uri
}
}
return u.String()
}
// LocalIPs returns a slice of local IPv4 addresses
func LocalIPs() (ips []net.IPNet) {
ifaces, err := net.Interfaces()

View file

@ -16,3 +16,25 @@ func TestDefaultPort(t *testing.T) {
t.Errorf("expected %s, got %s", expect, uri)
}
}
func TestDefaultScheme(t *testing.T) {
expect := "http://localhost"
if uri := DefaultScheme("localhost", "http"); uri != expect {
t.Errorf("expected %s, got %s", expect, uri)
}
if uri := DefaultScheme("http://localhost", "http"); uri != expect {
t.Errorf("expected %s, got %s", expect, uri)
}
if uri := DefaultScheme("http://localhost", "https"); uri != expect {
t.Errorf("expected %s, got %s", expect, uri)
}
expect = "ws://localhost:8080"
if uri := DefaultScheme("localhost:8080", "ws"); uri != expect {
t.Errorf("expected %s, got %s", expect, uri)
}
}