2020-08-14 09:10:22 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
treatment is a netsender client intended to provide audio playback control,
|
2020-08-14 10:56:47 +03:00
|
|
|
and speaker health checking by reversing signal and recording using revid.
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2020-08-14 10:56:47 +03:00
|
|
|
Trek Hopton <trek@ausocean.org>
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package treatment is a program for playing and recording audio through a common
|
|
|
|
// speaker unit.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/container/mts"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
|
|
|
"bitbucket.org/ausocean/av/revid"
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/gpio"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/netlogger"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2021-03-03 14:12:01 +03:00
|
|
|
"github.com/kidoman/embd"
|
2020-08-21 14:51:13 +03:00
|
|
|
_ "github.com/kidoman/embd/host/rpi"
|
2020-08-14 09:10:22 +03:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Copyright information prefixed to all metadata.
|
|
|
|
const (
|
|
|
|
metaPreambleKey = "copyright"
|
|
|
|
metaPreambleData = "ausocean.org/license/content2020"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logging configuration.
|
|
|
|
const (
|
|
|
|
logPath = "/var/log/netsender/netsender.log"
|
|
|
|
logMaxSize = 500 // MB
|
|
|
|
logMaxBackup = 10
|
|
|
|
logMaxAge = 28 // days
|
2020-09-14 04:52:11 +03:00
|
|
|
logVerbosity = logger.Info
|
2020-08-14 09:10:22 +03:00
|
|
|
logSuppress = true
|
|
|
|
)
|
|
|
|
|
|
|
|
// Misc constants.
|
|
|
|
const (
|
|
|
|
netSendRetryTime = 5 * time.Second
|
|
|
|
defaultSleepTime = 60 // Seconds
|
|
|
|
pkg = "rv: "
|
2021-03-03 14:12:01 +03:00
|
|
|
minAmpVolume = 0
|
|
|
|
maxAmpVolume = 63
|
|
|
|
volAddr = 0x4B
|
2021-03-04 04:20:05 +03:00
|
|
|
i2cPort = 1
|
2020-08-14 09:10:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Treatment modes.
|
|
|
|
const (
|
|
|
|
modePaused = "Paused"
|
|
|
|
modeTreatment = "Play"
|
|
|
|
modeCheck = "Check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Variable map to send to netreceiver/vidgrind.
|
|
|
|
var varMap = map[string]string{
|
2021-02-11 02:23:35 +03:00
|
|
|
"mode": "enum:Paused,Play,Check",
|
2021-02-15 03:33:37 +03:00
|
|
|
"AudioFilePath": "string",
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})
|
|
|
|
|
|
|
|
// Set up the player command with audio file path.
|
2020-10-19 12:02:54 +03:00
|
|
|
filePtr := flag.String("path", "/home/pi/audio.wav", "Path to sound file we wish to play.")
|
2020-08-14 09:10:22 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Create lumberjack logger to handle logging to file.
|
|
|
|
fileLog := &lumberjack.Logger{
|
|
|
|
Filename: logPath,
|
|
|
|
MaxSize: logMaxSize,
|
|
|
|
MaxBackups: logMaxBackup,
|
|
|
|
MaxAge: logMaxAge,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create netlogger to handle logging to cloud.
|
|
|
|
netLog := netlogger.New()
|
|
|
|
|
|
|
|
// Create logger that we call methods on to log, which in turn writes to the
|
|
|
|
// lumberjack and netloggers.
|
|
|
|
log := logger.New(logVerbosity, io.MultiWriter(fileLog, netLog), logSuppress)
|
|
|
|
|
2020-08-19 06:13:56 +03:00
|
|
|
if *filePtr == "" {
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Fatal("no file path provided, check usage")
|
2020-08-19 06:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 09:10:22 +03:00
|
|
|
// The netsender client will handle communication with netreceiver and GPIO stuff.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("initialising netsender client")
|
2022-05-25 08:31:24 +03:00
|
|
|
ns, err := netsender.New(log, gpio.InitPin, nil, gpio.WritePin, netsender.WithVarTypes(varMap))
|
2020-08-14 09:10:22 +03:00
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
log.Fatal("could not initialise netsender client", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revid will handle the recording and sending of audio for sound checking.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("initialising revid")
|
2020-09-14 04:52:11 +03:00
|
|
|
rv, err := revid.New(config.Config{Logger: log}, ns)
|
2020-08-14 09:10:22 +03:00
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
log.Fatal("could not initialise revid", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the control loop.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("starting control loop")
|
2020-08-21 14:51:13 +03:00
|
|
|
run(rv, ns, filePtr, log, netLog)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2020-08-21 14:51:13 +03:00
|
|
|
func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger, nl *netlogger.Logger) {
|
2020-08-14 09:10:22 +03:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
audioQuit chan struct{}
|
|
|
|
treating bool
|
|
|
|
vs int
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("running netsender")
|
2020-08-14 09:10:22 +03:00
|
|
|
err := ns.Run()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning("run failed. Retrying...", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("sending logs")
|
2020-08-14 09:10:22 +03:00
|
|
|
err = nl.Send(ns)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning(pkg+"Logs could not be sent", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("checking varsum")
|
2020-08-14 09:10:22 +03:00
|
|
|
newVs := ns.VarSum()
|
|
|
|
if vs == newVs {
|
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vs = newVs
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("varsum changed", "vs", vs)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("getting new vars")
|
2020-08-14 09:10:22 +03:00
|
|
|
vars, err := ns.Vars()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"netSender failed to get vars", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("got new vars", "vars", vars)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// Configure revid based on the vars.
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("updating revid configuration")
|
2020-08-14 09:10:22 +03:00
|
|
|
err = rv.Update(vars)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning(pkg+"couldn't update revid", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("revid successfully reconfigured")
|
2020-08-14 09:10:22 +03:00
|
|
|
|
2021-03-03 14:12:01 +03:00
|
|
|
l.Debug("checking amplifier volume")
|
|
|
|
v := vars["AmpVolume"]
|
|
|
|
if v != "" {
|
|
|
|
vol, err := strconv.ParseInt(v, 10, 8)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"failed to parse amplifier volume", "error", err)
|
2021-03-03 14:12:01 +03:00
|
|
|
} else if vol < minAmpVolume || vol > maxAmpVolume {
|
|
|
|
l.Error(fmt.Sprintf("%s invalid amplifier volume, must be between %v and %v", pkg, minAmpVolume, maxAmpVolume), "volume", vol)
|
|
|
|
} else {
|
2021-03-04 04:20:05 +03:00
|
|
|
bus := embd.NewI2CBus(i2cPort)
|
2021-03-03 14:12:01 +03:00
|
|
|
err := bus.WriteByte(volAddr, byte(vol))
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"failed to write amplifier volume", "error", err)
|
2021-03-03 14:12:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("checking mode")
|
2020-08-14 09:10:22 +03:00
|
|
|
switch ns.Mode() {
|
|
|
|
case modePaused:
|
|
|
|
stopAudio(&wg, &treating, audioQuit)
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("mode is Paused, stopping revid")
|
2020-08-14 09:10:22 +03:00
|
|
|
rv.Stop()
|
|
|
|
case modeTreatment:
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("checking audio file path")
|
|
|
|
f := vars["AudioFilePath"]
|
2021-02-11 05:55:04 +03:00
|
|
|
if f != "" && *file != f {
|
|
|
|
file = &f
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("updated audio file path, stopping audio", "AudioFilePath", f)
|
2021-02-11 05:55:04 +03:00
|
|
|
stopAudio(&wg, &treating, audioQuit)
|
|
|
|
}
|
2020-08-14 09:10:22 +03:00
|
|
|
if !treating {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("starting audio treatment")
|
2020-08-14 09:10:22 +03:00
|
|
|
rv.Stop()
|
|
|
|
audioQuit = make(chan struct{})
|
|
|
|
treating = true
|
|
|
|
wg.Add(1)
|
2020-08-21 14:51:13 +03:00
|
|
|
go playAudio(file, audioQuit, &wg, l)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
case modeCheck:
|
|
|
|
stopAudio(&wg, &treating, audioQuit)
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("sound checking")
|
2020-08-14 09:10:22 +03:00
|
|
|
err = rv.Start()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error("could not start revid", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
ns.SetMode(modePaused, &vs)
|
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-10 06:36:22 +03:00
|
|
|
default:
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Warning("mode is not valid", "mode", ns.Mode())
|
2020-09-10 06:36:22 +03:00
|
|
|
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("revid updated with new mode")
|
2020-08-14 09:10:22 +03:00
|
|
|
sleep(ns, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-08-21 14:51:13 +03:00
|
|
|
func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.Logger) {
|
2020-08-14 09:10:22 +03:00
|
|
|
var numPlays int
|
|
|
|
for {
|
2020-08-21 14:51:13 +03:00
|
|
|
cmd := exec.Command(audioCmd, *file)
|
2020-08-14 09:10:22 +03:00
|
|
|
// We'd like to see what the playback software is outputting, so pipe
|
|
|
|
// stdout and stderr.
|
|
|
|
outPipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to pipe stdout", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
errPipe, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to pipe stderr", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start playback of the audio file.
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error("start failed", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
numPlays++
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("playing audio", "numPlays", numPlays)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// Copy any std out to a buffer for logging.
|
|
|
|
var outBuff bytes.Buffer
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(&outBuff, outPipe)
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to copy out pipe", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Copy any std error to a buffer for logging.
|
|
|
|
var errBuff bytes.Buffer
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(&errBuff, errPipe)
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to copy error pipe", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for playback to complete.
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error("failed to wait for execution finish", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("stdout received", "stdout", string(outBuff.Bytes()))
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// If there was any errors on stderr, log them.
|
|
|
|
if errBuff.Len() != 0 {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("errors from stderr", "stderr", string(errBuff.Bytes()))
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for audio signal halt.
|
|
|
|
// TODO: work out better way to do this. Doing it this way means we have to wait for
|
|
|
|
// the audio file to finish playing.
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}) {
|
2020-08-21 14:51:13 +03:00
|
|
|
if !*treating {
|
|
|
|
return
|
|
|
|
}
|
2020-08-14 09:10:22 +03:00
|
|
|
close(signal)
|
|
|
|
wg.Wait()
|
|
|
|
*treating = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// sleep uses a delay to halt the program based on the monitoring period
|
|
|
|
// netsender parameter (mp) defined in the netsender.conf config.
|
|
|
|
func sleep(ns *netsender.Sender, l *logger.Logger) {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("sleeping")
|
2020-08-14 09:10:22 +03:00
|
|
|
t, err := strconv.Atoi(ns.Param("mp"))
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error(pkg+"could not get sleep time, using default", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
t = defaultSleepTime
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(t) * time.Second)
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("finished sleeping")
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkPath wraps the use of lookPath to check the existence of executables
|
|
|
|
// that will be used by the audio looper.
|
|
|
|
func checkPath(cmd string, l *logger.Logger) {
|
|
|
|
path, err := exec.LookPath(cmd)
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Fatal(fmt.Sprintf("couldn't find %s", cmd), "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug(fmt.Sprintf("found %s", cmd), "path", path)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|