/
power.go
67 lines (58 loc) · 1.06 KB
/
power.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
package light
import (
"context"
"net"
"time"
"go.yhsif.com/lifxlan"
)
// RawSetLightPowerPayload defines the struct to be used for encoding and decoding.
//
// https://lan.developer.lifx.com/docs/changing-a-device#setlightpower---packet-117
type RawSetLightPowerPayload struct {
Level lifxlan.Power
Duration lifxlan.TransitionTime
}
func (ld *device) SetLightPower(
ctx context.Context,
conn net.Conn,
power lifxlan.Power,
transition time.Duration,
ack bool,
) error {
if ctx.Err() != nil {
return ctx.Err()
}
if conn == nil {
newConn, err := ld.Dial()
if err != nil {
return err
}
defer newConn.Close()
conn = newConn
if ctx.Err() != nil {
return ctx.Err()
}
}
var flags lifxlan.AckResFlag
if ack {
flags |= lifxlan.FlagAckRequired
}
// Send
seq, err := ld.Send(
ctx,
conn,
flags,
SetLightPower,
&RawSetLightPowerPayload{
Level: power,
Duration: lifxlan.ConvertDuration(transition),
},
)
if err != nil {
return err
}
if ack {
return lifxlan.WaitForAcks(ctx, conn, ld.Source(), seq)
}
return nil
}