summaryrefslogtreecommitdiffstats
path: root/widgets/temp_freebsd.go
blob: 8d9eda65ccf3225f22818f07060072f8641d065d (plain)
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
// +build freebsd

package widgets

import (
	"fmt"
	"log"
	"os/exec"
	"strconv"
	"strings"

	"github.com/xxxserxxx/gotop/utils"
)

var sensorOIDS = map[string]string{
	"dev.cpu.0.temperature":           "CPU 0 ",
	"hw.acpi.thermal.tz0.temperature": "Thermal zone 0",
}

type sensorMeasurement struct {
	name        string
	temperature float64
}

func removeUnusedChars(s string) string {
	s1 := strings.Replace(s, "C", "", 1)
	s2 := strings.TrimSuffix(s1, "\n")
	return s2
}

func refineOutput(output []byte) (float64, error) {
	convertedOutput := utils.ConvertLocalizedString(removeUnusedChars(string(output)))
	value, err := strconv.ParseFloat(convertedOutput, 64)
	if err != nil {
		return 0, err
	}
	return value, nil
}

func collectSensors() ([]sensorMeasurement, error) {
	var measurements []sensorMeasurement
	for k, v := range sensorOIDS {
		output, err := exec.Command("sysctl", "-n", k).Output()
		if err != nil {
			return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err)
		}

		value, err := refineOutput(output)
		if err != nil {
			return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err)
		}

		measurements = append(measurements, sensorMeasurement{v, value})

	}
	return measurements, nil

}

func (self *TempWidget) update() {
	sensors, err := collectSensors()
	if err != nil {
		log.Printf("error received from gopsutil: %v", err)
	}
	for _, sensor := range sensors {
		switch self.TempScale {
		case Fahrenheit:
			self.Data[sensor.name] = utils.CelsiusToFahrenheit(int(sensor.temperature))
		case Celsius:
			self.Data[sensor.name] = int(sensor.temperature)
		}
	}
}