Skip to content

Commit

Permalink
Split common utility functions into util package (evcc-io#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Apr 27, 2020
1 parent 56ad3da commit 1729399
Show file tree
Hide file tree
Showing 44 changed files with 177 additions and 193 deletions.
5 changes: 3 additions & 2 deletions charger/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package charger
import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
)

// Charger is an api.Charger implementation with configurable getters and setters.
Expand All @@ -14,9 +15,9 @@ type Charger struct {
}

// NewConfigurableFromConfig creates a new configurable charger
func NewConfigurableFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewConfigurableFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ Status, Enable, Enabled, MaxCurrent provider.Config }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

charger := NewConfigurable(
provider.NewStringGetterFromConfig(log, cc.Status),
Expand Down
3 changes: 2 additions & 1 deletion charger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"strings"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

type apiFunction string

// NewFromConfig creates charger from configuration
func NewFromConfig(log *api.Logger, typ string, other map[string]interface{}) api.Charger {
func NewFromConfig(log *util.Logger, typ string, other map[string]interface{}) api.Charger {
var c api.Charger

switch strings.ToLower(typ) {
Expand Down
9 changes: 5 additions & 4 deletions charger/evsewifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

const (
Expand Down Expand Up @@ -43,22 +44,22 @@ type EVSEListEntry struct {

// EVSEWifi charger implementation
type EVSEWifi struct {
*api.HTTPHelper
*util.HTTPHelper
uri string
}

// NewEVSEWifiFromConfig creates a EVSEWifi charger from generic config
func NewEVSEWifiFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewEVSEWifiFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ URI string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewEVSEWifi(cc.URI)
}

// NewEVSEWifi creates EVSEWifi charger
func NewEVSEWifi(uri string) api.Charger {
evse := &EVSEWifi{
HTTPHelper: api.NewHTTPHelper(api.NewLogger("wifi")),
HTTPHelper: util.NewHTTPHelper(util.NewLogger("wifi")),
uri: strings.TrimRight(uri, "/") + "/",
}

Expand Down
9 changes: 5 additions & 4 deletions charger/go-e.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

const (
Expand All @@ -26,22 +27,22 @@ type goeStatusResponse struct {

// GoE charger implementation
type GoE struct {
*api.HTTPHelper
*util.HTTPHelper
uri string
}

// NewGoEFromConfig creates a go-e charger from generic config
func NewGoEFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewGoEFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ URI string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewGoE(cc.URI)
}

// NewGoE creates GoE charger
func NewGoE(URI string) *GoE {
c := &GoE{
HTTPHelper: api.NewHTTPHelper(api.NewLogger("go-e")),
HTTPHelper: util.NewHTTPHelper(util.NewLogger("go-e")),
uri: strings.TrimRight(URI, "/"),
}

Expand Down
9 changes: 5 additions & 4 deletions charger/mcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

const (
Expand Down Expand Up @@ -56,7 +57,7 @@ type MCCCurrentCableInformation struct {

// MobileConnect charger supporting devices from Audi, Bentley, Porsche
type MobileConnect struct {
*api.HTTPHelper
*util.HTTPHelper
uri string
password string
token string
Expand All @@ -66,17 +67,17 @@ type MobileConnect struct {
}

// NewMobileConnectFromConfig creates a MCC charger from generic config
func NewMobileConnectFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewMobileConnectFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ URI, Password string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewMobileConnect(cc.URI, cc.Password)
}

// NewMobileConnect creates MCC charger
func NewMobileConnect(uri string, password string) *MobileConnect {
mcc := &MobileConnect{
HTTPHelper: api.NewHTTPHelper(api.NewLogger("mcc ")),
HTTPHelper: util.NewHTTPHelper(util.NewLogger("mcc ")),
uri: strings.TrimRight(uri, "/"),
password: password,
}
Expand Down
3 changes: 2 additions & 1 deletion charger/mcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

// HTTP testing appproach from http://hassansin.github.io/Unit-Testing-http-client-in-Go
Expand All @@ -36,7 +37,7 @@ func NewTestClient(fn roundTripFunc) *http.Client {
// NewTestMobileConnect .
func NewTestMobileConnect(t *testing.T, responses []apiResponse) *MobileConnect {
mcc := &MobileConnect{
HTTPHelper: api.NewHTTPHelper(nil),
HTTPHelper: util.NewHTTPHelper(nil),
uri: "http://192.168.1.1",
password: "none",
token: "token",
Expand Down
9 changes: 5 additions & 4 deletions charger/nrgkick.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)

const (
Expand Down Expand Up @@ -58,24 +59,24 @@ type NRGDeviceMetadata struct {

// NRGKick charger implementation
type NRGKick struct {
*api.HTTPHelper
*util.HTTPHelper
IP string
MacAddress string
Password string
}

// NewNRGKickFromConfig creates a NRGKick charger from generic config
func NewNRGKickFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewNRGKickFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ IP, MacAddress, Password string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewNRGKick(cc.IP, cc.MacAddress, cc.Password)
}

// NewNRGKick creates NRGKick charger
func NewNRGKick(IP, MacAddress, Password string) *NRGKick {
nrg := &NRGKick{
HTTPHelper: api.NewHTTPHelper(api.NewLogger("kick")),
HTTPHelper: util.NewHTTPHelper(util.NewLogger("kick")),
IP: IP,
MacAddress: MacAddress,
Password: Password,
Expand Down
9 changes: 5 additions & 4 deletions charger/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
"github.com/grid-x/modbus"
)

Expand All @@ -18,22 +19,22 @@ const (
// Phoenix 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.
type Phoenix struct {
log *api.Logger
log *util.Logger
client modbus.Client
handler *modbus.TCPClientHandler
}

// NewPhoenixFromConfig creates a Phoenix charger from generic config
func NewPhoenixFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewPhoenixFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ URI string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewPhoenix(cc.URI)
}

// NewPhoenix creates a Phoenix charger
func NewPhoenix(conn string) api.Charger {
log := api.NewLogger("phoe")
log := util.NewLogger("phoe")
if conn == "" {
log.FATAL.Fatal("missing connection")
}
Expand Down
9 changes: 5 additions & 4 deletions charger/simpleevse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
"github.com/grid-x/modbus"
)

// SimpleEVSE charger implementation
type SimpleEVSE struct {
log *api.Logger
log *util.Logger
client modbus.Client
handler modbus.ClientHandler
}
Expand All @@ -22,16 +23,16 @@ const (
)

// NewSimpleEVSEFromConfig creates a SimpleEVSE charger from generic config
func NewSimpleEVSEFromConfig(log *api.Logger, other map[string]interface{}) api.Charger {
func NewSimpleEVSEFromConfig(log *util.Logger, other map[string]interface{}) api.Charger {
cc := struct{ URI, Device string }{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

return NewSimpleEVSE(cc.URI, cc.Device)
}

// NewSimpleEVSE creates SimpleEVSE charger
func NewSimpleEVSE(conn, device string) api.Charger {
log := api.NewLogger("evse")
log := util.NewLogger("evse")

var handler modbus.ClientHandler
if conn != "" && device != "" {
Expand Down
9 changes: 5 additions & 4 deletions charger/wallbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
"github.com/grid-x/modbus"
)

Expand All @@ -27,19 +28,19 @@ const (
// Phoenix EV-CC-AC1-M3-CBC-RCM-ETH controller.
// It uses Modbus TCP to communicate with the wallbox at modbus client id 255.
type Wallbe struct {
log *api.Logger
log *util.Logger
client modbus.Client
handler *modbus.TCPClientHandler
factor int64
}

// NewWallbeFromConfig creates a Wallbe charger from generic config
func NewWallbeFromConfig(log *api.Logger, other map[string]interface{}) *Wallbe {
func NewWallbeFromConfig(log *util.Logger, other map[string]interface{}) *Wallbe {
cc := struct {
URI string
Legacy bool
}{}
api.DecodeOther(log, other, &cc)
util.DecodeOther(log, other, &cc)

wb := NewWallbe(cc.URI)

Expand All @@ -64,7 +65,7 @@ func NewWallbe(conn string) *Wallbe {
handler.ProtocolRecoveryTimeout = protocolTimeout

wb := &Wallbe{
log: api.NewLogger("wlbe"),
log: util.NewLogger("wlbe"),
client: client,
handler: handler,
factor: 10,
Expand Down
3 changes: 2 additions & 1 deletion cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/server"
"github.com/andig/evcc/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -22,7 +23,7 @@ func init() {
}

func runCharger(cmd *cobra.Command, args []string) {
configureLogging()
util.LogLevel(viper.GetString("log"))
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)

// load config
Expand Down
3 changes: 2 additions & 1 deletion cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/server"
"github.com/andig/evcc/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -22,7 +23,7 @@ func init() {
}

func runMeter(cmd *cobra.Command, args []string) {
configureLogging()
util.LogLevel(viper.GetString("log"))
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)

// load config
Expand Down
19 changes: 4 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"os"
"time"

"github.com/andig/evcc/api"
"github.com/andig/evcc/core"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/server"
"github.com/andig/evcc/util"

"github.com/spf13/cobra"
"github.com/spf13/viper"
latest "github.com/tcnksm/go-latest"
)

var (
log = api.NewLogger("main")
log = util.NewLogger("main")
cfgFile string
)

Expand Down Expand Up @@ -114,17 +114,6 @@ func Execute() {
}
}

func configureLogging() {
level := viper.GetString("log")

api.OutThreshold = api.LogLevelToThreshold(level)
api.LogThreshold = api.OutThreshold

api.Loggers(func(name string, logger *api.Logger) {
logger.SetStdoutThreshold(api.OutThreshold)
})
}

// checkVersion validates if updates are available
func checkVersion() {
githubTag := &latest.GithubTag{
Expand Down Expand Up @@ -161,12 +150,12 @@ func tee(in chan core.Param) (chan core.Param, <-chan core.Param) {
}

func run(cmd *cobra.Command, args []string) {
configureLogging()
util.LogLevel(viper.GetString("log"))
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)

// load config and re-configure logging after reading config file
conf := loadConfigFile(cfgFile)
configureLogging()
util.LogLevel(viper.GetString("log"))

go checkVersion()

Expand Down
Loading

0 comments on commit 1729399

Please sign in to comment.