diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rhctl/audio_switch.go | 7 | ||||
-rw-r--r-- | src/rhctl/audio_switch_command.go | 33 | ||||
-rw-r--r-- | src/rhctl/switch_control.go | 19 | ||||
-rw-r--r-- | src/rhctl/telnet.go | 35 |
4 files changed, 75 insertions, 19 deletions
diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go index 2a88b0d..b3a526e 100644 --- a/src/rhctl/audio_switch.go +++ b/src/rhctl/audio_switch.go @@ -48,11 +48,6 @@ type SwitchResponse struct { Message string } -type SwitchCommand struct { - Cmd SwitchCmdString - Response chan<- SwitchResponse -} - type SwitchUpdateType uint8 const ( @@ -159,7 +154,7 @@ func (sw *AudioSwitch) Run() { return case cmd := <-sw.Commands: sw.current = cmd - sw.port.tx <- cmd.Cmd.Generate(sw.unit) + sw.port.tx <- cmd.Cmd.Generate(append(cmd.Args, sw.unit)...) sw.timer = time.NewTimer(sw.timeout) case data := <-sw.port.rx: sw.handleData(data) diff --git a/src/rhctl/audio_switch_command.go b/src/rhctl/audio_switch_command.go index 693689c..c90b1e4 100644 --- a/src/rhctl/audio_switch_command.go +++ b/src/rhctl/audio_switch_command.go @@ -171,3 +171,36 @@ func (c SwitchCmdString) Generate(args ...interface{}) string { } return s } + +type SwitchCommand struct { + Cmd SwitchCmdString + Args []interface{} + Response chan<- interface{} +} + +func NewSwitchCommandFromStrings(cmd string, args ...string) (c *SwitchCommand, err error) { + c = &SwitchCommand{} + switch cmd { + case "status": + if len(args) == 0 { + return nil, fmt.Errorf("missing argument <status-type>") + } + switch args[0] { + case "audio": + c.Cmd = SwitchCmdStatusAudio + case "gpi": + c.Cmd = SwitchCmdStatusGPI + case "oc": + c.Cmd = SwitchCmdStatusOC + case "relay": + c.Cmd = SwitchCmdStatusRelay + case "silence": + c.Cmd = SwitchCmdStatusSilence + default: + return nil, fmt.Errorf("unknown status-type: '%s'", args[0]) + } + default: + return nil, fmt.Errorf("unknown command '%s'", cmd) + } + return +} diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index df3dce9..ca27b86 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -22,6 +22,8 @@ package main import ( + "fmt" + "github.com/btittelbach/pubsub" ) @@ -46,8 +48,9 @@ func (c CommandType) String() string { } type Command struct { - Type CommandType - args []string + Type CommandType + Args []string + Response chan<- interface{} } type SwitchControl struct { @@ -58,9 +61,19 @@ type SwitchControl struct { } func (ctrl *SwitchControl) handleCommand(cmd *Command) { - rhdl.Printf("Telnet: got command: %+v", cmd) switch cmd.Type { case CmdStatus: + if len(cmd.Args) == 0 { + rhl.Printf("SwitchCTRL: ignoring empty raw switch command") + return + } + c, err := NewSwitchCommandFromStrings(cmd.Args[0], cmd.Args[1:]...) + if err != nil { + cmd.Response <- fmt.Errorf("switch command syntax error: %s", err.Error()) + return + } + c.Response = cmd.Response + ctrl.sw.Commands <- c case CmdServer: case CmdSwitch: } diff --git a/src/rhctl/telnet.go b/src/rhctl/telnet.go index 4189fc8..c4212e6 100644 --- a/src/rhctl/telnet.go +++ b/src/rhctl/telnet.go @@ -29,7 +29,7 @@ type TelnetInterface struct { server *telgo.Server } -func telnetStatus(c *telgo.Client, args []string, ctrl *SwitchControl) bool { +func telnetCmdStatus(c *telgo.Client, args []string, ctrl *SwitchControl) bool { ctrl.Commands <- &Command{Type: CmdStatus} // TODO: implement this return false @@ -64,7 +64,7 @@ func telnetUpdateListener(c *telgo.Client, ctrl *SwitchControl) { } } -func telnetListen(c *telgo.Client, args []string, ctrl *SwitchControl) bool { +func telnetCmdListen(c *telgo.Client, args []string, ctrl *SwitchControl) bool { if len(args) <= 1 { c.Sayln("missing argument: <type>") return false @@ -101,15 +101,30 @@ func telnetListen(c *telgo.Client, args []string, ctrl *SwitchControl) bool { return false } -func telnetServer(c *telgo.Client, args []string, ctrl *SwitchControl) bool { +func telnetCmdServer(c *telgo.Client, args []string, ctrl *SwitchControl) bool { ctrl.Commands <- &Command{Type: CmdServer} // TODO: implement this return false } -func telnetSwitch(c *telgo.Client, args []string, ctrl *SwitchControl) bool { - ctrl.Commands <- &Command{Type: CmdSwitch} - // TODO: implement this +func telnetCmdSwitch(c *telgo.Client, args []string, ctrl *SwitchControl) bool { + if len(args) < 2 { + c.Sayln("missing switch command") + return false + } + resp := make(chan interface{}) + ctrl.Commands <- &Command{Type: CmdStatus, Args: args[1:], Response: resp} + r := <-resp + switch r.(type) { + case error: + c.Sayln("%v", r) + case SwitchResponse: + if r.(SwitchResponse).Result != SwitchOK { + c.Sayln("%v: %s", r.(SwitchResponse).Result, r.(SwitchResponse).Message) + } + default: + c.Sayln("invalid response of type %T: %+v", r, r) + } return false } @@ -179,10 +194,10 @@ func TelnetInit(conf *Config, ctrl *SwitchControl) (telnet *TelnetInterface) { telnet = &TelnetInterface{} cmdlist := make(telgo.CmdList) - cmdlist["status"] = func(c *telgo.Client, args []string) bool { return telnetStatus(c, args, ctrl) } - cmdlist["listen"] = func(c *telgo.Client, args []string) bool { return telnetListen(c, args, ctrl) } - cmdlist["server"] = func(c *telgo.Client, args []string) bool { return telnetServer(c, args, ctrl) } - cmdlist["switch"] = func(c *telgo.Client, args []string) bool { return telnetSwitch(c, args, ctrl) } + cmdlist["status"] = func(c *telgo.Client, args []string) bool { return telnetCmdStatus(c, args, ctrl) } + cmdlist["listen"] = func(c *telgo.Client, args []string) bool { return telnetCmdListen(c, args, ctrl) } + cmdlist["server"] = func(c *telgo.Client, args []string) bool { return telnetCmdServer(c, args, ctrl) } + cmdlist["switch"] = func(c *telgo.Client, args []string) bool { return telnetCmdSwitch(c, args, ctrl) } cmdlist["help"] = telnetHelp cmdlist["quit"] = telnetQuit |