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"
|
2021-02-04 04:27:46 +08:00
|
|
|
_ "embed"
|
2020-06-09 22:03:58 +08:00
|
|
|
"encoding/csv"
|
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
"io"
|
2021-02-04 04:27:46 +08:00
|
|
|
"log"
|
2020-06-09 22:03:58 +08:00
|
|
|
)
|
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 {
|
2021-02-04 04:27:46 +08:00
|
|
|
log.Printf("error getting sensor list for temps: %s", err)
|
2020-06-09 22:03:58 +08:00
|
|
|
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 {
|
2021-02-04 04:27:46 +08:00
|
|
|
sensorMap[sensor.SensorKey] = label
|
2020-06-09 22:03:58 +08:00
|
|
|
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
|
|
|
|
2021-02-04 04:27:46 +08:00
|
|
|
//go:embed "smc.tsv"
|
|
|
|
var smcData []byte
|
|
|
|
|
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)
|
2021-02-04 04:27:46 +08:00
|
|
|
parser := csv.NewReader(bytes.NewReader(smcData))
|
2020-06-09 22:03:58 +08:00
|
|
|
parser.Comma = '\t'
|
|
|
|
var line []string
|
2021-02-04 04:27:46 +08:00
|
|
|
var err error
|
2020-06-09 22:03:58 +08:00
|
|
|
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)
|
2020-06-09 22:03:58 +08:00
|
|
|
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
|
|
|
|
}
|