xmtop/devices/temp_darwin.go

82 lines
1.9 KiB
Go
Raw Normal View History

// +build darwin
package devices
2018-05-16 04:48:26 +08:00
import (
"bytes"
2021-02-04 04:27:46 +08:00
_ "embed"
"encoding/csv"
"github.com/shirou/gopsutil/host"
"io"
2021-02-04 04:27:46 +08:00
"log"
)
// All possible thermometers
func devs() []string {
// Did we already populate the sensorMap?
if sensorMap != nil {
return defs()
}
// Otherwise, get the sensor data from the system & filter it
ids := loadIDs()
sensors, err := host.SensorsTemperatures()
2020-04-23 02:33:51 +08:00
if err != nil {
2021-02-04 04:27:46 +08:00
log.Printf("error getting sensor list for temps: %s", err)
return []string{}
2018-05-16 04:48:26 +08:00
}
rv := make([]string, 0, len(sensors))
sensorMap = make(map[string]string)
for _, sensor := range sensors {
// 0-value sensors are not implemented
if sensor.Temperature == 0 {
continue
}
if label, ok := ids[sensor.SensorKey]; ok {
2021-02-04 04:27:46 +08:00
sensorMap[sensor.SensorKey] = label
rv = append(rv, label)
}
2018-05-16 04:48:26 +08:00
}
return rv
2018-05-16 04:48:26 +08:00
}
// Only the ones filtered
func defs() []string {
rv := make([]string, 0, len(sensorMap))
for _, val := range sensorMap {
rv = append(rv, val)
}
return rv
}
2021-02-04 04:27:46 +08:00
//go:embed "smc.tsv"
var smcData []byte
// loadIDs parses the embedded smc.tsv data that maps Darwin SMC
// sensor IDs to their human-readable labels into an array and returns the
// array. The array keys are the 4-letter sensor keys; the values are the
// human labels.
func loadIDs() map[string]string {
rv := make(map[string]string)
2021-02-04 04:27:46 +08:00
parser := csv.NewReader(bytes.NewReader(smcData))
parser.Comma = '\t'
var line []string
2021-02-04 04:27:46 +08:00
var err error
for {
if line, err = parser.Read(); err == io.EOF {
break
}
if err != nil {
2021-02-04 04:27:46 +08:00
log.Printf("error parsing SMC tags for temp widget: %s", err)
break
}
// The line is malformed if len(line) != 2, but because the asset is static
// it makes no sense to report the error to downstream users. This must be
// tested at/around compile time.
// FIXME assert all lines in smc.tsv have 2 columns during unit tests
if len(line) == 2 {
rv[line[0]] = line[1]
}
}
return rv
}