summaryrefslogtreecommitdiff
path: root/src/rhctl/audio_switch_command.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhctl/audio_switch_command.go')
-rw-r--r--src/rhctl/audio_switch_command.go66
1 files changed, 57 insertions, 9 deletions
diff --git a/src/rhctl/audio_switch_command.go b/src/rhctl/audio_switch_command.go
index 17be8d3..712eee0 100644
--- a/src/rhctl/audio_switch_command.go
+++ b/src/rhctl/audio_switch_command.go
@@ -27,6 +27,15 @@ import (
"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 {
@@ -38,7 +47,7 @@ func (u *SwitchUnitID) FromString(str string) error {
if err != nil {
return err
}
- if vuint > 3 {
+ if vuint > uint64(SwitchUnitIDMax) {
return fmt.Errorf("switch unit ID is out of range")
}
*u = SwitchUnitID(vuint)
@@ -60,7 +69,7 @@ func (i *SwitchInputNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchInputNumMax) {
return fmt.Errorf("switch input number is out of range")
}
*i = SwitchInputNum(vuint)
@@ -78,13 +87,31 @@ func (o *SwitchOutputNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 2 {
+ 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 {
@@ -96,7 +123,7 @@ func (r *SwitchRelayNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchRelayNumMax) {
return fmt.Errorf("switch relay number is out of range")
}
*r = SwitchRelayNum(vuint)
@@ -114,7 +141,7 @@ func (o *SwitchOCNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchOCNumMax) {
return fmt.Errorf("switch OC number is out of range")
}
*o = SwitchOCNum(vuint)
@@ -125,7 +152,8 @@ type SwitchCmdString string
const (
SwitchCmdStateAudio SwitchCmdString = "*uSL"
- SwitchCmdStateGPI SwitchCmdString = "*uSPA"
+ SwitchCmdStateGPI SwitchCmdString = "*uSPgg"
+ SwitchCmdStateGPIAll SwitchCmdString = "*uSPA"
SwitchCmdStateOC SwitchCmdString = "*uSO"
SwitchCmdStateRelay SwitchCmdString = "*uSR"
SwitchCmdStateSilence SwitchCmdString = "*uSS"
@@ -152,23 +180,43 @@ const (
SwitchCmdRelayPulse SwitchCmdString = "*uORrP"
)
-func (c SwitchCmdString) Generate(args ...interface{}) string {
+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
+ return s, nil
}
type SwitchCommand struct {
@@ -186,7 +234,7 @@ func SwitchCommandParseState(args []string) (cmdstr SwitchCmdString, cmdargs []i
case "audio":
cmdstr = SwitchCmdStateAudio
case "gpi":
- cmdstr = SwitchCmdStateGPI
+ cmdstr = SwitchCmdStateGPIAll
case "oc":
cmdstr = SwitchCmdStateOC
case "relay":