summaryrefslogtreecommitdiffstats
path: root/devices/temp_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'devices/temp_linux.go')
-rw-r--r--devices/temp_linux.go42
1 files changed, 29 insertions, 13 deletions
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