-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
197 lines (160 loc) · 4.85 KB
/
group.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
package hue
import (
"context"
"errors"
"net/http"
"strconv"
funk "github.com/thoas/go-funk"
)
// GroupService has functions for groups
type GroupService service
const groupServiceName = "groups"
const groupTypeLight = "LightGroup"
const groupTypeRoom = "Room"
type createGroupRequest struct {
Lights []string `json:"lights,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Class *string `json:"class,omitempty"`
}
type updateGroupRequest struct {
Lights []string `json:"lights,omitempty"`
Name *string `json:"name,omitempty"`
Class *string `json:"class,omitempty"`
}
func (s *GroupService) groupServicePath(params ...string) string {
return s.client.path(groupServiceName, params...)
}
// GetAll returns all groups
func (s *GroupService) GetAll(ctx context.Context) ([]Group, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, s.groupServicePath(), nil)
if err != nil {
return nil, nil, err
}
var groups map[string]Group
resp, err := s.client.do(ctx, req, &groups)
if err != nil {
return nil, resp, err
}
for i, g := range groups {
id, _ := strconv.Atoi(i)
g.ID = id
}
return funk.Values(groups).([]Group), resp, nil
}
// CreateGroup creates light group and returns id of the created group
func (s *GroupService) CreateGroup(ctx context.Context, name string, lights []string) (string, *Response, error) {
payload := &createGroupRequest{
Name: name,
Lights: lights,
Type: groupTypeLight,
}
req, err := s.client.newRequest(http.MethodPost, s.groupServicePath(), payload)
if err != nil {
return "", nil, err
}
var apiResponses []ApiResponse
resp, err := s.client.do(ctx, req, &apiResponses)
if err != nil {
return "", resp, err
}
if len(apiResponses) == 0 || (apiResponses)[0].Error != nil {
return "", resp, errors.New((apiResponses)[0].Error.Description)
}
// Get first success message
successResponse := (apiResponses)[0].Success
return successResponse["id"].(string), resp, nil
}
// CreateGroup creates light room and returns id of the room
func (s *GroupService) CreateRoom(ctx context.Context, name string, lights []string) (string, *Response, error) {
payload := &createGroupRequest{
Name: name,
Lights: lights,
Type: groupTypeRoom,
Class: String(name),
}
req, err := s.client.newRequest(http.MethodPost, s.groupServicePath(), payload)
if err != nil {
return "", nil, err
}
var apiResponses []ApiResponse
resp, err := s.client.do(ctx, req, &apiResponses)
if err != nil {
return "", resp, err
}
if len(apiResponses) == 0 || (apiResponses)[0].Error != nil {
return "", resp, errors.New((apiResponses)[0].Error.Description)
}
// Get first success message
successResponse := (apiResponses)[0].Success
return successResponse["id"].(string), resp, nil
}
// Get returns the group by id
func (s *GroupService) Get(ctx context.Context, id string) (*Group, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, s.groupServicePath(id), nil)
if err != nil {
return nil, nil, err
}
group := new(Group)
resp, err := s.client.do(ctx, req, group)
if err != nil {
return nil, resp, err
}
return group, resp, nil
}
// Update updates group by id
func (s *GroupService) Update(ctx context.Context, id string, name *string, lights []string, class *string) (bool, *Response, error) {
payload := &updateGroupRequest{
Name: name,
Lights: lights,
Class: class,
}
req, err := s.client.newRequest(http.MethodPut, s.groupServicePath(id), payload)
if err != nil {
return false, nil, err
}
var apiResponses []ApiResponse
resp, err := s.client.do(ctx, req, &apiResponses)
if err != nil {
return false, resp, err
}
if len(apiResponses) == 0 {
return false, resp, errors.New("the bridge didn't return valid response")
}
// If response has any error, return as fail
for _, r := range apiResponses {
if r.Error != nil {
return false, resp, errors.New(r.Error.Description)
}
}
return true, resp, nil
}
// SetState updates state of the group
func (s *GroupService) SetState(ctx context.Context, id string, payload SetStateParams) ([]ApiResponse, *Response, error) {
req, err := s.client.newRequest(http.MethodPut, s.groupServicePath(id, "action"), payload)
if err != nil {
return nil, nil, err
}
var apiResponses []ApiResponse
resp, err := s.client.do(ctx, req, &apiResponses)
if err != nil {
return nil, resp, err
}
return apiResponses, resp, nil
}
// Delete removes the group
func (s *GroupService) Delete(ctx context.Context, id string) (*Response, error) {
req, err := s.client.newRequest(http.MethodDelete, s.groupServicePath(id), nil)
if err != nil {
return nil, err
}
var apiResponses []map[string]string
resp, err := s.client.do(ctx, req, &apiResponses)
if err != nil {
return resp, err
}
if len(apiResponses) == 0 {
return resp, errors.New("the bridge didn't return valid response")
}
return resp, nil
}