-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from markusressel/feature/cli_set_fan_speed
Feature/cli set fan speed
- Loading branch information
Showing
7 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters