Skip to content

Commit

Permalink
Fix cached getter type not compatible and improve diagnostic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 30, 2020
1 parent 037cb43 commit 8d7cabf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
3 changes: 2 additions & 1 deletion charger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/andig/evcc/api"
)
Expand All @@ -16,7 +17,7 @@ type apiFunction string
func NewFromConfig(log *api.Logger, typ string, other map[string]interface{}) api.Charger {
var c api.Charger

switch typ {
switch strings.ToLower(typ) {
case "wallbe":
c = NewWallbeFromConfig(log, other)
case "phoenix":
Expand Down
15 changes: 11 additions & 4 deletions provider/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type CacheGetter struct {

// NewCacheGetter wraps a getter with a cache
func NewCacheGetter(getter interface{}, cache time.Duration) *CacheGetter {
if g, ok := getter.(func() (float64, error)); ok {
getter = FloatGetter(g)
}
if g, ok := getter.(func() (int64, error)); ok {
getter = IntGetter(g)
}

return &CacheGetter{
getter: getter,
cache: cache,
Expand All @@ -25,7 +32,7 @@ func (c *CacheGetter) FloatGetter() (float64, error) {
if time.Since(c.updated) > c.cache {
g, ok := c.getter.(FloatGetter)
if !ok {
log.FATAL.Fatal("invalid type")
log.FATAL.Fatalf("invalid type: %T", c.getter)
}

val, err := g()
Expand All @@ -45,7 +52,7 @@ func (c *CacheGetter) IntGetter() (int64, error) {
if time.Since(c.updated) > c.cache {
g, ok := c.getter.(IntGetter)
if !ok {
log.FATAL.Fatal("invalid type")
log.FATAL.Fatalf("invalid type: %T", c.getter)
}

val, err := g()
Expand All @@ -65,7 +72,7 @@ func (c *CacheGetter) StringGetter() (string, error) {
if time.Since(c.updated) > c.cache {
g, ok := c.getter.(StringGetter)
if !ok {
log.FATAL.Fatal("invalid type")
log.FATAL.Fatalf("invalid type: %T", c.getter)
}

val, err := g()
Expand All @@ -85,7 +92,7 @@ func (c *CacheGetter) BoolGetter() (bool, error) {
if time.Since(c.updated) > c.cache {
g, ok := c.getter.(BoolGetter)
if !ok {
log.FATAL.Fatal("invalid type")
log.FATAL.Fatalf("invalid type: %T", g)
}

val, err := g()
Expand Down
16 changes: 9 additions & 7 deletions push/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type telegramConfig struct {
Chats []int64
}

func init() {
tgbotapi.SetLogger(log.ERROR)
}

// NewTelegramMessenger creates new pushover messenger
func NewTelegramMessenger(token string, chats []int64) *Telegram {
bot, err := tgbotapi.NewBotAPI(token)
Expand Down Expand Up @@ -63,14 +67,12 @@ func (m *Telegram) trackChats() {
func (m *Telegram) Send(event Event, title, msg string) {
m.Lock()
for chat := range m.chats {
go func(chat int64) {
log.TRACE.Printf("telegram: sending to %d", chat)
log.TRACE.Printf("telegram: sending to %d", chat)

msg := tgbotapi.NewMessage(chat, msg)
if _, err := m.bot.Send(msg); err != nil {
log.ERROR.Print(err)
}
}(chat)
msg := tgbotapi.NewMessage(chat, msg)
if _, err := m.bot.Send(msg); err != nil {
log.ERROR.Print(err)
}
}
m.Unlock()
}
6 changes: 4 additions & 2 deletions vehicle/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package vehicle

import (
"strings"

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

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

switch typ {
case "script":
switch strings.ToLower(typ) {
case "script", "exec":
c = NewConfigurableFromConfig(log, other)
case "audi":
c = NewAudiFromConfig(log, other)
Expand Down

0 comments on commit 8d7cabf

Please sign in to comment.