-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
204 lines (170 loc) · 4.32 KB
/
lib.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/getcasa/plugin-philipshue/devices"
)
// Bridge define the physical Philips Hue bridge
type Bridge struct {
ID string
InternalIPAddress string
Username string
}
// DiscoverBridges list all bridge on local network
func DiscoverBridges() []Bridge {
res, err := http.Get("https://discovery.meethue.com/")
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
var bridges []Bridge
if err := json.Unmarshal(body, &bridges); err != nil {
fmt.Println(err)
}
return bridges
}
// User define token for requests
type User struct {
Username string
}
// UserResponse define user API response from bridge
type UserResponse struct {
Success User
}
// CreateUser register an user on bridge to authenticate requests
func (bridge *Bridge) CreateUser() string {
data := []byte(`{
"devicetype": "casa#plugin"
}`)
res, err := http.Post("http://"+bridge.InternalIPAddress+"/api", "application/json", bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
var responses []UserResponse
if err := json.Unmarshal(body, &responses); err != nil {
fmt.Println(err)
}
return responses[0].Success.Username
}
// GetLights list all lights from bridge
func (bridge *Bridge) GetLights() []devices.Hue {
res, err := http.Get("http://" + bridge.InternalIPAddress + "/api/" + bridge.Username + "/lights")
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
var result map[string]devices.Hue
json.Unmarshal([]byte(body), &result)
var lights []devices.Hue
for i := 1; i <= 50; i++ {
if result[strconv.Itoa(i)].Name != "" {
lights = append(lights, result[strconv.Itoa(i)])
}
}
return lights
}
// GetLight get state light from bridge
func (bridge *Bridge) GetLight(id int) devices.Hue {
res, err := http.Get("http://" + bridge.InternalIPAddress + "/api/" + bridge.Username + "/lights/" + strconv.Itoa(id))
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
var light devices.Hue
json.Unmarshal(body, &light)
return light
}
// GetLightID return int id of the light
func GetLightID(uid string) int {
for _, state := range States {
if state.Device.UniqueID == uid {
return state.DeviceID
}
}
return -1
}
// GetBridge return the bridge of the light
func GetBridge(uid string) Bridge {
for _, state := range States {
if state.Device.UniqueID == uid {
return state.Bridge
}
}
return Bridge{}
}
// SwitchLight send user params to the light
func (bridge *Bridge) SwitchLight(physicalID string, params Params) {
byteParams, err := json.Marshal(params)
if err != nil {
fmt.Println(err)
return
}
id := GetLightID(physicalID)
if id == -1 {
fmt.Println("Wrong id")
return
}
req, err := http.NewRequest(http.MethodPut, "http://"+bridge.InternalIPAddress+"/api/"+bridge.Username+"/lights/"+strconv.Itoa(id)+"/state", bytes.NewBuffer(byteParams))
if err != nil {
fmt.Println(err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
_, err = client.Do(req)
if err != nil {
fmt.Println(err)
}
return
}
// ToggleLight get the current state light to toggle on/off
func (bridge *Bridge) ToggleLight(physicalID string) {
id := GetLightID(physicalID)
if id == -1 {
fmt.Println("Wrong id")
return
}
light := bridge.GetLight(id)
on := !light.State.ON
lightReq := Params{
On: on,
Sat: light.State.Sat,
Bri: light.State.Bri,
Hue: light.State.Hue,
}
byteParams, err := json.Marshal(lightReq)
if err != nil {
fmt.Println(err)
return
}
req, err := http.NewRequest(http.MethodPut, "http://"+bridge.InternalIPAddress+"/api/"+bridge.Username+"/lights/"+strconv.Itoa(id)+"/state", bytes.NewBuffer(byteParams))
if err != nil {
fmt.Println(err)
}
fmt.Println("http://"+bridge.InternalIPAddress+"/api/"+bridge.Username+"/lights/"+strconv.Itoa(id)+"/state", bytes.NewBuffer(byteParams))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
_, err = client.Do(req)
if err != nil {
fmt.Println(err)
}
return
}