revid: add Burst method to revid

This commit is contained in:
Scott 2020-01-30 13:20:44 +10:30
parent 6a580ce594
commit 054f0a5e77
2 changed files with 17 additions and 20 deletions

View File

@ -54,7 +54,6 @@ USAGE
package main
import (
"fmt"
"io"
"os"
"runtime/pprof"
@ -199,7 +198,8 @@ func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger.
continue
}
case modeBurst:
err = burst(l, rv, ns)
dur := time.Duration(rv.Config().BurstPeriod) * time.Second
err = rv.Burst(dur)
if err != nil {
l.Log(logger.Warning, pkg+"could not start burst", "error", err.Error())
ns.SetMode(modePaused, &vs)
@ -254,21 +254,3 @@ func readPin(rv *revid.Revid) func(pin *netsender.Pin) error {
return nil
}
}
// burst starts revid, waits for time specified in the Config.BurstPeriod
// field, and then stops revid.
//
// TODO: move this functionality to the revid API into a Revid.Burst(time) method.
func burst(l *logger.Logger, r *revid.Revid, s *netsender.Sender) error {
l.Log(logger.Info, pkg+"starting burst")
err := r.Start()
if err != nil {
return fmt.Errorf("could not start revid: %w", err)
}
time.Sleep(time.Duration(r.Config().BurstPeriod) * time.Second)
l.Log(logger.Info, pkg+"stopping burst")
r.Stop()
return nil
}

View File

@ -492,6 +492,21 @@ func (r *Revid) Stop() {
r.running = false
}
// Burst starts revid, waits for time specified, and then stops revid.
func (r *Revid) Burst(duration time.Duration) error {
r.cfg.Logger.Log(logger.Info, pkg+"starting burst")
err := r.Start()
if err != nil {
return fmt.Errorf("could not start revid: %w", err)
}
time.Sleep(duration)
r.cfg.Logger.Log(logger.Info, pkg+"stopping burst")
r.Stop()
return nil
}
func (r *Revid) IsRunning() bool {
r.mu.Lock()
defer r.mu.Unlock()