Add gokrazy configuration

This commit is contained in:
Søren Rasmussen 2024-06-15 16:20:32 +02:00
parent e2c724536b
commit 9419a70c70
6 changed files with 81 additions and 39 deletions

View file

@ -44,7 +44,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) {
chn.Subscribe(chConfigChange) chn.Subscribe(chConfigChange)
chn.Subscribe(temperature.ConfigUpdate) chn.Subscribe(temperature.ConfigUpdate)
chn.Subscribe(ctrl.ConfigUpdates) chn.Subscribe(ctrl.ConfigUpdates)
//chn.Subscribe(display.ConfiguUpdate) //chn.Subscribe(display.ConfigUpdate)
chn.Notify(configuration.Global()) chn.Notify(configuration.Global())
viper.OnConfigChange(chn.OnConfigChange) viper.OnConfigChange(chn.OnConfigChange)
viper.WatchConfig() viper.WatchConfig()

View file

@ -1,6 +1,10 @@
//go:build !gokrazy
package configuration package configuration
import ( import (
"bytes"
_ "embed"
"log" "log"
"sync" "sync"
@ -11,25 +15,24 @@ import (
var ( var (
globalConfig Configuration globalConfig Configuration
globalLock *sync.Mutex globalLock *sync.Mutex
//go:embed fermentord.toml
globalData []byte
) )
func init() { func init() {
globalLock = &sync.Mutex{} globalLock = &sync.Mutex{}
} }
func Global() (c Configuration) { func Global() Configuration {
globalLock.Lock() globalLock.Lock()
c = globalConfig defer globalLock.Unlock()
globalLock.Unlock()
return return globalConfig
} }
func Initialize() { func Initialize() {
setDefaults() setDefaults()
viper.AddConfigPath("/etc")
viper.AddConfigPath("/usr/local/etc")
viper.AddConfigPath(".")
viper.SetConfigName("fermentord") viper.SetConfigName("fermentord")
viper.SetConfigType("toml") viper.SetConfigType("toml")
} }
@ -38,17 +41,22 @@ func LoadConfiguration() Configuration {
globalLock.Lock() globalLock.Lock()
defer globalLock.Unlock() defer globalLock.Unlock()
if err := viper.ReadInConfig(); err != nil { buf := bytes.NewBuffer(globalData)
if err := viper.ReadConfig(buf); err != nil {
panic(err)
}
if err := viper.MergeInConfig(); err != nil {
log.Printf("Error loading configuration: %v", err) log.Printf("Error loading configuration: %v", err)
} }
config := &Configuration{} config := Configuration{}
if err := viper.Unmarshal(config); err != nil { if err := viper.Unmarshal(&config); err != nil {
sentry.CaptureException(err) sentry.CaptureException(err)
log.Fatal(err) log.Fatal(err)
} }
globalConfig = *config globalConfig = config
return globalConfig return globalConfig
} }

View file

@ -1,9 +1,5 @@
package configuration package configuration
import (
"github.com/spf13/viper"
)
type Configuration struct { type Configuration struct {
Brew struct { Brew struct {
UUID string `mapstructure:"uuid"` UUID string `mapstructure:"uuid"`
@ -54,25 +50,3 @@ type Configuration struct {
Kd float64 `mapstructure:"kd"` // 2.0 Kd float64 `mapstructure:"kd"` // 2.0
} `mapstructure:"pid"` } `mapstructure:"pid"`
} }
func setDefaults() {
viper.SetDefault("cooler_enabled", true)
viper.SetDefault("heater_enabled", true)
viper.SetDefault("http.port", 8000)
viper.SetDefault("nats.stream", "DWJONDAHL")
viper.SetDefault("nats.subject.event", "DWJONDAHL.ingest.fermentor.ingest_event")
viper.SetDefault("nats.subject.state", "DWJONDAHL.ingest.fermentor.ingest_state")
viper.SetDefault("nats.subject.temp", "DWJONDAHL.ingest.fermentor.ingest_temperature_reading")
viper.SetDefault("nats.subject.tilt", "DWJONDAHL.ingest.fermentor.ingest_tilt_reading")
viper.SetDefault("nats.url", "nats.service.consul")
viper.SetDefault("pid.kd", 2.0)
viper.SetDefault("pid.ki", 0.0001)
viper.SetDefault("pid.kp", 2.0)
viper.SetDefault("limits.heater_grace_time_secs", 1800)
viper.SetDefault("limits.max_chamber_temp", 40)
viper.SetDefault("limits.max_cooler_runtime_secs", 86400)
viper.SetDefault("limits.min_chamber_temp", 5)
viper.SetDefault("limits.min_cooler_cooldown_secs", 300)
viper.SetDefault("limits.min_cooler_runtime_secs", 300)
viper.SetDefault("sensors.weight", 0.8)
}

View file

@ -0,0 +1,40 @@
heater_enabled = true
cooler_enabled = true
fermentation_temp = 20.5
delta_temperature_cool = 1.0
delta_temperature_heat = 0.1
[brew]
uuid = "00000000-0000-0000-0000-000000000000"
[nats]
url = "nats://nats.service.consul:4222"
stream = "DWJONDAHL"
[nats.subject]
event = "DWJONDAHL.ingest.fermentor.ingest_event"
state = "DWJONDAHL.ingest.fermentor.ingest_state"
temp = "DWJONDAHL.ingest.fermentor.ingest_temperature_reading"
tilt = "DWJONDAHL.ingest.fermentor.ingest_tilt_reading"
[http]
port = 8000
[sensors]
wort = ""
chamber = ""
ambient = ""
weight = 0.8
[limits]
min_chamber_temp = 0.0
max_chamber_temp = 25
min_cooler_runtime_secs = 180
max_cooler_runtime_secs = 86400
min_cooler_cooldown_secs = 300
heater_grace_time_secs = 1800
[pid]
kp = 2.0
ki = 0.0001
kd = 2.0

View file

@ -0,0 +1,11 @@
//go:build !gokrazy
package configuration
import "github.com/spf13/viper"
func setDefaults() {
viper.AddConfigPath("/etc")
viper.AddConfigPath("/usr/local/etc")
viper.AddConfigPath(".")
}

View file

@ -0,0 +1,9 @@
//go:build gokrazy
package configuration
import "github.com/spf13/viper"
func setDefaults() {
viper.AddConfigPath("/perm")
}