-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_state.go
212 lines (187 loc) · 4.11 KB
/
light_state.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package insteon
import (
"fmt"
"math"
)
// LightOnOff represents a light on/off state.
type LightOnOff bool
const (
// LightOn indicates an on light.
LightOn LightOnOff = true
// LightOff indicates an off light.
LightOff LightOnOff = false
)
func (s LightOnOff) String() string {
if s {
return "on"
}
return "off"
}
// LightStateChange represents a light state change.
type LightStateChange int
const (
// ChangeNormal indicates that the light state must change as if a single
// press had been done.
ChangeNormal LightStateChange = iota
// ChangeInstant indicates that the light state must change instantly, as
// if a quick double-press had been done.
ChangeInstant
// ChangeStep indicates that the light state must change for one step up or
// down.
ChangeStep
// ChangeStart indicates that the light state must start changing until a
// ChangeStop change is set.
ChangeStart
// ChangeStop stops a light change started with ChangeStart.
ChangeStop
)
// UnmarshalText -
func (c *LightStateChange) UnmarshalText(b []byte) error {
s := string(b)
switch s {
case "normal":
*c = ChangeNormal
case "instant":
*c = ChangeInstant
case "step":
*c = ChangeStep
case "start":
*c = ChangeStart
case "stop":
*c = ChangeStop
default:
return fmt.Errorf("unsupported light state change: %s", s)
}
return nil
}
// MarshalText -
func (c LightStateChange) MarshalText() ([]byte, error) {
switch c {
case ChangeNormal:
return []byte("normal"), nil
case ChangeInstant:
return []byte("instant"), nil
case ChangeStep:
return []byte("step"), nil
case ChangeStart:
return []byte("start"), nil
case ChangeStop:
return []byte("stop"), nil
default:
return []byte("normal"), nil
}
}
// LightState represents a light state.
type LightState struct {
OnOff LightOnOff `json:"onoff"`
Change LightStateChange `json:"change,omitempty"`
Level float64 `json:"level,omitempty"`
}
func (s LightState) asCommandBytes() [2]byte {
data, _ := s.MarshalBinary()
return [2]byte{data[0], data[1]}
}
// MarshalBinary -
func (s LightState) MarshalBinary() ([]byte, error) {
levelByte := onLevelToByte(s.Level)
if s.OnOff == LightOn {
switch s.Change {
case ChangeInstant:
return []byte{0x12, levelByte}, nil
case ChangeStep:
return []byte{0x15, 0}, nil
case ChangeStart:
return []byte{0x17, 0x01}, nil
case ChangeStop:
return []byte{0x18, 0x00}, nil
}
return []byte{0x11, levelByte}, nil
}
switch s.Change {
case ChangeInstant:
return []byte{0x14, levelByte}, nil
case ChangeStep:
return []byte{0x16, 0}, nil
case ChangeStart:
return []byte{0x17, 0x00}, nil
case ChangeStop:
return []byte{0x18, 0x00}, nil
}
return []byte{0x13, levelByte}, nil
}
// UnmarshalBinary -
func (s *LightState) UnmarshalBinary(b []byte) error {
if len(b) != 2 {
return fmt.Errorf("expected 2 bytes, not %d", len(b))
}
switch b[0] {
case 0x11:
*s = LightState{
OnOff: LightOn,
Change: ChangeNormal,
Level: byteToOnLevel(b[1]),
}
case 0x12:
*s = LightState{
OnOff: LightOn,
Change: ChangeInstant,
Level: byteToOnLevel(b[1]),
}
case 0x13:
*s = LightState{
OnOff: LightOff,
Change: ChangeNormal,
Level: byteToOnLevel(b[1]),
}
case 0x14:
*s = LightState{
OnOff: LightOff,
Change: ChangeInstant,
Level: byteToOnLevel(b[1]),
}
case 0x15:
*s = LightState{
OnOff: LightOn,
Change: ChangeStep,
Level: 0,
}
case 0x16:
*s = LightState{
OnOff: LightOff,
Change: ChangeStep,
Level: 0,
}
case 0x17:
if b[1] == 0x00 {
*s = LightState{
OnOff: LightOff,
Change: ChangeStart,
Level: 0,
}
} else {
*s = LightState{
OnOff: LightOn,
Change: ChangeStart,
Level: 0,
}
}
case 0x18:
*s = LightState{
OnOff: LightOn,
Change: ChangeStop,
Level: 0,
}
default:
return fmt.Errorf("unexpected command code for light state: %02x%02x", b[0], b[1])
}
return nil
}
func byteToOnLevel(b byte) float64 {
return float64(b) / 0xff
}
func onLevelToByte(level float64) byte {
return byte(clampLevel(level) * 0xff)
}
func clampLevel(v float64) float64 {
return math.Max(0, math.Min(1, v))
}