2020-09-04 18:57:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
argonFan "git.sungo.io/sungo/argon/fan"
|
|
|
|
|
2020-09-05 19:49:49 +00:00
|
|
|
"git.kemonine.info/PiFrame/config"
|
2020-09-05 20:51:00 +00:00
|
|
|
"git.kemonine.info/PiFrame/utils"
|
2020-09-04 18:57:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BUS = 1
|
|
|
|
ADDRESS = 0x1a
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-09-05 19:49:49 +00:00
|
|
|
// Load the config file
|
2020-09-20 22:10:14 +00:00
|
|
|
pfConfig, configFileProvider := config.LoadConfig(false)
|
2020-09-06 07:36:35 +00:00
|
|
|
|
|
|
|
// Watch for config changes and re-load config if needed
|
|
|
|
configFileProvider.Watch(func(event interface{}, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error setting up watch of config : %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail on slideshow if there is a config change so it restarts with updated config
|
|
|
|
log.Fatalf("Config file changed! Exiting!")
|
|
|
|
})
|
2020-09-05 19:49:49 +00:00
|
|
|
|
|
|
|
// Get the various fan related config options as local variables
|
|
|
|
POLL_INTERVAL := pfConfig.String(config.CONFIG_KEY_FAN_POLL_INTERVAL)
|
2020-09-06 01:27:33 +00:00
|
|
|
SPEED_FULL_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_100)
|
|
|
|
SPEED_SEVENTY_FIVE_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_75)
|
|
|
|
SPEED_FIFTY_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_50)
|
|
|
|
SPEED_TWENTY_FIVE_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_25)
|
2020-09-05 19:49:49 +00:00
|
|
|
SPEED_MINIMUM := pfConfig.Int(config.CONFIG_KEY_FAN_MIN_SPEED)
|
|
|
|
|
2020-09-04 18:57:12 +00:00
|
|
|
// Setup fan and bail if we can't see it
|
|
|
|
fan, err := argonFan.New(ADDRESS, BUS)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error working with fan : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safe exit
|
|
|
|
defer fan.SafeClose()
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
|
|
sigc,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
<-sigc
|
|
|
|
fan.SafeClose()
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Control fan speed based on temps via a ticker / timeout
|
|
|
|
pollInterval, err := time.ParseDuration(POLL_INTERVAL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error parsing interval duration : %s", err)
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(pollInterval)
|
2020-09-04 19:04:44 +00:00
|
|
|
for range ticker.C {
|
|
|
|
cpuTemp := utils.GetCPUTemp()
|
|
|
|
gpuTemp := utils.GetGPUTemp()
|
2020-09-04 18:57:12 +00:00
|
|
|
|
2020-09-04 19:04:44 +00:00
|
|
|
if cpuTemp >= SPEED_FULL_TEMP || gpuTemp >= SPEED_FULL_TEMP {
|
2020-09-05 19:49:49 +00:00
|
|
|
if SPEED_MINIMUM > 100 {
|
|
|
|
fan.SetSpeed(SPEED_MINIMUM)
|
|
|
|
} else {
|
|
|
|
fan.SetSpeed(100)
|
|
|
|
}
|
2020-09-04 19:04:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP {
|
2020-09-05 19:49:49 +00:00
|
|
|
if SPEED_MINIMUM > 75 {
|
|
|
|
fan.SetSpeed(SPEED_MINIMUM)
|
|
|
|
} else {
|
|
|
|
fan.SetSpeed(75)
|
|
|
|
}
|
2020-09-04 19:04:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cpuTemp >= SPEED_FIFTY_PERCENT_TEMP || gpuTemp >= SPEED_FIFTY_PERCENT_TEMP {
|
2020-09-05 19:49:49 +00:00
|
|
|
if SPEED_MINIMUM > 50 {
|
|
|
|
fan.SetSpeed(SPEED_MINIMUM)
|
|
|
|
} else {
|
|
|
|
fan.SetSpeed(50)
|
|
|
|
}
|
2020-09-04 19:04:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP {
|
2020-09-05 19:49:49 +00:00
|
|
|
if SPEED_MINIMUM > 25 {
|
|
|
|
fan.SetSpeed(SPEED_MINIMUM)
|
|
|
|
} else {
|
|
|
|
fan.SetSpeed(25)
|
|
|
|
}
|
2020-09-04 19:04:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-09-04 19:20:15 +00:00
|
|
|
if cpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP {
|
2020-09-05 19:49:49 +00:00
|
|
|
fan.SetSpeed(SPEED_MINIMUM)
|
2020-09-04 19:04:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We should never get here but...
|
2020-09-05 19:49:49 +00:00
|
|
|
// Maxing fan to be on the safe side
|
2020-09-21 00:20:42 +00:00
|
|
|
log.Print("pf-fan : This should never happen")
|
2020-09-04 19:04:44 +00:00
|
|
|
fan.SetSpeed(100)
|
2020-09-04 18:57:12 +00:00
|
|
|
}
|
|
|
|
}
|