-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
141 lines (118 loc) · 2.39 KB
/
controller.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
package go_hue_util
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"sync"
)
func NewAPI(hostname string, username string) *HueAPI {
return &HueAPI{
sync.Mutex{},
username,
"http://" + hostname + "/api/" + username,
&http.Client{},
}
}
type HueAPI struct {
lock sync.Mutex
username string
apiUrl string
client *http.Client
}
type Light struct {
State LightState
Type string
Name string
ModelId string
UniqueId string
}
/* Dump of an example Hue state
{
"state":{
"on":false,
"bri":254,
"hue":14956,
"sat":140,
"effect":"none",
"xy":[0.4571,0.4097],
"ct":366,
"alert":"none",
"colormode":"ct",
"reachable":true
}
*/
type LightState struct {
On *bool `json:"on"`
Bri *int `json:"bri"`
Hue *int `json:"hue"`
Sat *int `json:"sat"`
Effect *string `json:"effect"`
Alert *string `json:"alert"`
}
func Bool(b bool) *bool {
return &b
}
func Int(i int) *int {
return &i
}
func Str(s string) *string {
return &s
}
func (h *HueAPI) GetLights() (map[string]Light, error) {
resp, err := http.Get(h.apiUrl + "/lights")
if err != nil {
return nil, err
}
output := map[string]Light{}
err = unmarshalBody(resp.Body, &output)
return output, err
}
func (h *HueAPI) SetLightState(lightId string, state LightState) error {
output := []struct{
Success map[string]bool
}{}
url := h.apiUrl + "/lights/" + lightId + "/state"
err := h.put(url, "application/json", state, &output)
log.Println(output, err)
return err
}
func (h *HueAPI) SetGroupState(groupId string, state LightState) error {
output := []struct{
//Success map[string]bool
}{}
url := h.apiUrl + "/groups/" + groupId + "/action"
err := h.put(url, "application/json", state, &output)
log.Println("A", output, err)
return err
}
func (h *HueAPI) put(url string,
contentType string,
dataObj interface{},
output interface{}) error {
h.lock.Lock()
defer h.lock.Unlock()
data, err := json.Marshal(dataObj)
if err != nil {
return err
}
buf := bytes.NewBuffer(data)
req, err := http.NewRequest("PUT", url, buf)
log.Println(url, dataObj)
req.Header.Add("Content-type", contentType)
log.Println("PUT", url)
resp, err := h.client.Do(req)
if err != nil {
log.Println(err)
}
return unmarshalBody(resp.Body, output)
}
func unmarshalBody(body io.Reader, obj interface{}) error {
data, err := ioutil.ReadAll(body)
if err != nil {
return err
}
return json.Unmarshal(data, obj)
}