-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.go
126 lines (108 loc) · 3.96 KB
/
lights.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
package hue
import (
"encoding/json"
"fmt"
)
// SwUpdate provides the current sw state and last install
type SwUpdate struct {
State string `json:"state,omitempty"`
LastInstall Time `json:"lastinstall,omitempty"`
}
// LightCapabilities type providing control and certification settings
type LightCapabilities struct {
Certified bool `json:"certified"`
Control *LightControl `json:"control"`
Streaming *StreamingCapabilities `json:"streaming"`
}
type StreamingCapabilities struct {
Renderer bool `json:"renderer"`
Proxy bool `json:"proxy"`
}
type DeviceConfig struct {
ArcheType string `json:"archetype"`
Function string `json:"function"`
Direction string `json:"direction"`
Startup *DeviceStartUp `json:"startup,omitempty"`
}
type DeviceStartUp struct {
Mode string `json:"mode"`
Configured bool `json:"configured"`
}
type LightControl struct {
MinDimLevel uint16 `json:"mindimlevel,omitempty"`
MaxLumen uint `json:"maxlumen,omitempty"`
ColorGamutType string `json:"colorgamuttype,omitempty"`
ColorGamut [][]float32 `json:"colorgamut,omitempty"`
Ct *LightCt `json:"ct,omitempty"`
}
type LightCt struct {
Min uint `json:"min"`
Max uint `json:"max"`
}
// Light hue object
type Light struct {
State *LightState `json:"state,omitempty"`
SwUpdate *SwUpdate `json:"swupdate,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
ModelID string `json:"modelid,omitempty"`
ManufacturerName string `json:"manufacturername,omitempty"`
Productname string `json:"productname,omitempty"`
Capabilities *LightCapabilities `json:"capabilities,omitempty"`
Config *DeviceConfig `json:"config"`
UniqueID string `json:"uniqueid,omitempty"`
SwVersion string `json:"swversion,omitempty"`
SwConfigID string `json:"swconfigid,omitempty"`
ProductID string `json:"productid,omitempty"`
}
// LightState is the hue light>state object
type LightState struct {
On bool `json:"on"`
BridgeID int `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Sat uint8 `json:"sat,omitempty"`
Effect string `json:"effect,omitempty"`
XY []float32 `json:"xy,omitempty"`
Ct uint16 `json:"ct,omitempty"`
Alert string `json:"alert,omitempty"`
TransitionTime uint16 `json:"transitiontime,omitempty"`
ColorMode string `json:"colormode,omitempty"`
Mode string `json:"mode,omitempty"`
Reachable bool `json:"reachable,omitempty"`
}
// LightsEndpoint for the lights
const LightsEndpoint = "/lights"
func (l *Light) String() string {
return fmt.Sprintf("Name=\"%s\" ModelID=\"%s\" ProductName=\"%s\" On=\"%v\" Manu=\"%s\" \n", l.Name, l.ModelID, l.Productname, l.State.On, l.ManufacturerName)
}
// ToggleLight switches light on or off
func (b *Bridge) ToggleLight(id string, on bool) (resp *BridgeResponse, err error) {
state := &LightState{
On: on,
}
return b.SetLightState(id, state)
}
// SetLightState updates the light state
func (b *Bridge) SetLightState(id string, state *LightState) (result *BridgeResponse, err error) {
res, err := b.putToBridge(LightsEndpoint+"/"+id+"/state", state)
// Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(result)
if errDecode != nil {
return nil, errDecode
}
return result, err
}
// GetLights returns all the hue lights
func (b *Bridge) GetLights() (result map[string]*Light, err error) {
result = make(map[string]*Light)
res, errCom := b.getFromBridge("/lights")
if errCom != nil {
return nil, errCom
}
// Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(&result)
if errDecode != nil {
return nil, errDecode
}
return result, err
}