Updated to use latest Netsender interface.

This commit is contained in:
Alan Noble 2018-06-01 20:50:56 +09:30
parent 6aee541256
commit 6241671275
1 changed files with 40 additions and 53 deletions

View File

@ -10,7 +10,7 @@ AUTHORS
Jack Richardson <jack@ausocean.org>
LICENSE
RevidCLI.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
RevidCLI.go 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
@ -69,6 +69,7 @@ const (
// Other misc consts
const (
progName = "revid-cli"
netSendRetryTime = 5
sleepTime = 2 * 43200
defaultRunDuration = 2 * 43200
@ -86,8 +87,7 @@ const (
// Globals
var (
varSum int = 0
vars = make(map[string]string)
ns netsender.Netsender
revidInst revid.Revid
config revid.Config
)
@ -241,11 +241,14 @@ func main() {
config.Timeout = *configFlags[timeoutPtr]
config.IntraRefreshPeriod = *configFlags[intraRefreshPeriodPtr]
//init netsender
var vs int
if *netSenderFlagPtr {
netsenderInit()
// initialize NetSender and use NetSender's logger
config.Logger = netsender.GetLogger()
ns = netsender.NewNetsender(false, revidReportActions)
vs = ns.GetVarSum()
} else {
// Also give the config a logger object
// alternatively, instantiate our own logger
config.Logger = smartlogger.New(loggerVerbosity, smartlogger.File, "/var/log/netsender/")
}
@ -253,29 +256,37 @@ func main() {
startRevid()
paused := false
// Is the netsender flag been used ? if so run this loop
// loop in NetSender mode
for *netSenderFlagPtr {
varsChanged, err := netSend()
if err != nil {
config.Logger.Log("revidCLI", "Error", err.Error())
if err := netSend(); err != nil {
config.Logger.Log(progName, "Error", err.Error())
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
continue
}
if varsChanged {
if vs != ns.GetVarSum() {
// vars changed
vars, err := ns.GetVars()
if err != nil {
config.Logger.Log(progName, "Error", err.Error())
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
continue
}
vs = ns.GetVarSum()
if vars["mode"] == "Paused" {
if !paused {
config.Logger.Log("revidCLI", "Info", "Pausing revid")
config.Logger.Log(progName, "Info", "Pausing revid")
stopRevid()
paused = true
}
} else {
updateRevid(!paused)
updateRevid(vars, !paused)
if paused {
paused = false
}
}
}
sleepTime, _ := strconv.Atoi(netsender.GetConfigParam("monPeriod"))
sleepTime, _ := strconv.Atoi(ns.GetConfigParam("monPeriod"))
time.Sleep(time.Duration(sleepTime) * time.Second)
}
@ -285,50 +296,26 @@ func main() {
stopRevid()
}
func netsenderInit() {
//initialize netsender and assign function to check X pins
config.Logger = netsender.GetLogger()
netsender.Init(false)
netsender.ExternalReader = revidReportActions
}
// netSend implements the NetSender client, and is called every monPeriod seconds.
// It handles NetReceiver configuration and sends requested data to the cloud.
// Finally, it updates vars and returns true when vars have changed
func netSend() (bool, error) {
var err error
if !netsender.IsConfigured() {
if err = netsender.GetConfig(); err != nil {
return false, err
func netSend() error {
if !ns.IsConfigured() {
if err := ns.Config(); err != nil {
return err
}
}
inputs := netsender.SplitCSV(netsender.GetConfigParam("inputs"))
var reconfig bool
if _, reconfig, err = netsender.Send(netsender.RequestPoll, inputs); err != nil {
return false, err
}
if reconfig {
if err = netsender.GetConfig(); err != nil {
return false, err
}
}
if cloudVarSum := netsender.GetVarSum(); cloudVarSum != varSum {
var newVars map[string]string
if newVars, err = netsender.GetVars(); err != nil {
return false, err
}
varSum = cloudVarSum
for newVar, value := range newVars {
if currentValue, varExists := vars[newVar]; !varExists || currentValue != value {
vars[newVar] = value
inputs := netsender.SplitCSV(ns.GetConfigParam("inputs"))
if _, reconfig, err := ns.Send(netsender.RequestPoll, inputs); err != nil {
return err
} else {
if reconfig {
if err := ns.Config(); err != nil {
return err
}
}
return true, nil
}
return false, nil
return nil
}
// wrappers for stopping and starting revid
@ -342,12 +329,12 @@ func stopRevid() {
time.Sleep(time.Duration(revidStopTime) * time.Second)
}
func updateRevid(stop bool) {
func updateRevid(vars map[string]string, stop bool) {
if stop {
stopRevid()
}
//look through var map and update revid where needed
//look through the vars and update revid where needed
for key, value := range vars {
switch key {
case "FramesPerClip":
@ -477,7 +464,7 @@ func createRevidInstance() {
// If the config does have a logger, use it to output error, otherwise
// just output to std output
if config.Logger != nil {
config.Logger.Log("revidCLI", "FATAL ERROR", err.Error())
config.Logger.Log(progName, "FATAL ERROR", err.Error())
} else {
fmt.Printf("FATAL ERROR: %v", err.Error())
}