Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/#47 better handling of missing values #105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin
fan2go.db
dist
fan2go*.db
fan2go*.yaml
test.db
28 changes: 18 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
BINARY_NAME=fan2go
OUTPUT_DIR=bin/
GO_FLAGS ?=
NAME := fan2go
OUTPUT_BIN ?= bin/${NAME}
PACKAGE := github.com/markusressel/$(NAME)
GIT_REV ?= $(shell git rev-parse --short HEAD)
SOURCE_DATE_EPOCH ?= $(shell date +%s)
DATE ?= $(shell date -u -d @${SOURCE_DATE_EPOCH} +"%Y-%m-%dT%H:%M:%SZ")
VERSION ?= 0.6.0

build:
go build -o ${OUTPUT_DIR}${BINARY_NAME} main.go
test: ## Run all tests
@go clean --testcache && go test -v ./...

run:
go build -o ${OUTPUT_DIR}${BINARY_NAME} main.go
./${OUTPUT_DIR}${BINARY_NAME}
build: ## Builds the CLI
@go build ${GO_FLAGS} \
-ldflags "-w -s -X ${PACKAGE}/cmd.version=${VERSION} -X ${PACKAGE}/cmd.commit=${GIT_REV} -X ${PACKAGE}/cmd.date=${DATE}" \
-a -tags netgo -o ${OUTPUT_BIN} main.go

test:
sudo go test -v ./...
run:
go build -o ${OUTPUT_BIN} main.go
./${OUTPUT_BIN}

clean:
go clean
rm ${OUTPUT_DIR}${BINARY_NAME}
rm ${OUTPUT_BIN}
22 changes: 17 additions & 5 deletions cmd/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ var detectCmd = &cobra.Command{

var fanRows [][]string
for _, fan := range fanList {
pwm := fan.GetPwm()
rpm := fan.GetRpm()
pwmText := "N/A"
if pwm, err := fan.GetPwm(); err == nil {
pwmText = strconv.Itoa(pwm)
}

rpmText := "N/A"
if rpm, err := fan.GetPwm(); err == nil {
rpmText = strconv.Itoa(rpm)
}

isAuto, _ := fan.IsPwmAuto()
fanRows = append(fanRows, []string{
"", strconv.Itoa(fan.Index), fan.Label, strconv.Itoa(rpm), strconv.Itoa(pwm), fmt.Sprintf("%v", isAuto),
"", strconv.Itoa(fan.Index), fan.Label, rpmText, pwmText, fmt.Sprintf("%v", isAuto),
})
}
var fanHeaders = []string{"Fans ", "Index", "Label", "RPM", "PWM", "Auto"}
Expand All @@ -66,13 +74,17 @@ var detectCmd = &cobra.Command{

var sensorRows [][]string
for _, sensor := range sensorList {
value, _ := sensor.GetValue()
value, err := sensor.GetValue()
valueText := "N/A"
if err == nil {
valueText = strconv.Itoa(int(value))
}

_, file := filepath.Split(sensor.Input)
labelAndFile := fmt.Sprintf("%s (%s)", sensor.Label, file)

sensorRows = append(sensorRows, []string{
"", strconv.Itoa(sensor.Index), labelAndFile, strconv.Itoa(int(value)),
"", strconv.Itoa(sensor.Index), labelAndFile, valueText,
})
}
var sensorHeaders = []string{"Sensors", "Index", "Label", "Value"}
Expand Down
10 changes: 4 additions & 6 deletions cmd/fan/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ var rpmCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
pterm.DisableOutput()

fanIdFlag := cmd.Flag("id")
fanId := fanIdFlag.Value.String()

fan, err := getFan(fanId)
if err != nil {
return err
}

fmt.Printf("%d", fan.GetRpm())
return nil
if rpm, err := fan.GetRpm(); err == nil {
fmt.Printf("%d", rpm)
}
return err
},
}

Expand Down
11 changes: 3 additions & 8 deletions cmd/fan/speed.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var speedCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
pterm.DisableOutput()

fanIdFlag := cmd.Flag("id")
fanId := fanIdFlag.Value.String()

