-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomely.go
190 lines (155 loc) · 4.53 KB
/
homely.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
package homely
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
"github.com/tokongs/homely/socketio"
"golang.org/x/oauth2"
)
const defaultBaseURL = "https://sdk.iotiliti.cloud"
type Client struct {
http http.Client
tokenSource oauth2.TokenSource
baseURL string
}
type Config struct {
Username string
Password string
BaseURL string
}
type Location struct {
Name string `json:"name"`
LocationID uuid.UUID `json:"locationId"`
UserID uuid.UUID `json:"userId"`
GatewaySerial string `json:"gatewayserial"`
PartnerCode int `json:"partnerCode"`
}
type LocationDetails struct {
LocationID uuid.UUID `json:"locationID"`
GatewaySerial string `json:"gatewayserial"`
Name string `json:"name"`
AlarmState string `json:"alarmState"`
UserRoleAtLocation string `json:"userRoleAtLocation"`
Devices []Device `json:"devices"`
}
type Device struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Location string `json:"location"`
Online bool `json:"online"`
ModelID uuid.UUID `json:"modelId"`
ModelName string `json:"modelName"`
Features map[string]Feature `json:"features"`
}
type Feature struct {
States map[string]State `json:"states"`
}
type State struct {
Value any `json:"value"`
LastUpdated time.Time `json:"lastUpdated"`
}
type Event struct {
Type string `json:"type"`
Data EventData `json:"data"`
}
type EventData struct {
DeviceID uuid.UUID `json:"deviceId"`
GatewayID uuid.UUID `json:"gatewayId"`
LocationID uuid.UUID `json:"locationId"`
ModelID uuid.UUID `json:"modelId"`
RootLocationID uuid.UUID `json:"rootLocationId"`
Changes []Change `json:"changes"`
PartnerCode int `json:"partnerCode"`
}
type Change struct {
Feature string `json:"feature"`
StateName string `json:"stateName"`
Value any `json:"value"`
LastUpdated time.Time `json:"lastUpdated"`
}
func New(c Config) *Client {
if c.BaseURL == "" {
c.BaseURL = defaultBaseURL
}
ts := oauth2.ReuseTokenSource(nil, &tokenSource{
baseURL: c.BaseURL,
username: c.Username,
password: c.Password,
})
return &Client{
tokenSource: ts,
http: *oauth2.NewClient(context.Background(), ts),
baseURL: c.BaseURL,
}
}
func (c *Client) Locations(ctx context.Context) ([]Location, error) {
req, err := c.newRequest(ctx, http.MethodGet, "homely/locations", nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
l := []Location{}
if err := c.doAndDecode(req, &l); err != nil {
return nil, err
}
return l, nil
}
func (c *Client) LocationDetails(ctx context.Context, locationID uuid.UUID) (LocationDetails, error) {
var l LocationDetails
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("homely/home/%s", locationID), nil)
if err != nil {
return l, fmt.Errorf("new request: %w", err)
}
if err := c.doAndDecode(req, &l); err != nil {
return l, err
}
return l, nil
}
func (c *Client) Stream(ctx context.Context, locationID uuid.UUID, h func(e Event)) error {
sio := socketio.New(fmt.Sprintf("%s/socket.io/?locationId=%s", c.baseURL, locationID), c.tokenSource)
return sio.HandleEvents(ctx, func(name, msg string) error {
if name != "event" {
slog.Warn("Got non event event", "name", name)
return nil
}
var e Event
if err := json.Unmarshal([]byte(msg), &e); err != nil {
return fmt.Errorf("unmarshal event: %w", err)
}
h(e)
return nil
})
}
func (c *Client) newRequest(ctx context.Context, method string, resource string, body io.Reader) (*http.Request, error) {
path, err := url.JoinPath(c.baseURL, resource)
if err != nil {
return nil, fmt.Errorf("join path: %w", err)
}
req, err := http.NewRequestWithContext(ctx, method, path, body)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application-json")
return req, nil
}
func (c *Client) doAndDecode(req *http.Request, v any) error {
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
msg, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("request failed and so did reading the body: %w", err)
}
return fmt.Errorf(string(msg))
}
return json.NewDecoder(resp.Body).Decode(v)
}