-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.go
139 lines (121 loc) · 3.62 KB
/
info.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
127
128
129
130
131
132
133
134
135
136
137
138
139
package lifx
import (
"context"
"encoding/binary"
"errors"
"fmt"
"time"
)
func (d *Device) GetLightPower(ctx context.Context) (uint16, error) {
payload, err := d.query(ctx, pktGetLightPower, pktStateLightPower, nil)
if err != nil {
return 0, err
}
if len(payload) != 2 {
return 0, fmt.Errorf("StateLightPower malformed: length=%d", len(payload))
}
return binary.LittleEndian.Uint16(payload), nil
}
func (d *Device) SetLightPower(ctx context.Context, level uint16, duration time.Duration) error {
dur, err := uint32Millis(duration)
if err != nil {
return err
}
var payload []byte
payload = binary.LittleEndian.AppendUint16(payload, level)
binary.LittleEndian.AppendUint32(payload, dur)
return d.set(ctx, pktSetLightPower, payload)
}
func (d *Device) GetPower(ctx context.Context) (uint16, error) {
payload, err := d.query(ctx, pktGetPower, pktStatePower, nil)
if err != nil {
return 0, err
}
if len(payload) != 2 {
return 0, fmt.Errorf("StatePower malformed: length=%d", len(payload))
}
return binary.LittleEndian.Uint16(payload), nil
}
func (d *Device) GetLabel(ctx context.Context) (string, error) {
payload, err := d.query(ctx, pktGetLabel, pktStateLabel, nil)
if err != nil {
return "", err
}
// Ignore trailing NULs in payload.
for i := len(payload) - 1; i >= 0; i-- {
if payload[i] != 0x00 {
break
}
payload = payload[:i]
}
return string(payload), nil
}
func (d *Device) GetVersion(ctx context.Context) (vendor, product uint32, err error) {
payload, err := d.query(ctx, pktGetVersion, pktStateVersion, nil)
if err != nil {
return 0, 0, err
}
if len(payload) != 12 {
return 0, 0, fmt.Errorf("StateVersion malformed: length=%d", len(payload))
}
vendor = binary.LittleEndian.Uint32(payload[0:4])
product = binary.LittleEndian.Uint32(payload[4:8])
return
}
type HostFirmware struct {
Build time.Time
Major, Minor uint16
}
func (d *Device) GetHostFirmware(ctx context.Context) (HostFirmware, error) {
payload, err := d.query(ctx, pktGetHostFirmware, pktStateHostFirmware, nil)
if err != nil {
return HostFirmware{}, err
}
if len(payload) != 20 {
return HostFirmware{}, fmt.Errorf("StateHostFirmware malformed: length=%d", len(payload))
}
var hf HostFirmware
hf.Build = time.Unix(0, int64(binary.LittleEndian.Uint64(payload[0:8]))) // empirically seems to be unix nanos
hf.Minor = binary.LittleEndian.Uint16(payload[16:18])
hf.Major = binary.LittleEndian.Uint16(payload[18:20])
return hf, nil
}
type State struct {
power uint16
zones []Color // nil if not a multi-zone device
// TODO: will need to capture effects too.
}
func (s State) NumZones() int { return len(s.zones) }
// CaptureState queries the device and returns its current configuration.
func (d *Device) CaptureState(ctx context.Context) (state State, err error) {
state.power, err = d.GetLightPower(ctx)
if err != nil {
err = fmt.Errorf("GetLightPower: %w", err)
return
}
state.zones, err = d.GetExtendedColorZones(ctx)
var ue unhandledError
if err == nil {
// OK
} else if errors.As(err, &ue) {
// This is okay; the device doesn't have this capability.
state.zones, err = nil, nil
} else {
err = fmt.Errorf("GetExtendedColorZones: %w", err)
return
}
return
}
// RestoreState restores a device to its configuration at the time CaptureState was invoked.
func (d *Device) RestoreState(ctx context.Context, state State) error {
if state.zones != nil {
err := d.SetExtendedColorZones(ctx, 0, state.zones)
if err != nil {
return fmt.Errorf("SetExtendedColorZones: %w", err)
}
}
if err := d.SetLightPower(ctx, state.power, 0); err != nil {
return fmt.Errorf("SetLightPower: %w", err)
}
return nil
}