2018-12-27 12:06:55 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2019-02-16 20:12:52 +08:00
|
|
|
"github.com/distatus/battery"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
2020-04-24 03:07:08 +08:00
|
|
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
2018-12-27 12:06:55 +08:00
|
|
|
)
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
type BatteryWidget struct {
|
2018-12-27 12:06:55 +08:00
|
|
|
*ui.LineGraph
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval time.Duration
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:43:10 +08:00
|
|
|
func NewBatteryWidget(horizontalScale int) *BatteryWidget {
|
2019-03-01 08:29:52 +08:00
|
|
|
self := &BatteryWidget{
|
|
|
|
LineGraph: ui.NewLineGraph(),
|
|
|
|
updateInterval: time.Minute,
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
2020-06-19 08:51:01 +08:00
|
|
|
self.Title = tr.Value("widget.label.battery")
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-12-27 12:06:55 +08:00
|
|
|
|
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()
|
2018-12-27 12:06:55 +08:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
go func() {
|
2019-03-01 08:29:52 +08:00
|
|
|
for range time.NewTicker(self.updateInterval).C {
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Lock()
|
2018-12-27 12:06:55 +08:00
|
|
|
self.update()
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Unlock()
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2020-02-18 23:44:29 +08:00
|
|
|
func (b *BatteryWidget) EnableMetric() {
|
|
|
|
bats, err := battery.GetAll()
|
|
|
|
if err != nil {
|
2020-05-05 21:13:25 +08:00
|
|
|
log.Printf(tr.Value("error.metricsetup", "batt", err.Error()))
|
2020-02-18 23:44:29 +08:00
|
|
|
return
|
|
|
|
}
|
2020-05-13 07:42:45 +08:00
|
|
|
for i, _ := range bats {
|
|
|
|
id := makeID(i)
|
|
|
|
metrics.NewGauge(makeName("battery", i), func() float64 {
|
|
|
|
if ds, ok := b.Data[id]; ok {
|
|
|
|
return ds[len(ds)-1]
|
|
|
|
}
|
|
|
|
return 0.0
|
2020-02-18 23:44:29 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func makeID(i int) string {
|
2020-06-19 08:51:01 +08:00
|
|
|
return tr.Value("widget.label.batt") + strconv.Itoa(i)
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
func (b *BatteryWidget) Scale(i int) {
|
|
|
|
b.LineGraph.HorizontalScale = i
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (b *BatteryWidget) update() {
|
2019-03-01 08:29:52 +08:00
|
|
|
batteries, err := battery.GetAll()
|
2018-12-27 12:06:55 +08:00
|
|
|
if err != nil {
|
2020-02-21 08:57:06 +08:00
|
|
|
switch errt := err.(type) {
|
|
|
|
case battery.ErrFatal:
|
2020-05-05 21:13:25 +08:00
|
|
|
log.Printf(tr.Value("error.fatalfetch", "batt", err.Error()))
|
2020-02-21 08:57:06 +08:00
|
|
|
return
|
|
|
|
case battery.Errors:
|
|
|
|
batts := make([]*battery.Battery, 0)
|
|
|
|
for i, e := range errt {
|
|
|
|
if e == nil {
|
|
|
|
batts = append(batts, batteries[i])
|
|
|
|
} else {
|
2020-05-05 21:13:25 +08:00
|
|
|
log.Printf(tr.Value("error.recovfetch"), "batt", e.Error())
|
2020-02-21 08:57:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(batts) < 1 {
|
2020-05-05 21:13:25 +08:00
|
|
|
log.Print(tr.Value("error.nodevfound", "batt"))
|
2020-02-21 08:57:06 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
batteries = batts
|
|
|
|
}
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
2019-03-01 08:29:52 +08:00
|
|
|
for i, battery := range batteries {
|
2020-04-28 09:33:41 +08:00
|
|
|
id := makeID(i)
|
2020-02-18 23:44:29 +08:00
|
|
|
perc := battery.Current / battery.Full
|
|
|
|
percentFull := math.Abs(perc) * 100.0
|
2020-05-13 07:42:45 +08:00
|
|
|
// TODO: look into this sort of thing; doesn't the array grow forever? Is the widget library truncating it?
|
2020-04-28 09:33:41 +08:00
|
|
|
b.Data[id] = append(b.Data[id], percentFull)
|
|
|
|
b.Labels[id] = fmt.Sprintf("%3.0f%% %.0f/%.0f", percentFull, math.Abs(battery.Current), math.Abs(battery.Full))
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
}
|