diff --git a/cmd/fan/init.go b/cmd/fan/init.go index f1ce5fb..5c92245 100644 --- a/cmd/fan/init.go +++ b/cmd/fan/init.go @@ -1,6 +1,7 @@ package fan import ( + "github.com/markusressel/fan2go/internal" "github.com/markusressel/fan2go/internal/configuration" "github.com/markusressel/fan2go/internal/control_loop" "github.com/markusressel/fan2go/internal/controller" @@ -27,7 +28,10 @@ var initCmd = &cobra.Command{ ui.Info("Using persistence at: %s", dbPath) p := persistence.NewPersistence(dbPath) - + _, err = internal.InitializeObjects() + if err != nil { + return err + } fanController := controller.NewFanController( p, fan, diff --git a/internal/backend.go b/internal/backend.go index 76568d9..aab3aa9 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -37,11 +37,16 @@ func RunDaemon() { pers := persistence.NewPersistence(configuration.CurrentConfig.DbPath) - fanControllers, err := initializeObjects(pers) + fanMap, err := InitializeObjects() if err != nil { ui.Fatal("Error initializing objects: %v", err) } + fanControllers, err := initializeFanControllers(pers, fanMap) + if err != nil { + ui.Fatal("Error initializing fan controllers: %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -221,7 +226,7 @@ func startStatisticsServer() *echo.Echo { return echoPrometheus } -func initializeObjects(pers persistence.Persistence) (map[fans.Fan]controller.FanController, error) { +func InitializeObjects() (map[configuration.FanConfig]fans.Fan, error) { controllers := hwmon.GetChips() err := initializeSensors(controllers) @@ -233,12 +238,16 @@ func initializeObjects(pers persistence.Persistence) (map[fans.Fan]controller.Fa return nil, fmt.Errorf("error initializing curves: %v", err) } - var result = map[fans.Fan]controller.FanController{} fanMap, err := initializeFans(controllers) if err != nil { return nil, fmt.Errorf("error initializing fans: %v", err) } + return fanMap, err +} + +func initializeFanControllers(pers persistence.Persistence, fanMap map[configuration.FanConfig]fans.Fan) (result map[fans.Fan]controller.FanController, err error) { + result = map[fans.Fan]controller.FanController{} for config, fan := range fanMap { updateRate := configuration.CurrentConfig.ControllerAdjustmentTickRate