-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.go
257 lines (234 loc) · 5.63 KB
/
device.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package onkyoctl
import (
"sync"
"time"
)
const protocol = "tcp"
// Callback is the type for message callback functions.
type Callback func(name, value string)
// Device is an Onkyo device.
type Device struct {
Host string
Port int
log Logger
commands CommandSet
callback Callback
onConnect func()
onDisconnect func()
wait *sync.WaitGroup
autoConnect bool
allowReconnect bool
reconnectTime time.Duration
client *client
}
// NewDevice sets up a new Onkyo device.
func NewDevice(cfg *Config) *Device {
commands := cfg.Commands
if commands == nil {
commands = emptyCommands()
}
log := cfg.Log
if log == nil {
log = NewLogger(NoLog)
}
d := &Device{
Host: cfg.Host,
Port: cfg.Port,
log: log,
commands: commands,
wait: &sync.WaitGroup{},
autoConnect: cfg.AutoConnect,
allowReconnect: cfg.AllowReconnect,
reconnectTime: time.Duration(cfg.ReconnectSeconds) * time.Second,
client: newClient(cfg.Host, cfg.Port, log),
}
d.client.handler = d.handleReceived
d.client.connectionCB = d.connectionChanged
return d
}
// OnMessage sets the handler for received messages to the given function.
// This will replace any existing handler.
func (d *Device) OnMessage(callback Callback) {
d.callback = callback
}
// OnDisconnected is called when the device is disconnected.
func (d *Device) OnDisconnected(callback func()) {
d.onDisconnect = callback
}
// OnConnected is called when the deivce is (re-)connected.
func (d *Device) OnConnected(callback func()) {
d.onConnect = callback
}
// Start connects to the device and starts receiving messages.
func (d *Device) Start() {
d.client.Start()
d.client.Connect()
}
// Stop disconnects from the device and stop message processing.
func (d *Device) Stop() {
d.log.Info("Stop device [%v:%v]", d.Host, d.Port)
d.client.Stop()
}
// SendCommand sends an "friendly" command (e.g. "power off") to the device.
//
// This method calls `SendISCP()` behind the scenes.
func (d *Device) SendCommand(name string, param interface{}) error {
command, err := d.commands.CreateCommand(name, param)
if err != nil {
return err
}
return d.SendISCP(command, 0)
}
// Query sends a QSTN command for the given friendly name.
//
// This method calls `SendISCP()` behind the scenes.
func (d *Device) Query(name string) error {
q, err := d.commands.CreateQuery(name)
if err != nil {
return err
}
return d.SendISCP(q, 0)
}
// SendISCP sends a raw ISCP command to the device.
//
// You must `Start()` before you can send messages.
// The device may lose its connection after start. With AutoConnect
// set to true, attempts to connect and returns an error only if that fails.
// Without autoconnect, an error is returned if the device is not connected.
//
// The message is send asynchronously. Use a non-zero timeout to wait until
// the message is sent.
// Note that the message may still be sent even if `ErrTimeout` is returned.
func (d *Device) SendISCP(cmd ISCPCommand, timeout time.Duration) error {
if d.autoConnect {
// if already connected, this does nothing
d.Start()
}
d.client.WaitConnect(timeout)
return d.client.Send(cmd, timeout)
}
func (d *Device) connectionChanged(s ConnectionState) {
d.log.Debug("Connection state changed to %q", s)
if s == Connected && d.onConnect != nil {
d.onConnect()
}
if s == Disconnected && d.onDisconnect != nil {
d.onDisconnect()
if d.allowReconnect {
//TODO: not when we Stop()'ed
d.log.Debug("Schedule reconnect")
go func() {
time.Sleep(d.reconnectTime)
d.client.Connect()
}()
}
}
}
func (d *Device) handleReceived(cmd ISCPCommand) {
name, value, err := d.commands.ReadCommand(cmd)
if err != nil {
d.log.Warning("Error reading %q: %v", cmd, err)
return
}
d.log.Debug("Received '%v %v'", name, value)
if d.callback != nil {
d.callback(name, value)
}
}
// BasicCommands creates a command set with some commonly used commands.
func BasicCommands() CommandSet {
commands := []Command{
{
Name: "power",
Group: "PWR",
ParamType: "onOff",
},
{
Name: "volume",
Group: "MVL",
ParamType: "intRangeEnum",
Lower: 0,
Upper: 100,
Scale: 2,
Lookup: map[string]string{
"UP": "up",
"DOWN": "down",
},
},
{
Name: "mute",
Group: "AMT",
ParamType: "onOffToggle",
},
{
Name: "speaker-a",
Group: "SPA",
ParamType: "onOff",
},
{
Name: "speaker-b",
Group: "SPB",
ParamType: "onOff",
},
{
Name: "dimmer",
Group: "DIM",
ParamType: "enum",
Lookup: map[string]string{
"00": "bright",
"01": "dim",
"02": "dark",
"03": "off",
"08": "led-off",
},
},
{
Name: "display",
Group: "DIF",
ParamType: "enumToggle",
Lookup: map[string]string{
"00": "default",
"01": "listening",
"02": "source",
"03": "mode-4",
},
},
{
Name: "input",
Group: "SLI",
ParamType: "enum",
Lookup: map[string]string{
"00": "video-1",
"01": "cbl-sat",
"02": "game",
"03": "aux1",
"20": "tv",
"2B": "network",
},
},
{
Name: "listen-mode",
Group: "LMD",
ParamType: "enum",
Lookup: map[string]string{
"00": "stereo",
"STEREO": "stereo",
"01": "direct",
"11": "pure",
},
},
{
Name: "update",
Group: "UPD",
ParamType: "enum",
Lookup: map[string]string{
"00": "no-new-firmware",
"01": "new-firmware",
},
},
}
return NewBasicCommandSet(commands)
}
func emptyCommands() CommandSet {
return NewBasicCommandSet(make([]Command, 0))
}