aboutsummaryrefslogtreecommitdiff
path: root/scene.go
blob: 687ca1c73ccfcf6038114f6e8e59d85578965e58 (plain)
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
/*
* scene.go
* GoHue library for Philips Hue
* Copyright (C) 2016 Collin Guarino (Collinux) collin.guarino@gmail.com
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
 */
// http://www.developers.meethue.com/documentation/scenes-api

package hue

import (
	"encoding/json"
	"fmt"
)

// Scene struct defines attributes for Scene items
type Scene struct {
	Appdata *struct {
		Data    string `json:"data,omitempty"`
		Version int    `json:"version,omitempty"`
	} `json:"appdata,omitempty"`
	Lastupdated string   `json:"lastupdated,omitempty"`
	Lights      []string `json:"lights,omitempty"`
	Locked      bool     `json:"locked,omitempty"`
	Name        string   `json:"name,omitempty"`
	Owner       string   `json:"owner,omitempty"`
	Picture     string   `json:"picture,omitempty"`
	Recycle     bool     `json:"recycle,omitempty"`
	Version     int      `json:"version,omitempty"`
	ID          string   `json:",omitempty"`
}

// Bridge.GetScenes gets the attributes for all scenes.
func (bridge *Bridge) GetAllScenes() ([]Scene, error) {
	uri := fmt.Sprintf("/api/%s/scenes", bridge.Username)
	body, _, err := bridge.Get(uri)
	if err != nil {
		return []Scene{}, err
	}

	scenes := map[string]Scene{}
	err = json.Unmarshal(body, &scenes)
	if err != nil {
		return []Scene{}, err
	}
	scenesList := []Scene{}
	for key, value := range scenes {
		scene := Scene{}
		scene = value
		scene.ID = key
		scenesList = append(scenesList, scene)
	}
	return scenesList, nil
}

// Bridge.GetScene gets the attributes for an individual scene.
// This is used to optimize time when updating the state of the scene.
// Note: The ID is not an index, it's a unique key generated for each scene.
func (bridge *Bridge) GetScene(id string) (Scene, error) {
	uri := fmt.Sprintf("/api/%s/scenes/%s", bridge.Username, id)
	body, _, err := bridge.Get(uri)
	if err != nil {
		return Scene{}, err
	}

	scene := Scene{}
	err = json.Unmarshal(body, &scene)
	if err != nil {
		return Scene{}, err
	}
	return scene, nil
}

// Bridge.CreateScene posts a new scene configuration to the bridge.
func (bridge *Bridge) CreateScene(scene Scene) error {
	uri := fmt.Sprintf("/api/%s/scenes/", bridge.Username)
	_, _, err := bridge.Post(uri, scene)
	if err != nil {
		return err
	}
	return nil
}

// Bridge.ModifySceneState amends light states for lights
// included in a scene list. See `Bridge.ModifyScene` for
// changing the lights included in the scene list.
// func (bridge *Bridge) ModifySceneState() error {
//
// }

// Bridge.ModifyScene amends the lights included for a given scene or
// it can be used to change the scene name. To amend light states for
// lights included in a scene list see `Bridge.ModifySceneState`.
// func (bridge *Bridge) ModifyScene() error {
//     uri := fmt.Sprintf("/api/%s/scenes/%s/lightstates/%s",
//         bridge.Username, oldScene.ID, )
// }