Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Apr 5, 2021
1 parent bec3116 commit cfd190b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
71 changes: 35 additions & 36 deletions internal/vehicle/niu.go
Expand Up @@ -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() {
Expand Down Expand Up @@ -56,33 +57,17 @@ 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()

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
}
Expand All @@ -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()) {
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions internal/vehicle/niu/types.go
@@ -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
Expand Down

0 comments on commit cfd190b

Please sign in to comment.