-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.go
175 lines (144 loc) · 4.55 KB
/
light.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
package hue
import (
"context"
"errors"
"net/http"
"strconv"
funk "github.com/thoas/go-funk"
)
type SetStateParams struct {
On *bool `json:"on,omitempty"`
Bri *uint8 `json:"bri,omitempty"`
Hue *uint16 `json:"hue,omitempty"`
Sat *uint8 `json:"sat,omitempty"`
Effect *string `json:"effect,omitempty"`
XY []float64 `json:"xy,omitempty"`
CT *uint16 `json:"ct,omitempty"`
Alert *string `json:"alert,omitempty"`
TransitionTime *uint16 `json:"transitiontime,omitempty"`
BriInc *uint8 `json:"bri_inc,omitempty"`
SatInc *uint8 `json:"sat_inc,omitempty"`
HueInc *uint16 `json:"hue_inc,omitempty"`
CtInc *uint16 `json:"ct_inc,omitempty"`
XYInc []float32 `json:"xy_inc,omitempty"`
Scene *string `json:"scene,omitempty"`
}
// LightService has functions for groups
type LightService service
const lightServiceName = "lights"
func (s *LightService) lightServicePath(params ...string) string {
return s.client.path(lightServiceName, params...)
}
// GetAll returns a list of all lights that have been discovered by the bridge.
func (s *LightService) GetAll(ctx context.Context) ([]Light, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, s.lightServicePath(), nil)
if err != nil {
return nil, nil, err
}
var lights map[string]Light
resp, err := s.client.do(ctx, req, &lights)
if err != nil {
return nil, resp, err
}
for k, l := range lights {
id, _ := strconv.Atoi(k)
l.ID = id
lights[k] = l
}
return funk.Values(lights).([]Light), resp, nil
}
// Get returns light by id
func (s *LightService) Get(ctx context.Context, id string) (*Light, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, s.lightServicePath(id), nil)
if err != nil {
return nil, nil, err
}
light := new(Light)
resp, err := s.client.do(ctx, req, light)
if err != nil {
return nil, resp, err
}
return light, resp, nil
}
// GetNew returns a list of lights that were discovered the last time a search for new lights was performed.
func (s *LightService) GetNew(ctx context.Context) (map[string]string, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, s.lightServicePath("new"), nil)
if err != nil {
return nil, nil, err
}
var parsed map[string]interface{}
resp, err := s.client.do(ctx, req, &parsed)
if err != nil {
return nil, resp, err
}
lights := make(map[string]string)
for k, l := range parsed {
if k == "lastscan" {
// Skip lastscan
} else {
lights[k] = l.(map[string]interface{})["name"].(string)
}
}
return lights, resp, nil
}
// Search starts searching for new lights
// The bridge will open the network for 40s.
func (s *LightService) Search(ctx context.Context) (*Response, error) {
req, err := s.client.newRequest(http.MethodPost, s.lightServicePath(), nil)
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)
}
return resp, nil
}
// Rename lights
func (s *LightService) Rename(ctx context.Context, id, name string) (*Response, error) {
var payload = struct {
Name string `json:"name"`
}{name}
req, err := s.client.newRequest(http.MethodPut, s.lightServicePath(id), 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)
}
return resp, nil
}
// SetState allows the user to turn the light on and off, modify the hue and effects.
func (s *LightService) SetState(ctx context.Context, id string, payload SetStateParams) ([]ApiResponse, *Response, error) {
req, err := s.client.newRequest(http.MethodPut, s.lightServicePath(id, "state"), 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 a light from the bridge.
func (s *LightService) Delete(ctx context.Context, id string) (*Response, error) {
req, err := s.client.newRequest(http.MethodDelete, s.lightServicePath(id), nil)
if err != nil {
return nil, err
}
resp, err := s.client.do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}