2020-02-18 03:00:52 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2020-02-28 08:51:28 +08:00
|
|
|
package devices
|
2018-05-16 04:48:26 +08:00
|
|
|
|
2020-06-09 22:03:58 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
"io"
|
|
|
|
)
|
2018-12-05 13:44:25 +08:00
|
|
|
|
2020-06-09 22:03:58 +08:00
|
|
|
// 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 {
|
2020-06-09 22:03:58 +08:00
|
|
|
// FIXME log an error here
|
|
|
|
return []string{}
|
2018-05-16 04:48:26 +08:00
|
|
|
}
|
2020-06-09 22:03:58 +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 {
|
|
|
|
sensorMap[sensor.SensorKey] = label
|
|
|
|
rv = append(rv, label)
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
2018-05-16 04:48:26 +08:00
|
|
|
}
|
2020-06-09 22:03:58 +08:00
|
|
|
return rv
|
2018-05-16 04:48:26 +08:00
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
|
2020-06-09 22:03:58 +08:00
|
|
|
// Only the ones filtered
|
|
|
|
func defs() []string {
|
|
|
|
rv := make([]string, 0, len(sensorMap))
|
|
|
|
for _, val := range sensorMap {
|
|
|
|
rv = append(rv, val)
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
2020-04-24 02:01:13 +08:00
|
|
|
|
2020-06-09 22:03:58 +08:00
|
|
|
// 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)
|
|
|
|
data, err := Asset("smc.tsv")
|
|
|
|
parser := csv.NewReader(bytes.NewReader(data))
|
|
|
|
parser.Comma = '\t'
|
|
|
|
var line []string
|
|
|
|
for {
|
|
|
|
if line, err = parser.Read(); err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// FIXME log an error here
|
|
|
|
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]
|
2020-04-24 02:01:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|