-
Notifications
You must be signed in to change notification settings - Fork 6
/
structure.go
223 lines (206 loc) · 5.79 KB
/
structure.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
package nest
import (
"bufio"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
/*
Structures returns a map of structures
https://developer.nest.com/documentation/api#structures
structures := client.Structures()
*/
func (c *Client) Structures() (map[string]*Structure, *APIError) {
resp, err := c.getStructures(NoStream)
if err != nil {
return nil, &APIError{
Error: "devices_error",
Description: err.Error(),
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, &APIError{
Error: "body_read_error",
Description: err.Error(),
}
}
if resp.StatusCode != 200 {
apiError := &APIError{}
json.Unmarshal(body, apiError)
return nil, apiError
}
structures := make(map[string]*Structure)
err = json.Unmarshal(body, &structures)
c.associateClientToStructures(structures)
return structures, nil
}
/*
Structures Stream emits events from the Nest structures REST streaming API
client.StructuresStream(func(event map[string]*Structure) {
fmt.Println(event)
})
*/
func (c *Client) StructuresStream(callback func(structures map[string]*Structure, err error)) {
c.setRedirectURL()
for {
c.streamStructures(callback)
}
}
/*
SetAway sets the away status of a structure
https://developer.nest.com/documentation/api#away
s.SetAway(nest.Away)
*/
func (s *Structure) SetAway(mode int) *APIError {
requestMode := make(map[string]string)
switch mode {
case Home:
requestMode["away"] = "home"
case Away:
requestMode["away"] = "away"
case AutoAway:
requestMode["away"] = "auto-away"
default:
return generateAPIError("Invalid Away requested - must be home, away or auto-away")
}
body, _ := json.Marshal(requestMode)
return s.setStructure(body)
}
/*
SetETA sets the ETA for the Nest API
https://developer.nest.com/documentation/eta-reference
*/
func (s *Structure) SetETA(tripID string, begin time.Time, end time.Time) *APIError {
apiErr := checkTimes(begin, end)
if apiErr != nil {
return apiErr
}
eta := &ETA{
TripID: tripID,
EstimatedArrivalWindowBegin: begin,
EstimatedArrivalWindowEnd: end,
}
data, _ := json.Marshal(eta)
req, _ := http.NewRequest("PUT", s.Client.RedirectURL+"/structures/"+s.StructureID+"/eta.json?auth="+s.Client.Token, bytes.NewBuffer(data))
resp, err := http.DefaultClient.Do(req)
if err != nil {
apiError := &APIError{
Error: "http_error",
Description: err.Error(),
}
return apiError
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
apiError := &APIError{}
json.Unmarshal(body, apiError)
apiError = generateAPIError(apiError.Error)
apiError.Status = resp.Status
apiError.StatusCode = resp.StatusCode
return apiError
}
return nil
}
// checkTimes ensure the times provided are set properly for the Nest API
func checkTimes(begin time.Time, end time.Time) *APIError {
if begin.Before(time.Now()) {
apiError := &APIError{
Error: "eta_error",
Description: "The begin time must be greater than the time now",
}
return apiError
}
if end.Before(begin) {
apiError := &APIError{
Error: "eta_error",
Description: "The end time must be greater than the begin time",
}
return apiError
}
return nil
}
// streamStructures connects to the stream, following the redirect and then watches the stream
func (c *Client) streamStructures(callback func(structures map[string]*Structure, err error)) {
resp, err := c.getStructures(Stream)
if err != nil {
callback(nil, err)
return
}
defer resp.Body.Close()
c.watchStructuresStream(resp, callback)
}
// watchStructuresStream grabs the data off the stream, parses them and invokes the callback
func (c *Client) watchStructuresStream(resp *http.Response, callback func(structures map[string]*Structure, err error)) {
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
if err != nil {
return
}
value := parseStreamData(line)
if value != "" {
structuresEvent := &StructuresEvent{}
json.Unmarshal([]byte(value), structuresEvent)
if structuresEvent.Data != nil {
c.associateClientToStructures(structuresEvent.Data)
callback(structuresEvent.Data, nil)
}
}
}
}
// getStructures does an HTTP get
func (c *Client) getStructures(action int) (*http.Response, error) {
if c.RedirectURL == "" {
req, _ := http.NewRequest("GET", c.APIURL+"/structures.json?auth="+c.Token, nil)
resp, err := http.DefaultClient.Do(req)
if resp.Request.URL != nil {
c.RedirectURL = resp.Request.URL.Scheme + "://" + resp.Request.URL.Host
}
return resp, err
}
req, _ := http.NewRequest("GET", c.RedirectURL+"/structures.json?auth="+c.Token, nil)
if action == Stream {
req.Header.Set("Accept", "text/event-stream")
}
resp, err := http.DefaultClient.Do(req)
return resp, err
}
// setStructure sends the request to the Nest REST API
func (s *Structure) setStructure(body []byte) *APIError {
url := s.Client.RedirectURL + "/structures/" + s.StructureID + "?auth=" + s.Client.Token
client := &http.Client{}
req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
apiError := &APIError{
Error: "http_error",
Description: err.Error(),
}
return apiError
}
body, _ = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if resp.StatusCode == 200 {
structure := &Structure{}
json.Unmarshal(body, structure)
return nil
}
apiError := &APIError{}
json.Unmarshal(body, apiError)
apiError = generateAPIError(apiError.Error)
apiError.Status = resp.Status
apiError.StatusCode = resp.StatusCode
return apiError
}
// associateClientToStructures ensures each structure knows its client details
func (c *Client) associateClientToStructures(structures map[string]*Structure) {
for _, value := range structures {
value.Client = c
}
}