-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight_helper.go
73 lines (60 loc) · 2.02 KB
/
light_helper.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
package hue
import (
"context"
"errors"
"image/color"
)
// TurnOn sets on status as true
func (s *LightService) TurnOn(ctx context.Context, id string) error {
_, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(true)})
return err
}
// TurnOff sets on status as false
func (s *LightService) TurnOff(ctx context.Context, id string) error {
_, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(false)})
return err
}
// TurnOnAll sets on status as true
func (s *LightService) TurnOnAll(ctx context.Context, ids ...string) {
for _, id := range ids {
apiResponses, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(true)})
if err != nil {
s.client.logger.Info("Turning on failed", "ApiResponses", apiResponses)
}
}
}
// TurnOffAll sets on status as false
func (s *LightService) TurnOffAll(ctx context.Context, ids ...string) {
for _, id := range ids {
apiResponses, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(false)})
if err != nil {
s.client.logger.Info("Turning off failed", "ApiResponses", apiResponses)
}
}
}
// SetColor changes the color of lamp with color
func (s *LightService) SetColor(ctx context.Context, id string, clr color.Color) error {
hueVal, satVal, briVal, ok := colorToHSV(clr)
if !ok {
return errors.New("the color is not supported")
}
apiResponses, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(true), Hue: &hueVal, Sat: &satVal, Bri: &briVal})
if err != nil {
return err
}
s.client.logger.Info("Set state successful", "ApiResponses", apiResponses)
return nil
}
// SetColorHex changes the color of lamp with hex color code
func (s *LightService) SetColorHex(ctx context.Context, id string, hex string) error {
hueVal, satVal, briVal, ok := hexColorToHSV(hex)
if !ok {
return errors.New("the color is not supported")
}
apiResponses, _, err := s.SetState(ctx, id, SetStateParams{On: Bool(true), Hue: &hueVal, Sat: &satVal, Bri: &briVal})
if err != nil {
return err
}
s.client.logger.Info("Set state successful", "ApiResponses", apiResponses)
return nil
}