summaryrefslogtreecommitdiffstats
path: root/devices/temp_linux.go
blob: 09e7d3c9c992693d104872567eba52d694a3c872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// +build linux

package devices

import (
	"strings"

	psHost "github.com/shirou/gopsutil/host"
)

func init() {
	devs() // Populate the sensorMap
	RegisterTemp(getTemps)
	RegisterDeviceList(Temperatures, devs, defs)
}

func getTemps(temps map[string]int) map[string]error {
	sensors, err := psHost.SensorsTemperatures()
	if err != nil {
		return map[string]error{"psHost": err}
	}
	for _, sensor := range sensors {
		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 {
		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
}