From 3148c4fcf21c00408b188aceea662cafc78274ce Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 18 Nov 2024 18:51:53 +0100 Subject: [PATCH] chore: refactor --- meter/zendure.go | 3 +-- meter/zendure/connection.go | 6 +++--- meter/zendure/credentials.go | 10 +++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/meter/zendure.go b/meter/zendure.go index fd7476effd..222dbdc7c5 100644 --- a/meter/zendure.go +++ b/meter/zendure.go @@ -33,8 +33,7 @@ func NewZendureFromConfig(other map[string]interface{}) (api.Meter, error) { return nil, err } - global := strings.ToUpper(cc.Region) != "EU" - conn, err := zendure.NewConnection(cc.Account, cc.Serial, global, cc.Timeout) + conn, err := zendure.NewConnection(strings.ToUpper(cc.Region), cc.Account, cc.Serial, cc.Timeout) if err != nil { return nil, err } diff --git a/meter/zendure/connection.go b/meter/zendure/connection.go index 47c447a080..8dce525d9d 100644 --- a/meter/zendure/connection.go +++ b/meter/zendure/connection.go @@ -22,7 +22,7 @@ type Connection struct { data *util.Monitor[Data] } -func NewConnection(account, serial string, global bool, timeout time.Duration) (*Connection, error) { +func NewConnection(region, account, serial string, timeout time.Duration) (*Connection, error) { mu.Lock() defer mu.Unlock() @@ -31,12 +31,12 @@ func NewConnection(account, serial string, global bool, timeout time.Duration) ( return conn, nil } - res, err := MqttCredentials(account, serial, global) + log := util.NewLogger("zendure") + res, err := MqttCredentials(log, region, account, serial) if err != nil { return nil, err } - log := util.NewLogger("zendure") client, err := mqtt.NewClient( log, net.JoinHostPort(res.Data.MqttUrl, strconv.Itoa(res.Data.Port)), res.Data.AppKey, res.Data.Secret, diff --git a/meter/zendure/credentials.go b/meter/zendure/credentials.go index 5c1ff21dd0..a02f6a882a 100644 --- a/meter/zendure/credentials.go +++ b/meter/zendure/credentials.go @@ -13,17 +13,17 @@ const ( GlobalCredentialsUri = "https://app.zendure.tech/v2/developer/api/apply" ) -func MqttCredentials(account, serial string, global bool) (CredentialsResponse, error) { - client := request.NewHelper(util.NewLogger("zendure")) +func MqttCredentials(log *util.Logger, region, account, serial string) (CredentialsResponse, error) { + client := request.NewHelper(log) data := CredentialsRequest{ SnNumber: serial, Account: account, } - uri := EUCredentialsUri - if global { - uri = GlobalCredentialsUri + uri := GlobalCredentialsUri + if region == "EU" { + uri = EUCredentialsUri } req, _ := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)