forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniu.go
157 lines (127 loc) Β· 3.36 KB
/
niu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package vehicle
import (
"crypto/md5"
"encoding/hex"
"errors"
"net/http"
"net/url"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/niu"
)
// Niu is an api.Vehicle implementation for Niu vehicles
type Niu struct {
*embed
*request.Helper
user, password string
serial string
token niu.Token
apiG func() (niu.Response, error)
}
func init() {
registry.Add("niu", NewNiuFromConfig)
}
// NewFordFromConfig creates a new vehicle
func NewNiuFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, Serial string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
if cc.Serial == "" {
return nil, errors.New("missing serial")
}
log := util.NewLogger("niu").Redact(cc.User, cc.Password)
v := &Niu{
embed: &cc.embed,
Helper: request.NewHelper(log),
user: cc.User,
password: cc.Password,
serial: strings.ToUpper(cc.Serial),
}
v.apiG = provider.Cached(v.batteryAPI, cc.Cache)
return v, nil
}
// login implements the Niu oauth2 api
func (v *Niu) login() error {
hash := md5.New()
hash.Write([]byte(v.password))
md5hash := hex.EncodeToString(hash.Sum(nil))
data := url.Values{
"account": []string{v.user},
"password": []string{md5hash},
"grant_type": []string{"password"},
"scope": []string{"base"},
"app_id": []string{"niu_8xt1afu6"},
}
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 {
var token niu.Token
if err = v.DoJSON(req, &token); err == nil {
v.token = token
}
}
return err
}
// request implements the Niu web request
func (v *Niu) request(uri string) (*http.Request, error) {
if v.token.AccessToken == "" || time.Until(v.token.Expiry) < time.Minute {
if err := v.login(); err != nil {
return nil, err
}
}
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"token": v.token.AccessToken,
})
return req, err
}
// batteryAPI provides battery api response
func (v *Niu) batteryAPI() (niu.Response, error) {
var res niu.Response
req, err := v.request(niu.ApiURI + "/v3/motor_data/index_info?sn=" + v.serial)
if err == nil {
err = v.DoJSON(req, &res)
}
return res, err
}
// Soc implements the api.Vehicle interface
func (v *Niu) Soc() (float64, error) {
res, err := v.apiG()
return float64(res.Data.Batteries.CompartmentA.BatteryCharging), err
}
var _ api.ChargeState = (*Niu)(nil)
// Status implements the api.ChargeState interface
func (v *Niu) Status() (api.ChargeStatus, error) {
status := api.StatusA // disconnected
res, err := v.apiG()
if err == nil {
if res.Data.IsConnected {
status = api.StatusB
}
if res.Data.IsCharging > 0 {
status = api.StatusC
}
}
return status, err
}
var _ api.VehicleRange = (*Niu)(nil)
// Range implements the api.VehicleRange interface
func (v *Niu) Range() (int64, error) {
res, err := v.apiG()
return res.Data.EstimatedMileage, err
}