fan, err := getFan(fanId)
if err != nil {
return err
Expand All @@ -29,12 +26,10 @@ var speedCmd = &cobra.Command{
return err
}
err = fan.SetPwm(pwmValue)
if err != nil {
return err
}
} else {
fmt.Printf("%d", fan.GetPwm())
return nil
if pwm, err := fan.GetPwm(); err == nil {
fmt.Printf("%d", pwm)
}
}

return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

var (
version, commit, date = "dev", "dev", ""

cfgFile string
noColor bool
noStyle bool
Expand Down
3 changes: 0 additions & 3 deletions cmd/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ var Command = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
pterm.DisableOutput()

sensorIdFlag := cmd.Flag("id")
sensorId := sensorIdFlag.Value.String()

sensor, err := getSensor(sensorId)
if err != nil {
return err
Expand Down
12 changes: 11 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import (
"github.com/spf13/cobra"
)

var long bool

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of fan2go",
Long: `All software has versions. This is fan2go's`,
Run: func(cmd *cobra.Command, args []string) {
ui.Printfln("0.6.0")
if verbose {
ui.Printfln("%s-%s-%s", version, commit, date)
} else if long {
ui.Printfln("%s-%s", version, commit)
} else {
ui.Printfln("%s", version)
}
},
}

func init() {
versionCmd.Flags().BoolVarP(&long, "long", "l", false, "Show the long version")

rootCmd.AddCommand(versionCmd)
}
56 changes: 41 additions & 15 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ func (f *fanController) runInitializationSequence() (err error) {
// verify that we were successful in writing our desired PWM value
// otherwise, skip this PWM value
// TODO: this has to be done _always_, while the RPM measurements only make sense if the fan supports it
actualPwm := fan.GetPwm()
actualPwm, err := fan.GetPwm()
if err != nil {
ui.Error("Unable to measure current PWM")
return err
}
if actualPwm != pwm {
ui.Debug("Actual PWM value differs from requested one, skipping. Requested: %d Actual: %d", pwm, actualPwm)
continue
Expand All @@ -215,7 +219,11 @@ func (f *fanController) runInitializationSequence() (err error) {
time.Sleep(2 * time.Second)
}

rpm := fan.GetRpm()
rpm, err := fan.GetRpm()
if err != nil {
ui.Error("Unable to measure RPM of fan %s", fan.GetId())
return err
}
ui.Debug("Measuring RPM of %s at PWM %d: %d", fan.GetId(), pwm, rpm)

// update rpm curve
Expand All @@ -241,8 +249,14 @@ func (f *fanController) runInitializationSequence() (err error) {

// read the current value of a fan RPM sensor and append it to the moving window
func measureRpm(fan fans.Fan) {
pwm := fan.GetPwm()
rpm := fan.GetRpm()
pwm, err := fan.GetPwm()
if err != nil {
ui.Warning("Error reading PWM value of fan %s: %v", fan.GetId(), err)
}
rpm, err := fan.GetRpm()
if err != nil {
ui.Warning("Error reading RPM value of fan %s: %v", fan.GetId(), err)
}

updatedRpmAvg := util.UpdateSimpleMovingAvg(fan.GetRpmAvg(), configuration.CurrentConfig.RpmRollingWindowSize, float64(rpm))
fan.SetRpmAvg(updatedRpmAvg)
Expand Down Expand Up @@ -280,7 +294,6 @@ func (f *fanController) restorePwmEnabled() {
// returns -1 if no rpm is detected even at fan.maxPwm
func (f *fanController) calculateTargetPwm() int {
fan := f.fan
currentPwm := fan.GetPwm()
target, err := f.curve.Evaluate()
if err != nil {
ui.Fatal("Unable to calculate optimal PWM value for %s: %v", fan.GetId(), err)
Expand Down Expand Up @@ -308,9 +321,11 @@ func (f *fanController) calculateTargetPwm() int {
if f.lastSetPwm != nil && f.pwmMap != nil {
lastSetPwm := *(f.lastSetPwm)
expected := f.pwmMap[lastSetPwm]
if currentPwm != expected {
ui.Warning("PWM of %s was changed by third party! Last set PWM value was: %d but is now: %d",
fan.GetId(), expected, currentPwm)
if currentPwm, err := fan.GetPwm(); err == nil {
if currentPwm != expected {
ui.Warning("PWM of %s was changed by third party! Last set PWM value was: %d but is now: %d",
fan.GetId(), expected, currentPwm)
}
}
}

Expand Down Expand Up @@ -342,12 +357,14 @@ func (f *fanController) calculateTargetPwm() int {

// set the pwm speed of a fan to the specified value (0..255)
func (f *fanController) setPwm(target int) (err error) {
current := f.fan.GetPwm()
current, err := f.fan.GetPwm()

f.lastSetPwm = &target
if f.pwmMap[target] == current {
// nothing to do
return nil
if err == nil {
if f.pwmMap[target] == current {
// nothing to do
return nil
}
}
err = f.fan.SetPwm(target)
return err
Expand All @@ -363,11 +380,16 @@ func (f *fanController) waitForFanToSettle(fan fans.Fan) {
oldRpm := 0
for !(measuredRpmDiffMax < diffThreshold) {
ui.Debug("Waiting for fan %s to settle (current RPM max diff: %f)...", fan.GetId(), measuredRpmDiffMax)
currentRpm := fan.GetRpm()
time.Sleep(1 * time.Second)

currentRpm, err := fan.GetRpm()
if err != nil {
ui.Warning("Cannot read RPM value of fan %s: %v", fan.GetId(), err)
continue
}
measuredRpmDiffWindow.Append(math.Abs(float64(currentRpm - oldRpm)))
oldRpm = currentRpm
measuredRpmDiffMax = math.Ceil(util.GetWindowMax(measuredRpmDiffWindow))
time.Sleep(1 * time.Second)
}
ui.Debug("Fan %s has settled (current RPM max diff: %f)", fan.GetId(), measuredRpmDiffMax)
}
Expand All @@ -385,7 +407,11 @@ func (f *fanController) updatePwmMap() {
for i := fans.MaxPwmValue; i >= fans.MinPwmValue; i-- {
fan.SetPwm(i)
time.Sleep(10 * time.Millisecond)
f.pwmMap[i] = fan.GetPwm()
pwm, err := fan.GetPwm()
if err != nil {
ui.Warning("Error reading PWM value of fan %s: %v", fan.GetId(), err)
}
f.pwmMap[i] = pwm
}

fan.SetPwm(f.pwmMap[fan.GetStartPwm()])
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (fan *MockFan) SetMaxPwm(pwm int) {
panic("not supported")
}

func (fan MockFan) GetRpm() int {
return fan.RPM
func (fan MockFan) GetRpm() (int, error) {
return fan.RPM, nil
}

func (fan MockFan) GetRpmAvg() float64 {
Expand All @@ -101,8 +101,8 @@ func (fan *MockFan) SetRpmAvg(rpm float64) {
panic("not supported")
}

func (fan MockFan) GetPwm() (result int) {
return fan.PWM
func (fan MockFan) GetPwm() (result int, err error) {
return fan.PWM, nil
}

func (fan *MockFan) SetPwm(pwm int) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/fans/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ type Fan interface {
SetMaxPwm(pwm int)

// GetRpm returns the current RPM value of this fan
GetRpm() int
GetRpm() (int, error)
GetRpmAvg() float64
SetRpmAvg(rpm float64)

// GetPwm returns the current PWM value of this fan
GetPwm() int
GetPwm() (int, error)
SetPwm(pwm int) (err error)

// GetFanCurveData returns the fan curve data for this fan
Expand Down
12 changes: 6 additions & 6 deletions internal/fans/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (fan *FileFan) SetMaxPwm(pwm int) {
return
}

func (fan FileFan) GetRpm() int {
return 0
func (fan FileFan) GetRpm() (int, error) {
return 0, nil
}

func (fan FileFan) GetRpmAvg() float64 {
Expand All @@ -60,24 +60,24 @@ func (fan *FileFan) SetRpmAvg(rpm float64) {
return
}

func (fan FileFan) GetPwm() (result int) {
func (fan FileFan) GetPwm() (result int, err error) {
filePath := fan.FilePath
// resolve home dir path
if strings.HasPrefix(filePath, "~") {
currentUser, err := user.Current()
if err != nil {
return result
return result, err
}

filePath = filepath.Join(currentUser.HomeDir, filePath[1:])
}

integer, err := util.ReadIntFromFile(filePath)
if err != nil {
return MinPwmValue
return MinPwmValue, err
}
result = integer
return result
return result, err
}

func (fan *FileFan) SetPwm(pwm int) (err error) {
Expand Down
Loading