xmtop/widgets/battery.go

107 lines
2.3 KiB
Go
Raw Normal View History

package widgets
import (
"fmt"
"log"
"math"
"strconv"
"time"
"github.com/distatus/battery"
"github.com/prometheus/client_golang/prometheus"
2019-01-31 07:20:09 +08:00
2020-02-29 04:48:35 +08:00
ui "github.com/xxxserxxx/gotop/v3/termui"
)
2019-03-01 08:29:52 +08:00
type BatteryWidget struct {
*ui.LineGraph
2019-03-01 08:29:52 +08:00
updateInterval time.Duration
metric []prometheus.Gauge
}
func NewBatteryWidget(horizontalScale int) *BatteryWidget {
2019-03-01 08:29:52 +08:00
self := &BatteryWidget{
LineGraph: ui.NewLineGraph(),
updateInterval: time.Minute,
}
2019-01-31 07:20:09 +08:00
self.Title = " Battery Status "
2019-01-13 08:31:37 +08:00
self.HorizontalScale = horizontalScale
2019-01-31 07:20:09 +08:00
// intentional duplicate
2019-03-01 08:29:52 +08:00
// adds 2 datapoints to the graph, otherwise the dot is difficult to see
2019-01-31 07:20:09 +08:00
self.update()
self.update()
go func() {
2019-03-01 08:29:52 +08:00
for range time.NewTicker(self.updateInterval).C {
self.Lock()
self.update()
self.Unlock()
}
}()
return self
}
func (b *BatteryWidget) EnableMetric() {
bats, err := battery.GetAll()
if err != nil {
log.Printf("error setting up metrics: %v", err)
return
}
b.metric = make([]prometheus.Gauge, len(bats))
for i, bat := range bats {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "battery",
Name: fmt.Sprintf("%d", i),
})
gauge.Set(bat.Current / bat.Full)
b.metric[i] = gauge
prometheus.MustRegister(gauge)
}
}
2019-03-01 08:29:52 +08:00
func makeId(i int) string {
return "Batt" + strconv.Itoa(i)
}
func (b *BatteryWidget) Scale(i int) {
b.LineGraph.HorizontalScale = i
}
2019-03-01 08:29:52 +08:00
func (self *BatteryWidget) update() {
batteries, err := battery.GetAll()
if err != nil {
switch errt := err.(type) {
case battery.ErrFatal:
log.Printf("fatal error fetching battery info: %v", err)
return
case battery.Errors:
batts := make([]*battery.Battery, 0)
for i, e := range errt {
if e == nil {
batts = append(batts, batteries[i])
} else {
log.Printf("recoverable error fetching battery info; skipping battery: %v", e)
}
}
if len(batts) < 1 {
log.Print("no usable batteries found")
return
}
batteries = batts
}
}
2019-03-01 08:29:52 +08:00
for i, battery := range batteries {
id := makeId(i)
perc := battery.Current / battery.Full
percentFull := math.Abs(perc) * 100.0
2019-03-01 08:29:52 +08:00
self.Data[id] = append(self.Data[id], percentFull)
self.Labels[id] = fmt.Sprintf("%3.0f%% %.0f/%.0f", percentFull, math.Abs(battery.Current), math.Abs(battery.Full))
if self.metric != nil {
self.metric[i].Set(perc)
}
}
}