simplify the driver interface
This commit is contained in:
@@ -13,11 +13,11 @@ type HwmonDriver struct {
|
||||
hwmonDriver string
|
||||
}
|
||||
|
||||
func NewHwmonDriver(expectedDrivers []string) *HwmonDriver {
|
||||
func NewHwmonDriver(expectedDrivers []string) (*HwmonDriver, error) {
|
||||
// Locate hwmon path
|
||||
dirs, err := filepath.Glob("/sys/class/hwmon/hwmon*")
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, dir := range dirs {
|
||||
@@ -29,16 +29,35 @@ func NewHwmonDriver(expectedDrivers []string) *HwmonDriver {
|
||||
return &HwmonDriver{
|
||||
hwmonDir: dir,
|
||||
hwmonDriver: driver,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("could not find %#v in /sys/class/hwmon", expectedDrivers)
|
||||
}
|
||||
|
||||
func (r *HwmonDriver) Render(console *strings.Builder) error {
|
||||
t, err := r.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
temps := t.([]TemperatureSensor)
|
||||
|
||||
fmt.Fprint(console, "--- Temperature ---\n")
|
||||
for _, sensor := range temps {
|
||||
tempC := sensor.TempC
|
||||
tempF := (tempC * 9 / 5) + 32
|
||||
// \033[K clears from cursor to end of line (prevents lingering chars)
|
||||
fmt.Fprintf(console, "%-16s | %.2f °C (%.3f °F)\033[K\n", sensor.Name, tempC, tempF)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *HwmonDriver) ReadTemperatureSensors() ([]TemperatureSensor, error) {
|
||||
func (r *HwmonDriver) Read() (any, error) {
|
||||
inputs, err := filepath.Glob(filepath.Join(r.hwmonDir, "temp*_input"))
|
||||
if err != nil || len(inputs) == 0 {
|
||||
return nil, fmt.Errorf("no temperature inputs found in %s", r.hwmonDir)
|
||||
|
||||
Reference in New Issue
Block a user