forked from md14454/gosensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosensors.go
175 lines (134 loc) · 2.79 KB
/
gosensors.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gosensors
// #cgo LDFLAGS: -lsensors
// #include <stdlib.h>
// #include <stdio.h>
// #include <sensors/sensors.h>
import "C"
import (
"unsafe"
)
type SubFeature struct {
Name string
Number int32
Type int32
Mapping int32
Flags uint32
chip *C.struct_sensors_chip_name
}
func (s SubFeature) GetValue() float64 {
var value C.double
C.sensors_get_value(s.chip, C.int(s.Number), &value)
return float64(value)
}
type Feature struct {
Name string
Number int32
Type int32
chip *C.struct_sensors_chip_name
feature *C.struct_sensors_feature
}
func (f Feature) GetSubFeatures() []SubFeature {
var subfeatures []SubFeature
var count C.int = 0
for {
resp := C.sensors_get_all_subfeatures(f.chip, f.feature, &count)
if resp == nil {
break
}
subfeature := SubFeature{
Name: C.GoString(resp.name),
Number: int32(resp.number),
Type: int32(resp._type),
Mapping: int32(resp.mapping),
Flags: uint32(resp.flags),
chip: f.chip,
}
subfeatures = append(subfeatures, subfeature)
}
return subfeatures
}
func (f Feature) GetLabel() string {
clabel := C.sensors_get_label(f.chip, f.feature)
golabel := C.GoString(clabel)
C.free(unsafe.Pointer(clabel))
return golabel
}
func (f Feature) GetValue() float64 {
return f.GetSubFeatures()[0].GetValue()
}
type Bus struct {
Type int16
Nr int16
bus *C.struct_sensors_bus_id
}
func (b Bus) String() string {
if b.Type == -1 {
return "*"
} else {
return C.GoString(C.sensors_get_adapter_name(b.bus))
}
}
type Chip struct {
Prefix string
Bus Bus
Addr int32
Path string
chip *C.struct_sensors_chip_name
}
func (c Chip) String() string {
var buffer [200]C.char
len := C.sensors_snprintf_chip_name(&buffer[0], C.size_t(len(buffer)), c.chip)
return C.GoStringN(&buffer[0], len)
}
func (c Chip) AdapterName() string {
return c.Bus.String()
}
func (c Chip) GetFeatures() []Feature {
var features []Feature
var count C.int = 0
for {
resp := C.sensors_get_features(c.chip, &count)
if resp == nil {
break
}
feature := Feature{
Name: C.GoString(resp.name),
Number: int32(resp.number),
Type: int32(resp._type),
chip: c.chip,
feature: resp,
}
features = append(features, feature)
}
return features
}
func Init() {
C.sensors_init(nil)
}
func Cleanup() {
C.sensors_cleanup()
}
func GetDetectedChips() []Chip {
var chips []Chip
var count C.int = 0
for {
resp := C.sensors_get_detected_chips(nil, &count)
if resp == nil {
break
}
bus := Bus{
Type: int16(resp.bus._type),
Nr: int16(resp.bus.nr),
bus: &resp.bus,
}
chip := Chip{
Prefix: C.GoString(resp.prefix),
Bus: bus,
Addr: int32(resp.addr),
Path: C.GoString(resp.path),
chip: resp,
}
chips = append(chips, chip)
}
return chips
}