From af3c3e7cf792eb21e5fe9c96237723561644b7c4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 26 Mar 2016 20:57:22 +0100 Subject: disconnecting serial devices is now properly handled diff --git a/sample-config.toml b/sample-config.toml index e5f87df..6709572 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -11,11 +11,11 @@ timeout = "500ms" heartbeat_dev = "/dev/ttyUSB2" heartbeat_baud = 19200 - [servers.standby] - ctrl_dev = "/dev/ttyUSB3" - ctrl_baud = 38400 - heartbeat_dev = "/dev/ttyUSB4" - heartbeat_baud = 19200 + # [servers.standby] + # ctrl_dev = "/dev/ttyUSB3" + # ctrl_baud = 38400 + # heartbeat_dev = "/dev/ttyUSB4" + # heartbeat_baud = 19200 [clients] diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go index 1368da8..5fc9fac 100644 --- a/src/rhctl/audio_switch.go +++ b/src/rhctl/audio_switch.go @@ -23,7 +23,6 @@ package main import ( "fmt" - "time" ) type AudioSwitch struct { @@ -32,15 +31,22 @@ type AudioSwitch struct { func (sw *AudioSwitch) Run() { rhdl.Printf("running audio switch") - sw.port.tx <- "hello switch" - time.Sleep(10 * time.Second) + stop := make(chan bool) + sw.port.Run(stop) + + select { + case <-stop: + return + case data := <-sw.port.rx: + rhl.Printf("got data from audio switch: %q", data) + } } func SwitchInit(conf *Config) (sw *AudioSwitch, err error) { sw = &AudioSwitch{} - if sw.port, err = SerialOpenAndHandle(conf.Audioswitch.Device, conf.Audioswitch.Baudrate, ""); err != nil { + if sw.port, err = SerialOpen(conf.Audioswitch.Device, conf.Audioswitch.Baudrate, ""); err != nil { err = fmt.Errorf("Error opening switch port: %s", err) return } diff --git a/src/rhctl/main.go b/src/rhctl/main.go index e6164a0..23a7f13 100644 --- a/src/rhctl/main.go +++ b/src/rhctl/main.go @@ -92,6 +92,10 @@ func main() { } servers = append(servers, server) } + if len(servers) <= 0 { + rhl.Printf("Error: there is no playout server configured...") + return + } // running essential parts stop := make(chan bool) diff --git a/src/rhctl/playout_server.go b/src/rhctl/playout_server.go index 5491b23..276c462 100644 --- a/src/rhctl/playout_server.go +++ b/src/rhctl/playout_server.go @@ -43,10 +43,10 @@ type PlayoutServer struct { func (srv *PlayoutServer) Run() { rhdl.Printf("running playout server: %s", srv.name) - srv.control.tx <- fmt.Sprintf("hello %s", srv.name) - srv.heartbeat.tx <- fmt.Sprintf("beat %s", srv.name) - time.Sleep(5 * time.Second) + for { + time.Sleep(time.Second) + } } func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) { @@ -55,11 +55,11 @@ func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) { srv.health = Dead srv.channel = "music" - if srv.control, err = SerialOpenAndHandle(conf.Servers[name].ControlDevice, conf.Servers[name].ControlBaudrate, "\r\n"); err != nil { + if srv.control, err = SerialOpen(conf.Servers[name].ControlDevice, conf.Servers[name].ControlBaudrate, "\r\n"); err != nil { err = fmt.Errorf("Error opening control port(%s): %s", srv.name, err) return } - if srv.heartbeat, err = SerialOpenAndHandle(conf.Servers[name].HeartbeatDevice, conf.Servers[name].HeartbeatBaudrate, "\r\n"); err != nil { + if srv.heartbeat, err = SerialOpen(conf.Servers[name].HeartbeatDevice, conf.Servers[name].HeartbeatBaudrate, "\r\n"); err != nil { err = fmt.Errorf("Error opening control port(%s): %s", srv.name, err) return } diff --git a/src/rhctl/serial_port.go b/src/rhctl/serial_port.go index c7804a6..e809484 100644 --- a/src/rhctl/serial_port.go +++ b/src/rhctl/serial_port.go @@ -127,44 +127,61 @@ func (b *Baudrate) UnmarshalTOML(data []byte) error { } type SerialPort struct { - port *sio.Port - rx <-chan string - tx chan<- string + port *sio.Port + rx <-chan string + tx chan<- string + newline string } -func serialReader(c chan<- string, port *sio.Port) { +func serialReader(c chan<- string, port *sio.Port, stop chan<- bool) { + defer func() { + port.Close() + stop <- true + }() + scanner := bufio.NewScanner(port) scanner.Split(bufio.ScanLines) for scanner.Scan() { - if err := scanner.Err(); err != nil { - panic(err.Error()) - } data := scanner.Text() if len(data) == 0 { continue } c <- string(data) } + if err := scanner.Err(); err != nil { + rhl.Printf("device(%s): read error: %s", port.LocalAddr(), err) + panic(err.Error()) + } else { + rhl.Printf("device(%s): closed", port.LocalAddr()) + } } -func serialWriter(c <-chan string, port *sio.Port, newline string) { +func serialWriter(c <-chan string, port *sio.Port, newline string, stop chan<- bool) { + defer func() { + port.Close() + stop <- true + }() + for data := range c { port.Write([]byte(data + newline)) } - port.Close() } -func SerialOpenAndHandle(device string, rate Baudrate, newline string) (port *SerialPort, err error) { - port = &SerialPort{} - if port.port, err = sio.Open(device, rate.code); err != nil { - return - } +func (port *SerialPort) Run(stop chan<- bool) (err error) { tx := make(chan string, 10) rx := make(chan string, 20) - go serialReader(rx, port.port) - go serialWriter(tx, port.port, newline) + go serialReader(rx, port.port, stop) + go serialWriter(tx, port.port, port.newline, stop) port.rx = rx port.tx = tx return } + +func SerialOpen(device string, rate Baudrate, newline string) (port *SerialPort, err error) { + port = &SerialPort{newline: newline} + if port.port, err = sio.Open(device, rate.code); err != nil { + return + } + return +} -- cgit v0.10.2