diff --git a/cmd/treatment/main.go b/cmd/treatment/main.go index dc7fde99..6d9fbb48 100644 --- a/cmd/treatment/main.go +++ b/cmd/treatment/main.go @@ -46,6 +46,7 @@ import ( "bitbucket.org/ausocean/iot/pi/netlogger" "bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/utils/logger" + _ "github.com/kidoman/embd/host/rpi" lumberjack "gopkg.in/natefinch/lumberjack.v2" ) @@ -109,7 +110,6 @@ func main() { if *filePtr == "" { log.Log(logger.Fatal, "no file path provided, check usage") } - cmd := exec.Command(audioCmd, *filePtr) // The netsender client will handle communication with netreceiver and GPIO stuff. log.Log(logger.Debug, "initialising netsender client") @@ -127,12 +127,12 @@ func main() { // Start the control loop. log.Log(logger.Debug, "starting control loop") - run(rv, ns, cmd, log, netLog) + run(rv, ns, filePtr, log, netLog) } // run starts a control loop that runs netsender, sends logs, checks for var changes, and // if var changes, changes current mode (paused,audio playback or soundcheck) -func run(rv *revid.Revid, ns *netsender.Sender, cmd *exec.Cmd, l *logger.Logger, nl *netlogger.Logger) { +func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger, nl *netlogger.Logger) { var ( wg sync.WaitGroup audioQuit chan struct{} @@ -196,7 +196,7 @@ func run(rv *revid.Revid, ns *netsender.Sender, cmd *exec.Cmd, l *logger.Logger, audioQuit = make(chan struct{}) treating = true wg.Add(1) - go playAudio(cmd, audioQuit, &wg, l) + go playAudio(file, audioQuit, &wg, l) } case modeCheck: stopAudio(&wg, &treating, audioQuit) @@ -217,9 +217,10 @@ func run(rv *revid.Revid, ns *netsender.Sender, cmd *exec.Cmd, l *logger.Logger, // playAudio is intended to be run as a routine. It will repeatedly play an audio file until // a signal is received to return. The entire audio file is played before the termination // signal chan is checked. -func playAudio(cmd *exec.Cmd, quit chan struct{}, wg *sync.WaitGroup, l *logger.Logger) { +func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.Logger) { var numPlays int for { + cmd := exec.Command(audioCmd, *file) // We'd like to see what the playback software is outputting, so pipe // stdout and stderr. outPipe, err := cmd.StdoutPipe() @@ -285,6 +286,9 @@ func playAudio(cmd *exec.Cmd, quit chan struct{}, wg *sync.WaitGroup, l *logger. // stopAudio signals to the playAudio routine to terminate and then waits for it to // do so. func stopAudio(wg *sync.WaitGroup, treating *bool, signal chan struct{}) { + if !*treating { + return + } close(signal) wg.Wait() *treating = false diff --git a/go.mod b/go.mod index 1d22f89b..83dffefb 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/go-audio/wav v0.0.0-20181013172942-de841e69b884 github.com/go-delve/delve v1.5.0 github.com/google/go-cmp v0.4.1 + github.com/kidoman/embd v0.0.0-20170508013040-d3d8c0c5c68d github.com/mewkiz/flac v1.0.5 github.com/pkg/errors v0.8.1 github.com/yobert/alsa v0.0.0-20180630182551-d38d89fa843e