mirror of https://bitbucket.org/ausocean/av.git
234 lines
5.4 KiB
Go
234 lines
5.4 KiB
Go
/*
|
|
NAME
|
|
revid-cli - command line interface for revid.
|
|
|
|
DESCRIPTION
|
|
See Readme.md
|
|
|
|
AUTHORS
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
Jack Richardson <jack@ausocean.org>
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
LICENSE
|
|
revid-cli is Copyright (C) 2017-2018 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
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
*/
|
|
|
|
// revid-cli is a command line interface for revid.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"runtime/pprof"
|
|
"strconv"
|
|
"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/netlogger"
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
|
"bitbucket.org/ausocean/iot/pi/sds"
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
// Copyright information prefixed to all metadata.
|
|
const (
|
|
metaPreambleKey = "copyright"
|
|
metaPreambleData = "ausocean.org/license/content2019"
|
|
)
|
|
|
|
// Logging configuration.
|
|
const (
|
|
logPath = "/var/log/netsender/netsender.log"
|
|
logMaxSize = 500 // MB
|
|
logMaxBackup = 10
|
|
logMaxAge = 28 // days
|
|
logVerbosity = logger.Info
|
|
logSuppress = true
|
|
)
|
|
|
|
// Revid modes.
|
|
const (
|
|
modeNormal = "Normal"
|
|
modePaused = "Paused"
|
|
modeBurst = "Burst"
|
|
)
|
|
|
|
// Misc consts.
|
|
const (
|
|
netSendRetryTime = 5 * time.Second
|
|
defaultSleepTime = 60 // Seconds
|
|
profilePath = "rv.prof"
|
|
pkg = "revid-cli:"
|
|
)
|
|
|
|
// canProfile is set to false with revid-cli is built with "-tags profile".
|
|
var canProfile = true
|
|
|
|
func main() {
|
|
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})
|
|
|
|
fileLog := &lumberjack.Logger{
|
|
Filename: logPath,
|
|
MaxSize: logMaxSize,
|
|
MaxBackups: logMaxBackup,
|
|
MaxAge: logMaxAge,
|
|
}
|
|
|
|
netLog := netlogger.New()
|
|
|
|
log := logger.New(logVerbosity, io.MultiWriter(fileLog, netLog), logSuppress)
|
|
|
|
if canProfile {
|
|
profile(log)
|
|
defer pprof.StopCPUProfile()
|
|
}
|
|
|
|
var rv *revid.Revid
|
|
|
|
ns, err := netsender.New(log, nil, readPin(rv), nil, config.TypeData)
|
|
if err != nil {
|
|
log.Log(logger.Fatal, pkg+"could not initialise netsender client: "+err.Error())
|
|
}
|
|
|
|
run(rv, ns, log, netLog)
|
|
}
|
|
|
|
func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger.Logger) {
|
|
var vs int
|
|
for {
|
|
err := ns.Run()
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"Run Failed. Retrying...", "error", err.Error())
|
|
time.Sleep(netSendRetryTime)
|
|
continue
|
|
}
|
|
|
|
err = nl.Send(ns)
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"Logs could not be sent", "error", err.Error())
|
|
}
|
|
|
|
// If var sum hasn't changed we continue.
|
|
newVs := ns.VarSum()
|
|
if vs == newVs {
|
|
sleep(ns, l)
|
|
continue
|
|
}
|
|
vs = newVs
|
|
|
|
vars, err := ns.Vars()
|
|
if err != nil {
|
|
l.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error())
|
|
time.Sleep(netSendRetryTime)
|
|
continue
|
|
}
|
|
|
|
if rv == nil {
|
|
rv, err = revid.New(config.Config{Logger: l}, ns)
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"could not initialise revid", "error", err.Error())
|
|
sleep(ns, l)
|
|
continue
|
|
}
|
|
}
|
|
|
|
err = rv.Update(vars)
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"Couldn't update revid", "error", err.Error())
|
|
sleep(ns, l)
|
|
continue
|
|
}
|
|
|
|
switch ns.Mode() {
|
|
case modePaused:
|
|
rv.Stop()
|
|
case modeNormal:
|
|
err = rv.Start()
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"could not start revid", "error", err.Error())
|
|
ns.SetMode(modePaused, &vs)
|
|
sleep(ns, l)
|
|
continue
|
|
}
|
|
case modeBurst:
|
|
err = burst(l, rv, ns)
|
|
if err != nil {
|
|
l.Log(logger.Warning, pkg+"could not start burst", "error", err.Error())
|
|
ns.SetMode(modePaused, &vs)
|
|
sleep(ns, l)
|
|
continue
|
|
}
|
|
ns.SetMode(modePaused, &vs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func profile(l *logger.Logger) {
|
|
f, err := os.Create(profilePath)
|
|
if err != nil {
|
|
l.Log(logger.Fatal, pkg+"could not create CPU profile", "error", err.Error())
|
|
}
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
l.Log(logger.Fatal, pkg+"could not start CPU profile", "error", err.Error())
|
|
}
|
|
}
|
|
|
|
func sleep(ns *netsender.Sender, l *logger.Logger) {
|
|
t, err := strconv.Atoi(ns.Param("mp"))
|
|
if err != nil {
|
|
l.Log(logger.Error, pkg+"could not get sleep time, using default", "error", err)
|
|
t = defaultSleepTime
|
|
}
|
|
time.Sleep(time.Duration(t) * time.Second)
|
|
}
|
|
|
|
func readPin(rv *revid.Revid) func(pin *netsender.Pin) error {
|
|
return func(pin *netsender.Pin) error {
|
|
switch {
|
|
case pin.Name == "X23":
|
|
pin.Value = -1
|
|
if rv != nil {
|
|
pin.Value = rv.Bitrate()
|
|
}
|
|
case pin.Name[0] == 'X':
|
|
return sds.ReadSystem(pin)
|
|
default:
|
|
pin.Value = -1
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|