Browse Source

Tie fan into config (both ui and fan daemon)

master
KemoNine 2 years ago
parent
commit
3ca4b66304
  1. 48
      cmd/fan/fan.go
  2. 6
      cmd/gui/README.md
  3. 44
      cmd/gui/gui.go
  4. 42
      config/config.go
  5. 20
      config/constants.go

48
cmd/fan/fan.go

@ -10,6 +10,7 @@ import (
argonFan "git.sungo.io/sungo/argon/fan"
"git.kemonine.info/PiFrame/utils"
"git.kemonine.info/PiFrame/config"
)
const (
@ -17,15 +18,19 @@ const (
ADDRESS = 0x1a
)
const (
POLL_INTERVAL = "30s"
SPEED_TWENTY_FIVE_PERCENT_TEMP = 45.00
SPEED_FIFTY_PERCENT_TEMP = 50.00
SPEED_SEVENTY_FIVE_PERCENT_TEMP = 52.00
SPEED_FULL_TEMP = 55.00
)
func main() {
// Load the config file
pfConfig, _ := config.LoadConfig()
// Get the various fan related config options as local variables
speedMap := pfConfig.Float64Map(config.CONFIG_KEY_FAN_SPEEDS)
POLL_INTERVAL := pfConfig.String(config.CONFIG_KEY_FAN_POLL_INTERVAL)
SPEED_FULL_TEMP := speedMap[pfConfig.String(config.CONFIG_MAP_KEY_FAN_SPEED_100)]
SPEED_SEVENTY_FIVE_PERCENT_TEMP := speedMap[pfConfig.String(config.CONFIG_MAP_KEY_FAN_SPEED_75)]
SPEED_FIFTY_PERCENT_TEMP := speedMap[pfConfig.String(config.CONFIG_MAP_KEY_FAN_SPEED_50)]
SPEED_TWENTY_FIVE_PERCENT_TEMP := speedMap[pfConfig.String(config.CONFIG_MAP_KEY_FAN_SPEED_25)]
SPEED_MINIMUM := pfConfig.Int(config.CONFIG_KEY_FAN_MIN_SPEED)
// Setup fan and bail if we can't see it
fan, err := argonFan.New(ADDRESS, BUS)
if err != nil {
@ -59,26 +64,43 @@ func main() {
gpuTemp := utils.GetGPUTemp()
if cpuTemp >= SPEED_FULL_TEMP || gpuTemp >= SPEED_FULL_TEMP {
fan.SetSpeed(100)
if SPEED_MINIMUM > 100 {
fan.SetSpeed(SPEED_MINIMUM)
} else {
fan.SetSpeed(100)
}
continue
}
if cpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP {
fan.SetSpeed(75)
if SPEED_MINIMUM > 75 {
fan.SetSpeed(SPEED_MINIMUM)
} else {
fan.SetSpeed(75)
}
continue
}
if cpuTemp >= SPEED_FIFTY_PERCENT_TEMP || gpuTemp >= SPEED_FIFTY_PERCENT_TEMP {
fan.SetSpeed(50)
if SPEED_MINIMUM > 50 {
fan.SetSpeed(SPEED_MINIMUM)
} else {
fan.SetSpeed(50)
}
continue
}
if cpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP {
fan.SetSpeed(25)
if SPEED_MINIMUM > 25 {
fan.SetSpeed(SPEED_MINIMUM)
} else {
fan.SetSpeed(25)
}
continue
}
if cpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP {
fan.SetSpeed(10)
fan.SetSpeed(SPEED_MINIMUM)
continue
}
// We should never get here but...
// Maxing fan to be on the safe side
fan.SetSpeed(100)
}
}

6
cmd/gui/README.md

@ -4,7 +4,9 @@ This is the main source code for the custom UI used by PiFrame. This is responsi
## Config
The GUI will work off a config similar to the following. Use the ```generate config``` option to generate a default configuration in ```/etc/default/pf.toml```
The GUI uses a config similar to the following. To generate a default config, launch the gui and immediatly select the ```Save & Exit``` button. The generated configuration will be at ```/etc/default/pf.toml```.
**Please Note**: The below is a sample and may or may not match the coded defaults. We also do *not* recommend changing values that are not present in the UI unless you are sure of the consequences.
```
@ -18,7 +20,7 @@ slideshow.restartinterval = "7d"
hdmi.off = "*-*-* 00:00:00"
hdmi.on = "*-*-* 06:00:00"
# Album configuration
# Album configuration (DO NOT CHANGE albums.root )
albums.root = "/tank/pictures"
albums.selected = ["/", "/KemoNine"]

44
cmd/gui/gui.go

@ -6,14 +6,11 @@ import (
"os"
"time"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
flag "github.com/spf13/pflag"
pfconfig "git.kemonine.info/PiFrame/config"
"git.kemonine.info/PiFrame/config"
"git.kemonine.info/PiFrame/ui"
)
@ -25,37 +22,16 @@ func main() {
os.Exit(0)
}
// Command line flags
f.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
cliFlag := f.Lookup(pfconfig.CLI_FLAG_CONFIG_ONLY)
f.Bool(config.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
cliFlag := f.Lookup(config.CLI_FLAG_CONFIG_ONLY)
if cliFlag != nil {
cliFlag.NoOptDefVal = "true"
}
// Process command line flags into handler
f.Parse(os.Args[1:])
// Main config variable
var pfConfig = koanf.New(".")
// Setup some defaults
pfConfig.Load(confmap.Provider(map[string]interface{}{
pfconfig.CONFIG_KEY_SLIDESHOW_INTERVAL: pfconfig.DEFAULT_SLIDESHOW_INTERVAL,
pfconfig.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL: pfconfig.DEFAULT_SLIDESHOW_RESTART_INTERVAL,
pfconfig.CONFIG_KEY_HDMI_OFF: pfconfig.DEFAULT_HDMI_OFF,
pfconfig.CONFIG_KEY_HDMI_ON: pfconfig.DEFAULT_HDMI_ON,
pfconfig.CONFIG_KEY_ALBUMS_ROOT: pfconfig.DEFAULT_ALBUMS_ROOT,
pfconfig.CONFIG_KEY_ALBUMS_SELECTED: []string{pfconfig.DEFAULT_ALBUM_SELECTED},
}, "."), nil)
// Bring in /etc/defaults/pf.toml if it exists
configFileProvider := file.Provider(pfconfig.CONFIG_FILE_PATH)
_, err := os.Stat(pfconfig.CONFIG_FILE_PATH)
if os.IsNotExist(err) {
//log.Printf("%s does not exist, USING DEFAULTS", ui.CONFIG_FILE_PATH)
} else {
if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
log.Fatalf("Error loading config : %s", err)
}
}
// Load the config file
pfConfig, configFileProvider := config.LoadConfig()
// Watch for config changes and re-load config if needed
configFileProvider.Watch(func(event interface{}, err error) {
@ -76,14 +52,16 @@ func main() {
log.Fatalf("Error loading command line flags : %s", err)
}
if !pfConfig.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY) {
ui.Slideshow(pfConfig)
}
hideSlideshow := pfConfig.Bool(config.CLI_FLAG_CONFIG_ONLY)
// Reset the CLI flag so it's never writted to the config as 'true'
pfConfig.Load(confmap.Provider(map[string]interface{}{
pfconfig.CLI_FLAG_CONFIG_ONLY: false,
config.CLI_FLAG_CONFIG_ONLY: false,
}, "."), nil)
if !hideSlideshow {
ui.Slideshow(pfConfig)
}
ui.ConfigGui(pfConfig)
}

42
config/config.go

@ -0,0 +1,42 @@
package config
import (
"log"
"os"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
kfile "github.com/knadh/koanf/providers/file"
)
func LoadConfig() (*koanf.Koanf, *kfile.File) {
// Main config variable
var pfConfig = koanf.New(".")
// Setup defaults
pfConfig.Load(confmap.Provider(map[string]interface{}{
CONFIG_KEY_SLIDESHOW_INTERVAL: DEFAULT_SLIDESHOW_INTERVAL,
CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL: DEFAULT_SLIDESHOW_RESTART_INTERVAL,
CONFIG_KEY_HDMI_OFF: DEFAULT_HDMI_OFF,
CONFIG_KEY_HDMI_ON: DEFAULT_HDMI_ON,
CONFIG_KEY_ALBUMS_ROOT: DEFAULT_ALBUMS_ROOT,
CONFIG_KEY_ALBUMS_SELECTED: DEFAULT_ALBUM_SELECTED,
CONFIG_KEY_FAN_POLL_INTERVAL: DEFAULT_FAN_POLL_INTERVAL,
CONFIG_KEY_FAN_SPEEDS: DEFAULT_FAN_SPEEDS,
CONFIG_KEY_FAN_MIN_SPEED: DEFAULT_FAN_MIN_SPEED,
}, "."), nil)
// Bring in /etc/defaults/pf.toml if it exists
configFileProvider := kfile.Provider(CONFIG_FILE_PATH)
_, err := os.Stat(CONFIG_FILE_PATH)
if os.IsNotExist(err) {
//log.Printf("%s does not exist, USING DEFAULTS", ui.CONFIG_FILE_PATH)
} else {
if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
log.Fatalf("Error loading config : %s", err)
}
}
return pfConfig, configFileProvider
}

20
config/constants.go

@ -13,6 +13,13 @@ const (
CONFIG_KEY_HDMI_ON = "hdmi.on"
CONFIG_KEY_ALBUMS_ROOT = "albums.root"
CONFIG_KEY_ALBUMS_SELECTED = "albums.selected"
CONFIG_KEY_FAN_POLL_INTERVAL = "fan.pollinginterval"
CONFIG_KEY_FAN_SPEEDS = "fan.speeds"
CONFIG_KEY_FAN_MIN_SPEED = "fan.minspeed"
CONFIG_MAP_KEY_FAN_SPEED_25 = "25"
CONFIG_MAP_KEY_FAN_SPEED_50 = "50"
CONFIG_MAP_KEY_FAN_SPEED_75 = "75"
CONFIG_MAP_KEY_FAN_SPEED_100 = "100"
)
const (
@ -21,5 +28,16 @@ const (
DEFAULT_HDMI_OFF = "*-*-* 00:00:00"
DEFAULT_HDMI_ON = "*-*-* 06:00:00"
DEFAULT_ALBUMS_ROOT = "/tank/pictures"
DEFAULT_ALBUM_SELECTED = "/"
DEFAULT_FAN_POLL_INTERVAL = "30s"
DEFAULT_FAN_MIN_SPEED = 10
)
var DEFAULT_ALBUM_SELECTED = []string{"/"}
// Speed : Temp to activate speed
var DEFAULT_FAN_SPEEDS = map[string]float64{
CONFIG_MAP_KEY_FAN_SPEED_25: 45.00,
CONFIG_MAP_KEY_FAN_SPEED_50: 50.00,
CONFIG_MAP_KEY_FAN_SPEED_75: 52.00,
CONFIG_MAP_KEY_FAN_SPEED_100: 55.00,
}

Loading…
Cancel
Save