-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
171 lines (139 loc) · 3.38 KB
/
api.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package goe
import (
"errors"
"fmt"
"strings"
"time"
"github.com/thommyho/robotui/util"
"github.com/thommyho/robotui/util/request"
)
const CloudURI = "https://api.go-e.co"
// Response is the v1 and v2 api response interface
type Response interface {
Status() int
Enabled() bool
CurrentPower() float64
ChargedEnergy() float64
TotalEnergy() float64
Currents() (float64, float64, float64)
Voltages() (float64, float64, float64)
Identify() string
}
type UpdateResponse map[string]interface{}
type API interface {
IsV2() bool
Status() (Response, error)
Update(payload string) error
}
type LocalAPI struct {
*request.Helper
uri string
v2 bool
status Response
updated time.Time
cache time.Duration
}
var _ API = (*LocalAPI)(nil)
func NewLocal(log *util.Logger, uri string, cache time.Duration) *LocalAPI {
uri = strings.TrimRight(uri, "/")
uri = strings.TrimSuffix(uri, "/api")
api := &LocalAPI{
Helper: request.NewHelper(log),
uri: uri,
cache: cache,
}
api.upgradeV2()
return api
}
// upgradeV2 will switch to use the v2 api and revert if not available
func (c *LocalAPI) upgradeV2() {
c.v2 = true // use v2 response struct
res := new(StatusResponse2)
err := c.response("api/status?filter=alw", &res)
if err == nil {
c.uri = c.uri + "/api"
} else {
c.v2 = false
}
}
// IsV2 returns v2 api usage
func (c *LocalAPI) IsV2() bool {
return c.v2
}
// response returns a v1/v2 api response
func (c *LocalAPI) response(partial string, res interface{}) error {
url := fmt.Sprintf("%s/%s", c.uri, partial)
return c.GetJSON(url, &res)
}
// Status reads a v1/v2 api response
func (c *LocalAPI) Status() (res Response, err error) {
if time.Since(c.updated) > c.cache {
if c.v2 {
c.status = new(StatusResponse2)
err = c.response("status?filter=alw,car,eto,nrg,wh,trx,cards", &c.status)
} else {
c.status = new(StatusResponse)
err = c.response("status", &c.status)
}
if err == nil {
c.updated = time.Now()
}
}
return c.status, err
}
// Update executes a v1/v2 api update and returns the response
func (c *LocalAPI) Update(payload string) error {
c.updated = time.Time{}
res := new(UpdateResponse)
if c.v2 {
err := c.response(fmt.Sprintf("set?%s", payload), &res)
return err
}
err := c.response(fmt.Sprintf("mqtt?payload=%s", payload), &res)
return err
}
type cloud struct {
*request.Helper
token string
cache time.Duration
updated time.Time
status Response
}
var _ API = (*cloud)(nil)
func NewCloud(log *util.Logger, token string, cache time.Duration) API {
return &cloud{
Helper: request.NewHelper(log),
token: token,
cache: cache,
}
}
func (c *cloud) IsV2() bool {
return false
}
func (c *cloud) response(function, payload string) (*StatusResponse, error) {
var status CloudResponse
url := fmt.Sprintf("%s/%s?token=%s", CloudURI, function, c.token)
if payload != "" {
url += "&payload=" + payload
}
err := c.GetJSON(url, &status)
if err == nil && status.Success != nil && !*status.Success {
err = errors.New(status.Error)
}
return &status.Data, err
}
func (c *cloud) Status() (status Response, err error) {
if time.Since(c.updated) >= c.cache {
status, err = c.response("api_status", "")
if err == nil {
c.updated = time.Now()
c.status = status
}
}
return c.status, err
}
func (c *cloud) Update(payload string) error {
c.updated = time.Time{}
_, err := c.response("api", payload)
return err
}