From 5fc9b92f404e6ab666079ac1e09afcae57f0a763 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 7 Jun 2021 07:45:34 +0200 Subject: [PATCH] Hyundai/Kia: perform daily download of stamps (#1119) --- vehicle/bluelink/identity.go | 3 ++ vehicle/bluelink/stamps.go | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/vehicle/bluelink/identity.go b/vehicle/bluelink/identity.go index 140ecfbbb..29415eb0d 100644 --- a/vehicle/bluelink/identity.go +++ b/vehicle/bluelink/identity.go @@ -53,6 +53,9 @@ func NewIdentity(log *util.Logger, config Config) (*Identity, error) { config: config, } + // fetch updated stamps + updateStamps(log, config.CCSPApplicationID) + return v, nil } diff --git a/vehicle/bluelink/stamps.go b/vehicle/bluelink/stamps.go index 238d85de6..fedc008f4 100644 --- a/vehicle/bluelink/stamps.go +++ b/vehicle/bluelink/stamps.go @@ -3,8 +3,14 @@ package bluelink import ( "bufio" _ "embed" // embedded files + "fmt" "math/rand" "strings" + "sync" + "time" + + "github.com/andig/evcc/util" + "github.com/andig/evcc/util/request" ) //go:embed 693a33fa-c117-43f2-ae3b-61a02d24f417 @@ -21,6 +27,52 @@ var stamps = Stamps{ "99cfff84-f4e2-4be8-a5ed-e5b755eb6581": unpack(hyundai), } +var ( + mu sync.Mutex + updater map[string]struct{} = make(map[string]struct{}) + + client = request.NewHelper(util.NewLogger("http")) + brands = map[string]string{ + "693a33fa-c117-43f2-ae3b-61a02d24f417": "kia", + "99cfff84-f4e2-4be8-a5ed-e5b755eb6581": "hyundai", + } +) + +func download(log *util.Logger, id, brand string) { + var res []string + uri := fmt.Sprintf("https://raw.githubusercontent.com/neoPix/bluelinky-stamps/master/%s.json", brand) + + err := client.GetJSON(uri, &res) + if err != nil { + log.ERROR.Println(err) + return + } + + if len(res) < 100 { + return + } + + mu.Lock() + stamps[id] = res + mu.Unlock() +} + +// updateStamps updates stamps according to https://github.com/Hacksore/bluelinky/pull/144 +func updateStamps(log *util.Logger, id string) { + if _, ok := updater[id]; ok { + return + } + + updater[id] = struct{}{} + download(log, id, brands[id]) + + go func() { + for range time.NewTicker(24 * time.Hour).C { + download(log, id, brands[id]) + } + }() +} + func unpack(source string) (res []string) { scanner := bufio.NewScanner(strings.NewReader(source)) for scanner.Scan() { @@ -34,6 +86,9 @@ func unpack(source string) (res []string) { // New creates a new stamp func (s Stamps) New(id string) string { + mu.Lock() + defer mu.Unlock() + source, ok := s[id] if !ok { panic(id)