summaryrefslogtreecommitdiffstats
path: root/devices
diff options
context:
space:
mode:
authorSean E. Russell <ser@ser1.net>2020-04-23 13:01:13 -0500
committerSean E. Russell <ser@ser1.net>2020-04-23 13:01:13 -0500
commit6cee395f0636e17174881fa88e4a34f1f1e6a3dc (patch)
treee6b80632530cc635dd258346704bc81f67d0d0ce /devices
parent5ada5315d940038fcd83777b1e73797bbbada108 (diff)
Add sensible defaults for temps. Improve config writing.
Diffstat (limited to 'devices')
-rw-r--r--devices/devices.go37
-rw-r--r--devices/temp_darwin.go16
-rw-r--r--devices/temp_freebsd.go2
-rw-r--r--devices/temp_linux.go42
-rw-r--r--devices/temp_windows.go2
5 files changed, 70 insertions, 29 deletions
diff --git a/devices/devices.go b/devices/devices.go
index efdc404..4a029cd 100644
--- a/devices/devices.go
+++ b/devices/devices.go
@@ -3,12 +3,13 @@ package devices
import "log"
const (
- Temperatures = "Temperatures"
+ Temperatures = "Temperatures" // Device domain for temperature sensors
)
var Domains []string = []string{Temperatures}
-var shutdownFuncs []func() error
+var _shutdownFuncs []func() error
var _devs map[string][]string
+var _defaults map[string][]string
// RegisterShutdown stores a function to be called by gotop on exit, allowing
// extensions to properly release resources. Extensions should register a
@@ -16,14 +17,14 @@ var _devs map[string][]string
// released. The returned error will be logged, but no other action will be
// taken.
func RegisterShutdown(f func() error) {
- shutdownFuncs = append(shutdownFuncs, f)
+ _shutdownFuncs = append(_shutdownFuncs, f)
}
// Shutdown will be called by the `main()` function if gotop is exited
// cleanly. It will call all of the registered shutdown functions of devices,
// logging all errors but otherwise not responding to them.
func Shutdown() {
- for _, f := range shutdownFuncs {
+ for _, f := range _shutdownFuncs {
err := f()
if err != nil {
log.Print(err)
@@ -31,17 +32,31 @@ func Shutdown() {
}
}
-func RegisterDeviceList(typ string, f func() []string) {
+func RegisterDeviceList(typ string, all func() []string, def func() []string) {
if _devs == nil {
_devs = make(map[string][]string)
}
- if ls, ok := _devs[typ]; ok {
- _devs[typ] = append(ls, f()...)
- return
+ if _defaults == nil {
+ _defaults = make(map[string][]string)
}
- _devs[typ] = f()
+ if _, ok := _devs[typ]; !ok {
+ _devs[typ] = []string{}
+ }
+ _devs[typ] = append(_devs[typ], all()...)
+ if _, ok := _defaults[typ]; !ok {
+ _defaults[typ] = []string{}
+ }
+ _defaults[typ] = append(_defaults[typ], def()...)
}
-func Devices(domain string) []string {
- return _devs[domain]
+// Return a list of devices registered under domain, where `domain` is one of the
+// defined constants in `devices`, e.g., devices.Temperatures. The
+// `enabledOnly` flag determines whether all devices are returned (false), or
+// only the ones that have been enabled for the domain.
+func Devices(domain string, all bool) []string {
+ if all {
+ return _devs[domain]
+ } else {
+ return _defaults[domain]
+ }
}
diff --git a/devices/temp_darwin.go b/devices/temp_darwin.go
index fe98fc2..37559d1 100644
--- a/devices/temp_darwin.go
+++ b/devices/temp_darwin.go
@@ -6,7 +6,7 @@ import smc "github.com/xxxserxxx/iSMC"
func init() {
RegisterTemp(update)
- RegisterDeviceList(Temperatures, devs)
+ RegisterDeviceList(Temperatures, devs, defs)
ts = make(map[string]float32)
}
@@ -25,8 +25,6 @@ func update(temps map[string]int) map[string]error {
return nil
}
-// TODO: Set reasonable default devices
-// CPU (TC[01]P), GPU (TG0P), Memory (Ts0S) and Disk (TH0P)
func devs() []string {
rv := make([]string, len(smc.AppleTemp))
for i, v := range smc.AppleTemp {
@@ -34,3 +32,15 @@ func devs() []string {
}
return rv
}
+
+func defs() []string {
+ // CPU 0 CPU 1 GPU Memory Disk
+ ids := map[string]bool{"TC0P": true, "TC1P": true, "TG0P": true, "Ts0S": true, "TH0P": true}
+ rv := make([]string, 0, len(ids))
+ for _, v := range smc.AppleTemp {
+ if ids[v.Key] {
+ rv = append(rv, v.Desc)
+ }
+ }
+ return rv
+}
diff --git a/devices/temp_freebsd.go b/devices/temp_freebsd.go
index a0277a6..1a2104b 100644
--- a/devices/temp_freebsd.go
+++ b/devices/temp_freebsd.go
@@ -12,7 +12,7 @@ import (
func init() {
RegisterTemp(update)
- RegisterDeviceList(Temperatures, devs)
+ RegisterDeviceList(Temperatures, devs, devs)
}
var sensorOIDS = map[string]string{
diff --git a/devices/temp_linux.go b/devices/temp_linux.go
index 2ca01f5..09e7d3c 100644
--- a/devices/temp_linux.go
+++ b/devices/temp_linux.go
@@ -9,8 +9,9 @@ import (
)
func init() {
+ devs() // Populate the sensorMap
RegisterTemp(getTemps)
- RegisterDeviceList(Temperatures, devs)
+ RegisterDeviceList(Temperatures, devs, defs)
}
func getTemps(temps map[string]int) map[string]error {
@@ -19,30 +20,45 @@ func getTemps(temps map[string]int) map[string]error {
return map[string]error{"psHost": err}
}
for _, sensor := range sensors {
- // removes '_input' from the end of the sensor name
- idx := strings.Index(sensor.SensorKey, "_input")
- if idx >= 0 {
- label := sensor.SensorKey[:idx]
- if _, ok := temps[label]; ok {
- temps[label] = int(sensor.Temperature)
- }
+ label := sensorMap[sensor.SensorKey]
+ if _, ok := temps[label]; ok {
+ temps[label] = int(sensor.Temperature)
}
}
return nil
}
+// Optimization to avoid string manipulation every update
+var sensorMap map[string]string
+
func devs() []string {
+ if sensorMap == nil {
+ sensorMap = make(map[string]string)
+ }
sensors, err := psHost.SensorsTemperatures()
if err != nil {
return []string{}
}
rv := make([]string, 0, len(sensors))
for _, sensor := range sensors {
- // only sensors with input in their name are giving us live temp info
- if strings.Contains(sensor.SensorKey, "input") && sensor.Temperature != 0 {
- // removes '_input' from the end of the sensor name
- label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")]
- rv = append(rv, label)
+ label := sensor.SensorKey
+ if strings.Contains(sensor.SensorKey, "input") {
+ label = sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")]
+ }
+ rv = append(rv, label)
+ sensorMap[sensor.SensorKey] = label
+ }
+ return rv
+}
+
+// Only include sensors with input in their name; these are the only sensors
+// returning live data
+func defs() []string {
+ // MUST be called AFTER init()
+ rv := make([]string, 0)
+ for k, v := range sensorMap {
+ if k != v { // then it's an _input sensor
+ rv = append(rv, v)
}
}
return rv
diff --git a/devices/temp_windows.go b/devices/temp_windows.go
index 54fe0eb..108b923 100644
--- a/devices/temp_windows.go
+++ b/devices/temp_windows.go
@@ -8,7 +8,7 @@ import (
func init() {
RegisterTemp(update)
- RegisterDeviceList(Temperatures, devs)
+ RegisterDeviceList(Temperatures, devs, devs)
}
func update(temps map[string]int) map[string]error {