-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
172 lines (142 loc) · 3.78 KB
/
bridge.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
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type Bridge struct {
XMLName xml.Name `xml:"root"`
BaseUrl string `xml:"URLBase"`
DeviceName string `xml:"device>modelName"`
DeviceModel string `xml:"device>modelNumber"`
SerialNumber string `xml:"device>serialNumber"`
}
type RegisterUser struct {
//Username string `json:"username"`
DeviceName string `json:"devicetype"`
}
type Success struct {
Username string `json:"username,omitempty"`
}
type Error struct {
ErrorType int `json:"type,omitempty"`
ErrorAddress string `json:"address,omitempty"`
ErrorDescription string `json:"description,omitempty"`
}
type Response struct {
Success Success `json:"success,omitempty"`
Error Error `json:"error,omitempty"`
}
type ApiResponse []Response
type Light struct {
Config struct {
Type string `json:"archetype"`
} `json:"config"`
Name string `json:"name"`
}
type Lights map[string]Light
type LightState struct {
On bool `json:"on"`
Brightness uint8 `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Saturation uint8 `json:"sat,omitempty"`
Coordinates []float64 `json:"xy,omitempty"`
ColorTemperature uint16 `json:"ct,omitempty"`
TransitionTime uint16 `json:"transitiontime,omitempty"`
}
func (resp ApiResponse) HasErrors() bool {
hasErrors := false
for _, r := range resp {
if r.Error.ErrorDescription != "" {
hasErrors = true
break
}
}
return hasErrors
}
func (resp ApiResponse) PrintErrors() {
for _, r := range resp {
if r.Error.ErrorDescription != "" {
log.Printf("hue: bridge: %s\n", r.Error.ErrorDescription)
}
}
}
// RegisterUser registers a new user (username randomly generated as per hue
// api definition) and returns the username it is to be used for creating an
// developer interface on the hue bridge; and generally should be called as
// one of the first things in the execution (for any API request)
func (b Bridge) RegisterUser(deviceName string) string {
body, err := json.Marshal(RegisterUser{deviceName})
if err != nil {
logError(err.Error())
}
resp, err := http.Post(b.BaseUrl+"api", "application/json", bytes.NewBuffer(body))
if err != nil {
logError(err.Error())
}
defer resp.Body.Close()
respBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
logError(readErr.Error())
}
var apiResp ApiResponse
err = json.Unmarshal(respBody, &apiResp)
if err != nil {
logError(err.Error())
}
if !apiResp.HasErrors() {
return apiResp[0].Success.Username
} else {
apiResp.PrintErrors()
}
return ""
}
func (b Bridge) AllLights(username string) Lights {
resp, err := http.Get(b.BaseUrl + "api/" + username + "/lights")
if err != nil {
logError(err.Error())
}
defer resp.Body.Close()
respBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
logError(readErr.Error())
}
var lights Lights
err = json.Unmarshal(respBody, &lights)
if err != nil {
logError(err.Error())
}
return lights
}
func (b Bridge) SetLightState(username string, id string, state LightState) {
body, bodyErr := json.Marshal(state)
if bodyErr != nil {
logError(bodyErr.Error())
}
fmt.Printf("hue: set light '%s'\n", id)
req, reqErr := http.NewRequest(http.MethodPut, b.BaseUrl+"api/"+username+"/lights/"+id+"/state", bytes.NewBuffer(body))
if reqErr != nil {
logError(reqErr.Error())
}
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
logError(respErr.Error())
}
defer resp.Body.Close()
respBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
logError(readErr.Error())
}
var apiResp ApiResponse
err := json.Unmarshal(respBody, &apiResp)
if err != nil {
logError(err.Error())
}
if apiResp.HasErrors() {
apiResp.PrintErrors()
}
}