-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.go
93 lines (77 loc) · 2.45 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
package hue
import (
"encoding/json"
"errors"
"strconv"
)
const scenesEndpoint = "/scenes"
type AppData struct {
Version int `json:"version,omitempty"`
Data string `json:"data,omitempty"`
}
// TODO: maybe change the naming and hierachy of the types.
type BaseScene struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Group string `json:"group,omitempty"`
Lights []int `json:"lights,omitempty"`
Owner string `json:"owner,omitempty"`
Recycle bool `json:"recycle,omitempty"`
Locked bool `json:"lcoked,omitempty"`
AppData *AppData `json:"appdata,omitempty"`
Picture string `json:"picture,omitempty"`
LastUpdated Time `json:"lastupdated,omitempty"`
Version int `json:"version,omitempty"`
}
type Scene struct {
BaseScene
LightStates map[int][]*LightState `json:"lightstates,omitempty"`
}
type CreationScene struct {
Scene
TransitionTime int `json:"transitiontime,omitempty"`
}
// GetScenes returns all the hue lights
func (b *Bridge) GetScenes() (map[string]*BaseScene, error) {
result := make(map[string]*BaseScene)
err := b.getAndDecode(scenesEndpoint, &result)
return result, err
}
// TODO: probably support the old version
// CreateScene will create a new Scene on the bridge. It will return the
// id of the new scene if no error occures.
func (b *Bridge) CreateScene(s *CreationScene) (string, error) {
res, err := b.postToBridge(scenesEndpoint, s)
if err != nil {
return "", err
}
var result string
// Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(result)
if errDecode != nil {
return "", errDecode
}
return result, nil
}
// TODO:
// - modify scene
// GetScene will return the scene specified by the id. The result will contain the
//lights states of lights within the scene.
func (b *Bridge) GetScene(id string) (*Scene, error) {
result := &Scene{}
err := b.getAndDecode(scenesEndpoint + "/" + id, result)
return result, err
}
// DeleteScene deletes the scene specified by the id. Additional to communication
// errors a non 2xx status code of the response will trigger an error.
func (b *Bridge) DeleteScene(id string) error {
res, errCom := b.deleteFromBridge(scenesEndpoint + "/" + id, nil)
if errCom != nil {
return errCom
}
// TODO: Should we put the check into the bridge methods (or maybe remove it)?
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return errors.New("Response has non 2xx status code: " + strconv.Itoa(res.StatusCode))
}
return nil
}