chore: refactor

This commit is contained in:
andig 2024-11-18 18:51:53 +01:00
parent b8b7135028
commit 3148c4fcf2
3 changed files with 9 additions and 10 deletions

View file

@ -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
}

View file

@ -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,

View file

@ -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)