forked from savaki/go.hue
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscene.go
172 lines (148 loc) · 5.2 KB
/
scene.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 hue
import "fmt"
import "errors"
// Scene represents a Hue scene saved on the bridge.
type Scene struct {
bridge *Bridge
Id string `json:"-"`
Name string `json:"name"`
Lights []string `json:"lights"`
Owner string `json:"owner"`
Recycle bool `json:"recycle"`
Locked bool `json:"locked"`
Appdata map[string]interface{} `json:"appdata"`
Picture string `json:"picture"`
LastUpdated string `json:"lastupdated"`
Version int `json:"version"`
LightStates map[string]LightState `json:"lightstates"`
}
// CreateScene contains all necessary attributes to create a new scene on the bridge.
type CreateScene struct {
Name string `json:"name,omitempty"`
Lights []string `json:"lights,omitempty"`
Recycle bool `json:"recycle"`
TransitionTime int `json:"transistiontime,omitempty"`
Appdata map[string]interface{} `json:"appdata,omitempty"`
Picture string `json:"picture,omitempty"`
}
// ModifyScene contains all attributes to be changed on a given scene.
type ModifyScene struct {
Name string `json:"name,omitempty"`
Lights []string `json:"lights,omitempty"`
StoreLightState bool `json:"storelightstate,omitempty"`
}
// ModifyLightState contains all light attributes to be changed on a given scene.
type ModifyLightState struct {
On bool `json:"on,omitempty"`
Brightness uint8 `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Saturation uint8 `json:"sat,omitempty"`
Xy []float32 `json:"xy,omitempty"`
ColorTemperature uint16 `json:"ct,omitempty"`
Effect string `json:"effect,omitempty"`
TransitionTime uint16 `json:"transistiontime,omitempty"`
}
// CreateScene stores a new scene with the given attributes on the bridge.
// In addition to the given information the current light states of all referenced
// lights will be part of the scene.
func (bridge *Bridge) CreateScene(scenedata CreateScene) ([]Result, error) {
var results []Result
err := bridge.post("/scenes/", &scenedata, &results)
if err != nil {
return nil, err
}
return results, nil
}
// AllScenes returns all scenes currently saved on the bridge.
func (bridge *Bridge) AllScenes() ([]*Scene, error) {
var scenes []*Scene
var results map[string]Scene
err := bridge.get("/scenes", &results)
if err != nil {
return scenes, err
}
// and convert them into scenes
for id, scene := range results {
scene := scene
scene.Id = id
scene.bridge = bridge
if err != nil {
return scenes, err
}
scenes = append(scenes, &scene)
}
return scenes, nil
}
// SceneByID looks up the scene with the given ID on the bridge.
func (bridge *Bridge) SceneByID(id string) (*Scene, error) {
var result Scene
err := bridge.get(fmt.Sprintf("/scenes/%s", id), &result)
if err != nil {
return nil, err
}
result.Id = id
result.bridge = bridge
return &result, nil
}
// SceneByName looks up the scene with the given name on the bridge.
func (bridge *Bridge) SceneByName(name string) (*Scene, error) {
scenes, err := bridge.AllScenes()
if err != nil {
return nil, err
}
for _, scene := range scenes {
if scene.Name == name {
return bridge.SceneByID(scene.Id) // second request to fill lightstates
}
}
return nil, errors.New("Unable to find scene with name " + name)
}
// Modify adjusts a saved scene according to the given attributes.
func (scene *Scene) Modify(modifyScene ModifyScene) ([]Result, error) {
var results []Result
err := scene.bridge.put("/scenes/"+scene.Id, &modifyScene, &results)
if err != nil {
return nil, err
}
return results, nil
}
// ModifyLightStates adjusts the saved light states of all lights in the given scene on the bridge.
func (scene *Scene) ModifyLightStates(lightstate ModifyLightState) ([]Result, error) {
var results []Result
for _, light := range scene.Lights {
result, err := scene.ModifyLightState(light, lightstate)
results = append(results, result...)
if err != nil {
return results, err
}
}
return results, nil
}
// ModifyLightState adjusts the saved light state of the given light in the given scene on the bridge.
func (scene *Scene) ModifyLightState(lightID string, lightstate ModifyLightState) ([]Result, error) {
var results []Result
err := scene.bridge.put(fmt.Sprintf("/scenes/%s/lightstates/%s", scene.Id, lightID), &lightstate, &results)
if err != nil {
return nil, err
}
return results, nil
}
// Delete will remove the given scene from the bridge.
func (scene *Scene) Delete() ([]Result, error) {
var results []Result
err := scene.bridge.delete("/scenes/"+scene.Id, &results)
if err != nil {
return nil, err
}
return results, err
}
// Activate will recall the given scene according to it's state on the bridge.
func (scene *Scene) Activate() ([]Result, error) {
request := map[string]string{"scene": scene.Id}
var results []Result
err := scene.bridge.put("/groups/0/action", &request, &results)
if err != nil {
return nil, err
}
return results, nil
}