-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpowersensor.go
72 lines (58 loc) · 1.45 KB
/
powersensor.go
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
66
67
68
69
70
71
72
package lmsensors
import (
"strconv"
"time"
)
var _ Sensor = &PowerSensor{}
// A PowerSensor is a Sensor that detects average electrical power consumption
// in watts.
type PowerSensor struct {
// The name of the sensor.
Name string
// The average electrical power consumption, in watts, indicated
// by the sensor.
Average float64
// The interval of time over which the average electrical power consumption
// is collected.
AverageInterval time.Duration
// Whether or not this sensor has a battery.
Battery bool
// The model number of the sensor.
ModelNumber string
// Miscellaneous OEM information about the sensor.
OEMInfo string
// The serial number of the sensor.
SerialNumber string
}
func (s *PowerSensor) name() string { return s.Name }
func (s *PowerSensor) setName(name string) { s.Name = name }
func (s *PowerSensor) parse(raw map[string]string) error {
for k, v := range raw {
switch k {
case "average":
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
// Raw temperature values are scaled by one million
f /= 1000000
s.Average = f
case "average_interval":
// Time values in milliseconds
d, err := time.ParseDuration(v + "ms")
if err != nil {
return err
}
s.AverageInterval = d
case "is_battery":
s.Battery = v != "0"
case "model_number":
s.ModelNumber = v
case "oem_info":
s.OEMInfo = v
case "serial_number":
s.SerialNumber = v
}
}
return nil
}