Minor cleanup

This commit is contained in:
andig 2021-04-05 22:05:31 +02:00
parent bec3116368
commit cfd190bc17
2 changed files with 37 additions and 38 deletions

View file

@ -20,10 +20,11 @@ import (
type Niu struct {
*embed
*request.Helper
user, password, sn string
tokens niu.Token
accessTokenExpiry time.Time
chargeStateG func() (float64, error)
user, password string
serial string
tokens niu.Token
accessTokenExpiry time.Time
chargeStateG func() (float64, error)
}
func init() {
@ -56,7 +57,7 @@ func NewNiuFromConfig(other map[string]interface{}) (api.Vehicle, error) {
Helper: request.NewHelper(log),
user: cc.User,
password: cc.Password,
sn: strings.ToUpper(cc.Serial),
serial: strings.ToUpper(cc.Serial),
}
v.chargeStateG = provider.NewCached(v.chargeState, cc.Cache).FloatGetter()
@ -64,25 +65,9 @@ func NewNiuFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return v, nil
}
// SoC implements the api.Vehicle interface
func (v *Niu) SoC() (float64, error) {
return v.chargeStateG()
}
// chargeState implements the api.Vehicle interface
func (v *Niu) chargeState() (float64, error) {
var resp niu.SoC
req, err := v.request(niu.API + "/v3/motor_data/index_info?sn=" + v.sn)
if err == nil {
err = v.DoJSON(req, &resp)
}
return float64(resp.Data.Batteries.CompartmentA.BatteryCharging), err
}
// login implements the Niu oauth2 api
func (v *Niu) login() error {
md5hash, err := getMD5Hash(v.password)
md5hash, err := md5Hash(v.password)
if err != nil {
return err
}
@ -95,23 +80,29 @@ func (v *Niu) login() error {
"app_id": []string{"niu_8xt1afu6"},
}
uri := niu.Auth + "/v3/api/oauth2/token"
uri := niu.AuthURI + "/v3/api/oauth2/token"
req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
})
if err != nil {
return err
}
var tokens niu.Token
if err = v.DoJSON(req, &tokens); err == nil {
v.tokens = tokens
v.accessTokenExpiry = time.Unix(v.tokens.Data.Token.TokenExpiresIn, 0)
if err == nil {
var tokens niu.Token
if err = v.DoJSON(req, &tokens); err == nil {
v.tokens = tokens
v.accessTokenExpiry = time.Unix(v.tokens.Data.Token.TokenExpiresIn, 0)
}
}
return err
}
// md5Hash creates a MD5 hash based on a string
func md5Hash(text string) (string, error) {
hasher := md5.New()
_, err := hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil)), err
}
// request implements the Niu web request
func (v *Niu) request(uri string) (*http.Request, error) {
if v.tokens.Data.Token.AccessToken == "" || v.accessTokenExpiry.Before(time.Now()) {
@ -127,11 +118,19 @@ func (v *Niu) request(uri string) (*http.Request, error) {
return req, err
}
// getMD5Hash creates a MD5 hash based on a string
func getMD5Hash(text string) (string, error) {
hasher := md5.New()
if _, err := hasher.Write([]byte(text)); err != nil {
return "", err
// chargeState implements the api.Vehicle interface
func (v *Niu) chargeState() (float64, error) {
var resp niu.SoC
req, err := v.request(niu.ApiURI + "/v3/motor_data/index_info?sn=" + v.serial)
if err == nil {
err = v.DoJSON(req, &resp)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
return float64(resp.Data.Batteries.CompartmentA.BatteryCharging), err
}
// SoC implements the api.Vehicle interface
func (v *Niu) SoC() (float64, error) {
return v.chargeStateG()
}

View file

@ -1,8 +1,8 @@
package niu
const (
Auth = "https://account-fk.niu.com"
API = "https://app-api-fk.niu.com"
AuthURI = "https://account-fk.niu.com"
ApiURI = "https://app-api-fk.niu.com"
)
// Token is the Niu oauth2 api response