-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
200 lines (155 loc) · 5.54 KB
/
rest.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
func (ctx *TadoContext) tadoGet(url string) ([]byte, error) {
res, err := ctx.tado.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(http.StatusText(res.StatusCode))
}
return ioutil.ReadAll(res.Body)
}
func (ctx *TadoContext) getTadoAccount() (ret TadoAccount, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/me")
if err != nil {
err = fmt.Errorf("GET /api/v2/me: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/me: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoHome(id int) (ret TadoHome, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(id))
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoZones(home TadoHome) (ret []TadoZone, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(home.ID) + "/zones")
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]/zones: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoZoneState(home TadoHome, zone TadoZone) (ret TadoZoneState, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(home.ID) + "/zones/" + strconv.Itoa(zone.ID) + "/state")
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]/zones/[zone]/state: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones/[zone]/state: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoAwayConfiguration(home TadoHome, zone TadoZone) (ret TadoAwayConfiguration, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(home.ID) + "/zones/" + strconv.Itoa(zone.ID) + "/awayConfiguration")
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]/zones/[zone]/awayConfiguration: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones/[zone]/awayConfiguration: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoActiveTimetable(home TadoHome, zone TadoZone) (ret TadoActiveTimetable, err error) {
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(home.ID) + "/zones/" + strconv.Itoa(zone.ID) + "/schedule/activeTimetable")
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]/zones/[zone]/schedule/activeTimetable: %w", err)
return
}
if err = json.Unmarshal(buf, &ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones/[zone]/schedule/activeTimetable: %w", err)
return
}
return
}
func (ctx *TadoContext) getTadoTimetableBlock(home TadoHome, zone TadoZone, timetable TadoActiveTimetable, instant time.Time) (ret TadoTimetableBlock, err error) {
loc, err := time.LoadLocation(home.DateTimeZone)
if err != nil {
err = fmt.Errorf("invalid home.DateTimeZone: %s", home.DateTimeZone)
return
}
instant = instant.In(loc)
day := TadoDayTypeMap[timetable.ID][instant.Weekday()]
buf, err := ctx.tadoGet("https://my.tado.com/api/v2/homes/" + strconv.Itoa(home.ID) + "/zones/" + strconv.Itoa(zone.ID) + "/schedule/timetables/" + strconv.Itoa(timetable.ID) + "/blocks/" + day)
if err != nil {
err = fmt.Errorf("GET /api/v2/homes/[home]/zones/[zone]/schedule/timetables/[timetable]/blocks/[day]: %w", err)
return
}
var blocks []TadoTimetableBlock
if err = json.Unmarshal(buf, &blocks); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones/[zone]/schedule/timetables/[timetable]/blocks/[day]: %w", err)
return
}
for _, block := range blocks {
start, _ := time.Parse("15:04", block.Start)
end, _ := time.Parse("15:04", block.End)
start = time.Date(instant.Year(), instant.Month(), instant.Day(), start.Hour(), start.Minute(), 0, 0, loc)
end = time.Date(instant.Year(), instant.Month(), instant.Day(), end.Hour(), end.Minute(), 0, 0, loc)
if start.After(end) {
end = end.AddDate(0, 0, 1)
}
if !start.After(instant) && instant.Before(end) {
return block, nil
}
}
return
}
func (ctx *TadoContext) putTadoOverlay(home TadoHome, zone TadoZone, overlay TadoOverlay) (TadoOverlay, error) {
var ret TadoOverlay
buf, err := json.Marshal(overlay)
if err != nil {
err = fmt.Errorf("encode overlay: %w", err)
return ret, err
}
req, err := http.NewRequest("PUT", "https://my.tado.com/api/v2/homes/"+strconv.Itoa(home.ID)+"/zones/"+strconv.Itoa(zone.ID)+"/overlay", bytes.NewReader(buf))
if err != nil {
err = fmt.Errorf("PUT /api/v2/homes/[home]/zones/[zone]/overlay: %w", err)
return ret, err
}
req.Header.Add("Content-Type", "application/json")
res, err := ctx.tado.Do(req)
if err != nil {
err = fmt.Errorf("PUT /api/v2/homes/[home]/zones/[zone]/overlay: %w", err)
return ret, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("PUT /api/v2/homes/[home]/zones/[zone]/overlay: %s", http.StatusText(res.StatusCode))
return ret, err
}
if err := json.NewDecoder(res.Body).Decode(&ret); err != nil {
err = fmt.Errorf("decode /api/v2/homes/[home]/zones/[zone]/overlay: %w", err)
return ret, err
}
return ret, nil
}