-
Notifications
You must be signed in to change notification settings - Fork 24
/
light.go
157 lines (139 loc) · 3.9 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
package hue
import (
"bytes"
"encoding/json"
"strconv"
)
// Light - encapsulates the controls for a specific philips hue light
type Light struct {
Id string
Name string
bridge *Bridge
}
type LightState struct {
Hue int `json:"hue"`
On bool `json:"on"`
Effect string `json:"effect"`
Alert string `json:"effect"`
Bri int `json:"bri"`
Sat int `json:"sat"`
Ct int `json:"ct"`
Xy []float32 `json:"xy"`
Reachable bool `json:"reachable"`
ColorMode string `json:"colormode"`
}
type SetLightState struct {
On string
Bri string
Hue string
Sat string
Xy []float32
Ct string
Alert string
Effect string
TransitionTime string
}
type LightAttributes struct {
State LightState `json:"state"`
Type string `json:"type"`
Name string `json:"name"`
ModelId string `json:"modelid"`
SoftwareVersion string `json:"swversion"`
PointSymbol map[string]string `json:"pointsymbol"`
}
// GetLightAttributes - retrieves light attributes and state as per
// http://developers.meethue.com/1_lightsapi.html#14_get_light_attributes_and_state
func (self *Light) GetLightAttributes() (*LightAttributes, error) {
response, err := self.bridge.get("/lights/" + self.Id)
if err != nil {
return nil, err
}
defer response.Body.Close()
result := new(LightAttributes)
err = json.NewDecoder(response.Body).Decode(&result)
return result, err
}
// SetName - sets the name of a light as per
// http://developers.meethue.com/1_lightsapi.html#15_set_light_attributes_rename
func (self *Light) SetName(newName string) ([]Result, error) {
params := map[string]string{"name": newName}
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
response, err := self.bridge.put("/lights/"+self.Id, bytes.NewReader(data))
if err != nil {
return nil, err
}
defer response.Body.Close()
var results []Result
err = json.NewDecoder(response.Body).Decode(&results)
return results, err
}
// On - a convenience method to turn on a light and set its effect to "none"
func (self *Light) On() ([]Result, error) {
state := SetLightState{
On: "true",
Effect: "none",
}
return self.SetState(state)
}
// Off - a convenience method to turn off a light
func (self *Light) Off() ([]Result, error) {
state := SetLightState{On: "false"}
return self.SetState(state)
}
// ColorLoop - a convenience method to turn on a light and have it begin
// a colorloop effect
func (self *Light) ColorLoop() ([]Result, error) {
state := SetLightState{
On: "true",
Effect: "colorloop",
}
return self.SetState(state)
}
// SetState sets the state of a light as per
// http://developers.meethue.com/1_lightsapi.html#16_set_light_state
func (self *Light) SetState(state SetLightState) ([]Result, error) {
params := make(map[string]interface{})
if state.On != "" {
value, _ := strconv.ParseBool(state.On)
params["on"] = value
}
if state.Bri != "" {
params["bri"], _ = strconv.Atoi(state.Bri)
}
if state.Hue != "" {
params["hue"], _ = strconv.Atoi(state.Hue)
}
if state.Sat != "" {
params["sat"], _ = strconv.Atoi(state.Sat)
}
if state.Xy != nil {
params["xy"] = state.Xy
}
if state.Ct != "" {
params["ct"], _ = strconv.Atoi(state.Ct)
}
if state.Alert != "" {
params["alert"] = state.Alert
}
if state.Effect != "" {
params["effect"] = state.Effect
}
if state.TransitionTime != "" {
params["transitiontime"], _ = strconv.Atoi(state.TransitionTime)
}
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
response, err := self.bridge.put("/lights/"+self.Id+"/state", bytes.NewReader(data))
if err != nil {
return nil, err
}
defer response.Body.Close()
var results []Result
err = json.NewDecoder(response.Body).Decode(&results)
return results, err
}