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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
//
// rhctl
//
// Copyright (C) 2009-2016 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhctl.
//
// rhctl is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// rhctl is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rhctl. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"strconv"
"strings"
)
const (
SwitchUnitIDMax SwitchUnitID = 3
SwitchInputNumMax SwitchInputNum = 8
SwitchOutputNumMax SwitchOutputNum = 2
SwitchGPINumMax SwitchGPINum = 16
SwitchRelayNumMax SwitchRelayNum = 8
SwitchOCNumMax SwitchOCNum = 8
)
type SwitchUnitID uint
func (u SwitchUnitID) String() string {
return strconv.FormatUint(uint64(u), 10)
}
func (u *SwitchUnitID) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint > uint64(SwitchUnitIDMax) {
return fmt.Errorf("switch unit ID is out of range")
}
*u = SwitchUnitID(vuint)
return nil
}
func (u *SwitchUnitID) UnmarshalTOML(data []byte) error {
return u.FromString(string(data))
}
type SwitchInputNum uint
func (i SwitchInputNum) String() string {
return strconv.FormatUint(uint64(i), 10)
}
func (i *SwitchInputNum) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint < 1 || vuint > uint64(SwitchInputNumMax) {
return fmt.Errorf("switch input number is out of range")
}
*i = SwitchInputNum(vuint)
return nil
}
type SwitchOutputNum uint
func (o SwitchOutputNum) String() string {
return strconv.FormatUint(uint64(o), 10)
}
func (o *SwitchOutputNum) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint < 1 || vuint > uint64(SwitchOutputNumMax) {
return fmt.Errorf("switch output number is out of range")
}
*o = SwitchOutputNum(vuint)
return nil
}
type SwitchGPINum uint
func (g SwitchGPINum) String() string {
return strconv.FormatUint(uint64(g), 10)
}
func (g *SwitchGPINum) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint < 1 || vuint > uint64(SwitchGPINumMax) {
return fmt.Errorf("switch GPI number is out of range")
}
*g = SwitchGPINum(vuint)
return nil
}
type SwitchRelayNum uint
func (r SwitchRelayNum) String() string {
return strconv.FormatUint(uint64(r), 10)
}
func (r *SwitchRelayNum) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint < 1 || vuint > uint64(SwitchRelayNumMax) {
return fmt.Errorf("switch relay number is out of range")
}
*r = SwitchRelayNum(vuint)
return nil
}
type SwitchOCNum uint
func (o SwitchOCNum) String() string {
return strconv.FormatUint(uint64(o), 10)
}
func (o *SwitchOCNum) FromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if vuint < 1 || vuint > uint64(SwitchOCNumMax) {
return fmt.Errorf("switch OC number is out of range")
}
*o = SwitchOCNum(vuint)
return nil
}
type SwitchCmdString string
const (
SwitchCmdStateAudio SwitchCmdString = "*uSL"
SwitchCmdStateGPI SwitchCmdString = "*uSPgg"
SwitchCmdStateGPIAll SwitchCmdString = "*uSPA"
SwitchCmdStateOC SwitchCmdString = "*uSO"
SwitchCmdStateRelay SwitchCmdString = "*uSR"
SwitchCmdStateSilence SwitchCmdString = "*uSS"
SwitchCmdAudioApplyInput SwitchCmdString = "*uiio"
SwitchCmdAudioApplyInputAll SwitchCmdString = "*uiiA"
SwitchCmdAudioAddInputTo1 SwitchCmdString = "*uii3"
SwitchCmdAudioAddInputTo2 SwitchCmdString = "*uii4"
SwitchCmdAudioRemoveInputFrom1 SwitchCmdString = "*uii5"
SwitchCmdAudioRemoveInputFrom2 SwitchCmdString = "*uii6"
SwitchCmdAudioMuteInput SwitchCmdString = "*uiiMo"
SwitchCmdAudioMuteInputAll SwitchCmdString = "*uiiMA"
SwitchCmdAudioMuteOutput SwitchCmdString = "*uMo"
SwitchCmdAudioMuteOutputAll SwitchCmdString = "*uMA"
SwitchCmdAudioFadeUpInput SwitchCmdString = "*uFUii"
SwitchCmdAudioFadeDownInput SwitchCmdString = "*uFDii"
SwitchCmdOCUnlatch SwitchCmdString = "*uOOcF"
SwitchCmdOCLatch SwitchCmdString = "*uOOcL"
SwitchCmdOCPulse SwitchCmdString = "*uOOcP"
SwitchCmdRelayUnlatch SwitchCmdString = "*uORrF"
SwitchCmdRelayLatch SwitchCmdString = "*uORrL"
SwitchCmdRelayPulse SwitchCmdString = "*uORrP"
)
func (c SwitchCmdString) Generate(args ...interface{}) (string, error) {
s := string(c)
for _, arg := range args {
switch arg.(type) {
case SwitchUnitID:
if arg.(SwitchUnitID) > SwitchUnitIDMax {
return "", fmt.Errorf("switch unit ID is out of range")
}
s = strings.Replace(s, "u", fmt.Sprintf("%1d", arg.(SwitchUnitID)), -1)
case SwitchInputNum:
if arg.(SwitchInputNum) < 1 || arg.(SwitchInputNum) > SwitchInputNumMax {
return "", fmt.Errorf("switch input number is out of range")
}
s = strings.Replace(s, "ii", fmt.Sprintf("%02d", arg.(SwitchInputNum)), -1)
case SwitchOutputNum:
if arg.(SwitchOutputNum) < 1 || arg.(SwitchOutputNum) > SwitchOutputNumMax {
return "", fmt.Errorf("switch input number is out of range")
}
s = strings.Replace(s, "o", fmt.Sprintf("%1d", arg.(SwitchOutputNum)), -1)
case SwitchGPINum:
if arg.(SwitchGPINum) < 1 || arg.(SwitchGPINum) > SwitchGPINumMax {
return "", fmt.Errorf("switch GPI number is out of range")
}
s = strings.Replace(s, "gg", fmt.Sprintf("%02d", arg.(SwitchGPINum)), -1)
case SwitchRelayNum:
if arg.(SwitchRelayNum) < 1 || arg.(SwitchRelayNum) > SwitchRelayNumMax {
return "", fmt.Errorf("switch relay number is out of range")
}
s = strings.Replace(s, "r", fmt.Sprintf("%1d", arg.(SwitchRelayNum)), -1)
case SwitchOCNum:
if arg.(SwitchOCNum) < 1 || arg.(SwitchOCNum) > SwitchOCNumMax {
return "", fmt.Errorf("switch OC number is out of range")
}
s = strings.Replace(s, "c", fmt.Sprintf("%1d", arg.(SwitchOCNum)), -1)
}
}
return s, nil
}
type SwitchCommand struct {
Cmd SwitchCmdString
Args []interface{}
Response chan<- interface{}
}
func SwitchCommandParseState(args []string) (cmdstr SwitchCmdString, cmdargs []interface{}, err error) {
if len(args) == 0 {
err = fmt.Errorf("missing argument <state-type>")
return
}
switch strings.ToLower(args[0]) {
case "audio":
cmdstr = SwitchCmdStateAudio
case "gpi":
cmdstr = SwitchCmdStateGPIAll
case "oc":
cmdstr = SwitchCmdStateOC
case "relay":
cmdstr = SwitchCmdStateRelay
case "silence":
cmdstr = SwitchCmdStateSilence
default:
err = fmt.Errorf("unknown state-type: '%s'", args[0])
return
}
return
}
func SwitchCommandParseAudio(args []string) (cmdstr SwitchCmdString, cmdargs []interface{}, err error) {
if len(args) < 2 || len(args) > 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
onum := SwitchOutputNum(0)
if strings.ToLower(args[0]) != "all" {
if err = onum.FromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, onum)
}
inum := SwitchInputNum(0)
if len(args) == 3 {
if err = inum.FromString(args[2]); err != nil {
return
}
cmdargs = append(cmdargs, inum)
}
switch strings.ToLower(args[1]) {
case "apply":
if len(args) != 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
if onum == 0 {
cmdstr = SwitchCmdAudioApplyInputAll
} else {
cmdstr = SwitchCmdAudioApplyInput
}
case "add":
if len(args) != 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
switch onum {
case 1:
cmdstr = SwitchCmdAudioAddInputTo1
case 2:
cmdstr = SwitchCmdAudioAddInputTo2
default:
err = fmt.Errorf("this operation cannot be applied to all outputs")
return
}
case "remove":
if len(args) != 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
switch onum {
case 1:
cmdstr = SwitchCmdAudioRemoveInputFrom1
case 2:
cmdstr = SwitchCmdAudioRemoveInputFrom2
default:
err = fmt.Errorf("this operation cannot be applied to all outputs")
return
}
case "up":
if len(args) != 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
if onum != 1 {
err = fmt.Errorf("this operation is only allowed on output 1")
return
}
cmdstr = SwitchCmdAudioFadeUpInput
case "down":
if len(args) != 3 {
err = fmt.Errorf("wrong number of arguments")
return
}
if onum != 1 {
err = fmt.Errorf("this operation is only allowed on output 1")
return
}
cmdstr = SwitchCmdAudioFadeDownInput
case "mute":
if len(args) == 3 {
if onum == 0 {
cmdstr = SwitchCmdAudioMuteInputAll
} else {
cmdstr = SwitchCmdAudioMuteInput
}
} else {
if onum == 0 {
cmdstr = SwitchCmdAudioMuteOutputAll
} else {
cmdstr = SwitchCmdAudioMuteOutput
}
}
default:
err = fmt.Errorf("unknown operation: '%s', must be one of latch, unlatch, pulse", args[1])
return
}
return
}
func SwitchCommandParseRelay(args []string) (cmdstr SwitchCmdString, cmdargs []interface{}, err error) {
if len(args) != 2 {
err = fmt.Errorf("wrong number of arguments, expecting: <relay-num> (latch|unlatch|pulse)")
return
}
var num SwitchRelayNum
if err = num.FromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, num)
switch strings.ToLower(args[1]) {
case "latch":
cmdstr = SwitchCmdRelayLatch
case "unlatch":
cmdstr = SwitchCmdRelayUnlatch
case "pulse":
cmdstr = SwitchCmdRelayPulse
default:
err = fmt.Errorf("unknown operation: '%s', must be one of latch, unlatch, pulse", args[1])
return
}
return
}
func SwitchCommandParseOC(args []string) (cmdstr SwitchCmdString, cmdargs []interface{}, err error) {
if len(args) != 2 {
err = fmt.Errorf("wrong number of arguments, expecting: <oc-num> (latch|unlatch|pulse)")
return
}
var num SwitchOCNum
if err = num.FromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, num)
switch strings.ToLower(args[1]) {
case "latch":
cmdstr = SwitchCmdOCLatch
case "unlatch":
cmdstr = SwitchCmdOCUnlatch
case "pulse":
cmdstr = SwitchCmdOCPulse
default:
err = fmt.Errorf("unknown operation: '%s', must be one of latch, unlatch, pulse", args[1])
return
}
return
}
func NewSwitchCommandFromStrings(cmd string, args ...string) (c *SwitchCommand, err error) {
c = &SwitchCommand{}
switch strings.ToLower(cmd) {
case "state":
c.Cmd, c.Args, err = SwitchCommandParseState(args)
case "out":
c.Cmd, c.Args, err = SwitchCommandParseAudio(args)
case "relay":
c.Cmd, c.Args, err = SwitchCommandParseRelay(args)
case "oc":
c.Cmd, c.Args, err = SwitchCommandParseOC(args)
default:
return nil, fmt.Errorf("unknown command '%s'", cmd)
}
return
}
|