summaryrefslogtreecommitdiffstats
path: root/devices/temp_darwin.go
diff options
context:
space:
mode:
authorSean E. Russell <ser@ser1.net>2020-06-09 09:03:58 -0500
committerSean E. Russell <ser@ser1.net>2020-06-09 09:54:10 -0500
commit1181b94bd228f5f081d49f6c7c9a9ee9c26557ed (patch)
tree6c56836eefc5e5ec256ff8a3adfb674562a28f29 /devices/temp_darwin.go
parent5590679cb4cc4dcd6ccb4694780eda38326bc4c5 (diff)
Closes #131, SMC GPL issue on OSX; consequently removes need to CGO in darwin.v4.0.1
Diffstat (limited to 'devices/temp_darwin.go')
-rw-r--r--devices/temp_darwin.go85
1 files changed, 57 insertions, 28 deletions
diff --git a/devices/temp_darwin.go b/devices/temp_darwin.go
index 1111943..ecdc9f1 100644
--- a/devices/temp_darwin.go
+++ b/devices/temp_darwin.go
@@ -2,45 +2,74 @@
package devices
-// TODO gopsutil team reports this is not needed; try getting rid of this dep
-import smc "github.com/xxxserxxx/iSMC"
+import (
+ "bytes"
+ "encoding/csv"
+ "github.com/shirou/gopsutil/host"
+ "io"
+)
-func init() {
- RegisterTemp(update)
- RegisterDeviceList(Temperatures, devs, defs)
- ts = make(map[string]float32)
-}
-
-var ts map[string]float32
-
-func update(temps map[string]int) map[string]error {
- err := smc.GetTemp(ts)
+// 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()
if err != nil {
- return map[string]error{"temps": err}
+ // FIXME log an error here
+ return []string{}
}
- for k, v := range ts {
- if _, ok := temps[k]; ok {
- temps[k] = int(v + 0.5)
+ 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)
}
}
- return nil
+ return rv
}
-func devs() []string {
- rv := make([]string, len(smc.AppleTemp))
- for i, v := range smc.AppleTemp {
- rv[i] = v.Desc
+// Only the ones filtered
+func defs() []string {
+ rv := make([]string, 0, len(sensorMap))
+ for _, val := range sensorMap {
+ rv = append(rv, val)
}
return rv
}
-func defs() []string {
- // CPU 0 CPU 1 GPU Memory Disk
- ids := map[string]bool{"TC0P": true, "TC1P": true, "TG0P": true, "Ts0S": true, "TH0P": true}
- rv := make([]string, 0, len(ids))
- for _, v := range smc.AppleTemp {
- if ids[v.Key] {
- rv = append(rv, v.Desc)
+// 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]
}
}
return rv