-
Notifications
You must be signed in to change notification settings - Fork 0
/
daikin.go
281 lines (213 loc) · 6.65 KB
/
daikin.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package daikin
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
)
type Daikin struct {
Email string
Password string
tokenCache *Token
tokenExpiresAt time.Time
httpClient *http.Client
urlBase string
}
type SetTempParams struct {
CoolSetpoint float32
HeatSetpoint float32
}
func New(email string, password string) *Daikin {
d := Daikin{
Email: email,
Password: password,
httpClient: &http.Client{Timeout: 10 * time.Second},
urlBase: "https://api.daikinskyport.com",
}
return &d
}
func (d *Daikin) getToken() (string, error) {
if d.tokenCache != nil && time.Now().Before(d.tokenExpiresAt) {
return d.tokenCache.AccessToken, nil
}
body := []byte(`{
"email": "` + d.Email + `",
"password": "` + d.Password + `"
}`)
r, err := http.NewRequest("POST", d.urlBase+"/users/auth/login", bytes.NewBuffer(body))
if err != nil {
return "", errors.New("http.NewRequest failed")
}
r.Header.Add("content-type", "application/json")
res, err := d.httpClient.Do(r)
if err != nil {
return "", errors.New("http request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("token request returned a non-success response: %s", res.Status)
}
token := &Token{}
derr := json.NewDecoder(res.Body).Decode(token)
if derr != nil {
return "", errors.New("json decode failed")
}
d.tokenCache = token
d.tokenExpiresAt = time.Now().Add(time.Duration(token.AccessTokenExpiresIn) * time.Second)
return token.AccessToken, nil
}
func (d *Daikin) GetDevices() (*Devices, error) {
r, err := http.NewRequest("GET", d.urlBase+"/devices", nil)
if err != nil {
return nil, errors.New("http.NewRequest failed")
}
r.Header.Add("content-type", "application/json")
token, err := d.getToken()
if err != nil {
return nil, errors.New("getToken did not return a token")
}
r.Header.Add("Authorization", "Bearer "+token)
res, err := d.httpClient.Do(r)
if err != nil {
return nil, errors.New("http request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("get devices request returned a non-success response: %s", res.Status)
}
var devices Devices
derr := json.NewDecoder(res.Body).Decode(&devices)
if derr != nil {
return nil, errors.New("json decode failed")
}
return &devices, nil
}
func (d *Daikin) GetDeviceInfo(deviceId string) (*DeviceInfo, error) {
r, err := http.NewRequest("GET", d.urlBase+"/deviceData/"+deviceId, nil)
if err != nil {
return nil, errors.New("http.NewRequest failed")
}
r.Header.Add("content-type", "application/json")
token, err := d.getToken()
if err != nil {
return nil, errors.New("getToken did not return a token")
}
r.Header.Add("Authorization", "Bearer "+token)
res, err := d.httpClient.Do(r)
if err != nil {
return nil, errors.New("http request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("get device info request returned a non-success response: %s", res.Status)
}
var deviceInfo DeviceInfo
derr := json.NewDecoder(res.Body).Decode(&deviceInfo)
if derr != nil {
return nil, errors.New("json decode failed")
}
return &deviceInfo, nil
}
func (d *Daikin) SetMode(deviceId string, mode Mode) error {
data := map[string]interface{}{"mode": mode}
json, err := json.Marshal(data)
if err != nil {
return errors.New("json marshal failed")
}
return d.updateDevice(deviceId, json)
}
func (d *Daikin) SetFanMode(deviceId string, fan_mode FanCirculate) error {
data := map[string]interface{}{"fanCirculate": fan_mode}
json, err := json.Marshal(data)
if err != nil {
return errors.New("json marshal failed")
}
return d.updateDevice(deviceId, json)
}
func (d *Daikin) SetFanSpeed(deviceId string, fan_speed FanCirculateSpeed) error {
data := map[string]interface{}{"fanCirculateSpeed": fan_speed}
json, err := json.Marshal(data)
if err != nil {
return errors.New("json marshal failed")
}
return d.updateDevice(deviceId, json)
}
func (d *Daikin) SetFanClean(deviceId string, fan_clean_active bool) error {
data := map[string]interface{}{"oneCleanFanActive": fan_clean_active}
json, err := json.Marshal(data)
if err != nil {
return errors.New("json marshal failed")
}
return d.updateDevice(deviceId, json)
}
func (d *Daikin) SetTemp(deviceId string, params SetTempParams) error {
if params.CoolSetpoint == params.HeatSetpoint {
return errors.New("invalid setpoints provided")
}
if params.CoolSetpoint != 0 && params.HeatSetpoint != 0 &&
params.CoolSetpoint < params.HeatSetpoint {
return errors.New("cool setpoint can not be lower than heat setpoint")
}
deviceInfo, err := d.GetDeviceInfo(deviceId)
if err != nil {
return errors.New("get device info failed")
}
if params.CoolSetpoint == 0 {
// hsp provided, default csp
params.CoolSetpoint = deviceInfo.CspHome
if (params.CoolSetpoint - params.HeatSetpoint) < deviceInfo.TempDeltaMin {
// min delta not met, increase csp
params.CoolSetpoint = params.HeatSetpoint + deviceInfo.TempDeltaMin
}
}
if params.HeatSetpoint == 0 {
// csp provided, default hsp
params.HeatSetpoint = deviceInfo.HspHome
if (params.CoolSetpoint - params.HeatSetpoint) < deviceInfo.TempDeltaMin {
// min delta not met, lower hsp
params.HeatSetpoint = params.CoolSetpoint - deviceInfo.TempDeltaMin
}
}
if params.CoolSetpoint < deviceInfo.TempSPMin || params.CoolSetpoint > deviceInfo.TempSPMax ||
params.HeatSetpoint < deviceInfo.TempSPMin || params.HeatSetpoint > deviceInfo.TempSPMax {
return errors.New("setpoint(s) outside of allowable range")
}
data := map[string]interface{}{
"cspHome": params.CoolSetpoint,
"hspHome": params.HeatSetpoint,
"schedOverride": 1,
}
json, err := json.Marshal(data)
if err != nil {
return errors.New("json marshal failed")
}
log.Println(string(json[:]))
return d.updateDevice(deviceId, json)
}
func (d *Daikin) UpdateDeviceRaw(deviceId string, json string) error {
return d.updateDevice(deviceId, []byte(json))
}
func (d *Daikin) updateDevice(deviceId string, body []byte) error {
r, err := http.NewRequest("PUT", d.urlBase+"/deviceData/"+deviceId, bytes.NewBuffer(body))
if err != nil {
return errors.New("http.NewRequest failed")
}
r.Header.Add("content-type", "application/json")
token, err := d.getToken()
if err != nil {
return errors.New("getToken did not return a token")
}
r.Header.Add("Authorization", "Bearer "+token)
res, err := d.httpClient.Do(r)
if err != nil {
return errors.New("http request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("update request returned a non-success response: %s", res.Status)
}
return nil
}