From bbbd6baecb013d855c17071b78ba3d90a877a125 Mon Sep 17 00:00:00 2001 From: David Sutton Date: Thu, 1 Jun 2023 09:30:55 +0930 Subject: [PATCH] Better handle errors in setChannels --- cmd/treatment/main.go | 57 +++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/cmd/treatment/main.go b/cmd/treatment/main.go index 31287927..c6460456 100644 --- a/cmd/treatment/main.go +++ b/cmd/treatment/main.go @@ -35,6 +35,7 @@ import ( "io" "os/exec" "strconv" + "strings" "time" "bitbucket.org/ausocean/av/container/mts" @@ -87,7 +88,7 @@ const ( // Variable map to send to netreceiver/vidgrind. var varMap = map[string]string{ - "speakerMode": "enum:Stereo,LeftMono,RightMono,Mute", + "SpeakerMode": "enum:"+strings.Join([]string{modeStereo, modeLeft, modeRight, modeMute}, ","), "AudioFilePath": "string", } @@ -131,7 +132,7 @@ func main() { log.Fatal("could not initialise revid", "error", err) } - // Play the audio (audio will play even whilst muted). + // Play the audio (audio will play even while muted). log.Debug("Playing the audio") go playAudio(filePtr, log) @@ -143,9 +144,7 @@ func main() { // 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, file *string, l logging.Logger, nl *netlogger.Logger) { - var ( - vs int - ) + var vs int for { l.Debug("running netsender") @@ -208,32 +207,26 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, } l.Debug("checking mode") - setChannels(vars["speakerMode"], l) - // l.Info("revid updated with new mode") + _ = setChannels(vars["SpeakerMode"], l) sleep(ns, l) } } -// setChannels handles the muting of one, both, or neither of the channels. It takes in speakerMode +// setChannels handles the muting of one, both, or neither of the channels. It takes in SpeakerMode // and sets the relevant volumes. -func setChannels(mode string, l logging.Logger) { - vols := "" - +func setChannels(mode string, l logging.Logger) error { l.Info("mode is", "mode", mode) // Set the volume of each channel. - switch mode { - case modeStereo: - vols = "100%,100%" - case modeLeft: - vols = "0%,100%" - case modeRight: - vols = "100%,0%" - case modeMute: - vols = "0%,0%" - default: - // If an invalid option has been chosen, keep the previous setting. - return + vols := map[string]string{ + modeStereo: "100%,100%", + modeLeft: "0%,100%", + modeRight: "100%,0%", + modeMute: "0%,0%", + }[mode] + if vols == "" { + l.Warning("invalid SpeakeMode", "SpeakerMode", mode) + return fmt.Errorf("invalid SpeakerMode: %s", mode) } // Create the command to change the channel volumes. @@ -242,17 +235,20 @@ func setChannels(mode string, l logging.Logger) { // Pipe the output to stdout and stderr. outPipe, err := cmd.StdoutPipe() if err != nil { - l.Error("failed to pipe stdout", "error", err) + l.Error("unable to setup pipe to stdout", "error", err) + return fmt.Errorf("unable to setup pipe to stdout: %w", err) } errPipe, err := cmd.StderrPipe() if err != nil { - l.Error("failed to pipe stderr", "error", err) + l.Error("unable to setup pipe to stderr", "error", err) + return fmt.Errorf("unable to setup pipe to stderr: %w", err) } // Execute the channel setting command. err = cmd.Start() if err != nil { - l.Error("channel setting failed", "error", err) + l.Error("unable to set channel", "error", err) + return fmt.Errorf("unable to set channel: %w", err) } // Copy any std out to a buffer for logging. @@ -262,6 +258,7 @@ func setChannels(mode string, l logging.Logger) { if err != nil { l.Error("failed to copy out pipe", "error", err) } + l.Info("command run", "stdout", outBuff) }() // Copy any std error to a buffer for logging. @@ -271,12 +268,18 @@ func setChannels(mode string, l logging.Logger) { if err != nil { l.Error("failed to copy error pipe", "error", err) } + l.Error("command failed", "stderr", errBuff) }() + if errBuff.String() != "" { + return fmt.Errorf("channel set command failed: %s", &errBuff) + } + l.Info("mode set to", "mode", mode) + return nil } -// playAudio is intended to be run as a routine. It will continuously run even whilst muted. +// playAudio is intended to be run as a routine. It will continuously run even while muted. func playAudio(file *string, l logging.Logger) { var numPlays int for {