Netsender changes, notably common X pins now being handled now by the former.

This commit is contained in:
Alan Noble 2018-06-14 16:37:57 +09:30
parent b72d9bdd0d
commit 3f59d353c7
1 changed files with 12 additions and 65 deletions

View File

@ -28,20 +28,15 @@ LICENSE
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"bitbucket.org/ausocean/av/revid"
"bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/utils/smartlogger"
linuxproc "github.com/c9s/goprocinfo/linux"
)
const (
@ -87,12 +82,6 @@ const (
prepTime = 20 * time.Second
)
const (
cpuUsage = 20
cpuTemp = 21
revidBitrate = 23
)
// Globals
var (
revidInst *revid.Revid
@ -248,14 +237,14 @@ func main() {
config.Timeout = *configFlags[timeoutPtr]
config.IntraRefreshPeriod = *configFlags[intraRefreshPeriodPtr]
var ns netsender.Netsender
var ns netsender.Sender
var vs int
if *useNetsender {
// initialize NetSender and use NetSender's logger
config.Logger = netsender.GetLogger()
var err error
ns, err = netsender.NewNetsender(false, revidReportActions)
err = ns.Init(nil, nil, nil)
if err != nil {
l := smartlogger.New(loggerVerbosity, smartlogger.File, "/var/log/netsender/")
l.Log(progName, "Error", err.Error()) // TODO(kortschak): Make this "Fatal" when that exists.
@ -282,7 +271,7 @@ func main() {
if vs != ns.GetVarSum() {
// vars changed
vars, err := ns.GetVars()
vars, err := ns.Vars()
if err != nil {
config.Logger.Log(progName, "Error", err.Error())
time.Sleep(netSendRetryTime)
@ -313,7 +302,7 @@ func main() {
}
// sendTo handles NetReceiver configuration and sends requested data to the cloud.
func sendTo(ns netsender.Netsender) error {
func sendTo(ns netsender.Sender) error {
if !ns.IsConfigured() {
err := ns.Config()
if err != nil {
@ -321,7 +310,14 @@ func sendTo(ns netsender.Netsender) error {
}
}
inputs := strings.Split(ns.GetConfigParam("ip"), ",")
// populate input values, if any
inputs := netsender.MakePins(ns.GetConfigParam("ip"))
for i, pin := range inputs {
if pin.Name == "X23" {
inputs[i].Value = int(revidInst.GetBitrate())
}
}
_, reconfig, err := ns.Send(netsender.RequestPoll, inputs, nil)
if err != nil {
return err
@ -438,52 +434,3 @@ func updateRevid(vars map[string]string, stop bool) {
startRevid()
}
func revidReportActions(pin int) (int, error) {
switch {
//function to measure temp of cpu
case pin == cpuTemp:
var out []byte
var err error
var val float64
if out, err = exec.Command("/opt/vc/bin/vcgencmd", "measure_temp").Output(); err != nil {
return -1, errors.New("CPU Temp Read Err: " + err.Error())
}
if val, err = strconv.ParseFloat(string(out[5:len(out)-3]), 32); err != nil {
return -1, errors.New("CPU Temp Read Err: " + err.Error())
}
return int(val), nil
//function to measure usage of cpu
case pin == cpuUsage:
stat, err := linuxproc.ReadStat("/proc/stat")
if err != nil {
return -1, errors.New("CPU Uage Read Err: " + err.Error())
}
total1 := stat.CPUStatAll.User + stat.CPUStatAll.Nice + stat.CPUStatAll.System +
stat.CPUStatAll.Idle + stat.CPUStatAll.IOWait + stat.CPUStatAll.IRQ +
stat.CPUStatAll.SoftIRQ + stat.CPUStatAll.Steal + stat.CPUStatAll.Guest +
stat.CPUStatAll.GuestNice
idle1 := stat.CPUStatAll.Idle
time.Sleep(time.Millisecond * 1000)
stat, err = linuxproc.ReadStat("/proc/stat")
if err != nil {
return -1, errors.New("CPU Usage Read Err: " + err.Error())
}
total2 := stat.CPUStatAll.User + stat.CPUStatAll.Nice + stat.CPUStatAll.System +
stat.CPUStatAll.Idle + stat.CPUStatAll.IOWait + stat.CPUStatAll.IRQ +
stat.CPUStatAll.SoftIRQ + stat.CPUStatAll.Steal + stat.CPUStatAll.Guest +
stat.CPUStatAll.GuestNice
idle2 := stat.CPUStatAll.Idle
return int((1.0 - (float64(idle2-idle1) / float64(total2-total1))) * 100), nil
case pin == revidBitrate:
return int(revidInst.GetBitrate()), nil
default:
return -1, errors.New("External pin" + strconv.Itoa(pin) + " not defined")
}
}