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

Feature/cli set fan speed #80

Merged
merged 22 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,28 @@ sudo systemctl enable --now fan2go
journalctl -u fan2go -f
```

## Print fan curve data
## CLI Commands

### Fans interaction

```shell
> fan2go fan --id cpu speed 100
> fan2go fan --id cpu speed
255
> fan2go fan --id cpu rpm
546
```

### Sensors

```shell
> fan2go sensor --id cpu_package
46000
```

### Print fan curve data

For each newly configured fan **fan2go** measures its fan curve and stores it in a db for future reference. You can take
a look at this measurement using the following command:
Expand Down
67 changes: 67 additions & 0 deletions cmd/fan/fan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fan

import (
"errors"
"fmt"
"github.com/markusressel/fan2go/internal/configuration"
"github.com/markusressel/fan2go/internal/fans"
"github.com/markusressel/fan2go/internal/hwmon"
"github.com/spf13/cobra"
"regexp"
)

var fanId string

var Command = &cobra.Command{
Use: "fan",
Short: "Fan related commands",
Long: ``,
TraverseChildren: true,
}

func init() {
Command.PersistentFlags().StringVarP(
&fanId,
"id", "i",
"",
"Fan ID as specified in the config",
)
_ = Command.MarkPersistentFlagRequired("id")
}

func getFan(id string) (fans.Fan, error) {
configuration.ReadConfigFile()

controllers := hwmon.GetChips()

for _, config := range configuration.CurrentConfig.Fans {
if config.ID == id {
if config.HwMon != nil {
for _, controller := range controllers {
matched, err := regexp.MatchString("(?i)"+config.HwMon.Platform, controller.Platform)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform))
}
if matched {
index := config.HwMon.Index - 1
if len(controller.Fans) > index {
fan := controller.Fans[index]
config.HwMon.PwmOutput = fan.PwmOutput
config.HwMon.RpmInput = fan.RpmInput
break
}
}
}
}

fan, err := fans.NewFan(config)
if err != nil {
return nil, err
}

return fan, nil
}
}

return nil, errors.New(fmt.Sprintf("No fan with id found: %s", fanId))
}
32 changes: 32 additions & 0 deletions cmd/fan/rpm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fan

import (
"fmt"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

var rpmCmd = &cobra.Command{
Use: "rpm",
Short: "Get the current RPM reading of a fan",
Long: ``,
Args: cobra.NoArgs,
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
},
}

func init() {
Command.AddCommand(rpmCmd)
}
46 changes: 46 additions & 0 deletions cmd/fan/speed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fan

import (
"fmt"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"strconv"
)

var speedCmd = &cobra.Command{
Use: "speed",
Short: "Get/Set the current speed setting of a fan to the given PWM value ([0..255])",
Long: ``,
Args: cobra.RangeArgs(0, 1),
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
}

if len(args) > 0 {
pwmValue, err := strconv.Atoi(args[0])
if err != nil {
return err
}
err = fan.SetPwm(pwmValue)
if err != nil {
return err
}
} else {
fmt.Printf("%d", fan.GetPwm())
return nil
}

return err
},
}

func init() {
Command.AddCommand(speedCmd)
}
19 changes: 13 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"github.com/markusressel/fan2go/cmd/fan"
"github.com/markusressel/fan2go/cmd/sensor"
"github.com/markusressel/fan2go/internal"
"github.com/markusressel/fan2go/internal/configuration"
"github.com/markusressel/fan2go/internal/ui"
Expand Down Expand Up @@ -33,6 +35,16 @@ on your computer based on temperature sensors.`,
},
}

func init() {
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.fan2go.yaml)")
rootCmd.PersistentFlags().BoolVarP(&noColor, "no-color", "", false, "Disable all terminal output coloration")
rootCmd.PersistentFlags().BoolVarP(&noStyle, "no-style", "", false, "Disable all terminal output styling")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "More verbose output")

rootCmd.AddCommand(fan.Command)
rootCmd.AddCommand(sensor.Command)
}

func setupUi() {
ui.SetDebugEnabled(verbose)

Expand Down Expand Up @@ -63,13 +75,8 @@ func Execute() {
configuration.InitConfig(cfgFile)
})

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.fan2go.yaml)")
rootCmd.PersistentFlags().BoolVarP(&noColor, "no-color", "", false, "Disable all terminal output coloration")
rootCmd.PersistentFlags().BoolVarP(&noStyle, "no-style", "", false, "Disable all terminal output styling")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "More verbose output")

if err := rootCmd.Execute(); err != nil {
ui.Fatal("Error Executing daemon: %v", err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
89 changes: 89 additions & 0 deletions cmd/sensor/sensor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package sensor

import (
"errors"
"fmt"
"github.com/markusressel/fan2go/internal/configuration"
"github.com/markusressel/fan2go/internal/hwmon"
"github.com/markusressel/fan2go/internal/sensors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"regexp"
)

var sensorId string

var Command = &cobra.Command{
Use: "sensor",
Short: "sensor related commands",
Long: ``,
TraverseChildren: true,
Args: cobra.NoArgs,
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
}

value, err := sensor.GetValue()
if err != nil {
return err
}
fmt.Printf("%d", int(value))
return nil
},
}

func init() {
Command.PersistentFlags().StringVarP(
&sensorId,
"id", "i",
"",
"Sensor ID as specified in the config",
)
_ = Command.MarkPersistentFlagRequired("id")
}

func getSensor(id string) (sensors.Sensor, error) {
configuration.ReadConfigFile()

controllers := hwmon.GetChips()

for _, config := range configuration.CurrentConfig.Sensors {
if config.ID == id {
if config.HwMon != nil {
for _, controller := range controllers {
matched, err := regexp.MatchString("(?i)"+config.HwMon.Platform, controller.Platform)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform))
}
if matched {
index := config.HwMon.Index - 1
if len(controller.Sensors) > index {
sensor := controller.Sensors[index]
if len(sensor.Input) <= 0 {
return nil, errors.New(fmt.Sprintf("Unable to find temp input for sensor %s", id))
}
config.HwMon.TempInput = sensor.Input
break
}
}
}
}

sensor, err := sensors.NewSensor(config)
if err != nil {
return nil, err
}

return sensor, nil
}
}

return nil, errors.New(fmt.Sprintf("No sensor with id found: %s", sensorId))
}
1 change: 1 addition & 0 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func (f *fanController) mapToClosestDistinct(target int) int {

func (f *fanController) updatePwmMap() {
fan := f.fan
trySetManualPwm(fan)

// check every pwm value
for i := fans.MaxPwmValue; i >= fans.MinPwmValue; i-- {
Expand Down