Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Dec 16, 2024
1 parent 7fbb24a commit 17ca8e9
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,17 +438,7 @@ func (f *DefaultFanController) calculateTargetPwm() (int, error) {
// TODO: remove
target = minPwm + int((float64(target)/fans.MaxPwmValue)*(float64(maxPwm)-float64(minPwm)))

if f.lastSetPwm != nil && f.pwmMap != nil {
lastSetPwm := *(f.lastSetPwm)
expected := f.applyPwmMapping(f.findClosestDistinctTarget(lastSetPwm))
if currentPwm, err := fan.GetPwm(); err == nil {
if currentPwm != expected {
f.stats.UnexpectedPwmValueCount += 1
ui.Warning("PWM of %s was changed by third party! Last set PWM value was: %d but is now: %d",
fan.GetId(), expected, currentPwm)
}
}
}
f.ensureNoThirdPartyIsMessingWithUs()

if fan.Supports(fans.FeatureRpmSensor) {
// make sure fans never stop by validating the current RPM
Expand Down Expand Up @@ -478,6 +468,23 @@ func (f *DefaultFanController) calculateTargetPwm() (int, error) {
return target, nil
}

// ensureNoThirdPartyIsMessingWithUs checks if the PWM value of the fan does not match the last
// value PWM set by fan2go. If that is the case, it is assumed that a third party has changed the PWM value
// of the fan, which can lead to unexpected behavior.
func (f *DefaultFanController) ensureNoThirdPartyIsMessingWithUs() {
if f.lastSetPwm != nil && f.pwmMap != nil {
lastSetPwm := *(f.lastSetPwm)
expected := f.applyPwmMapping(f.findClosestDistinctTarget(lastSetPwm))
if currentPwm, err := f.fan.GetPwm(); err == nil {
if currentPwm != expected {
f.stats.UnexpectedPwmValueCount += 1
ui.Warning("PWM of %s was changed by third party! Last set PWM value was: %d but is now: %d",
f.fan.GetId(), expected, currentPwm)
}
}
}
}

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

0 comments on commit 17ca8e9

Please sign in to comment.