fermentord/internal/configuration/config.go

95 lines
3.1 KiB
Go

package configuration
import (
"log"
"time"
"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
)
type Configuration struct {
NATS struct {
URL string `mapstructure:"url"`
Stream string `mapstructure:"stream"`
Subject struct {
Event string `mapstructure:"event"`
State string `mapstructure:"state"`
Temp string `mapstructure:"temp"`
Tilt string `mapstructure:"tilt"`
} `mapstructure:"subject"`
} `mapstructure:"nats"`
HTTP struct {
Port int16 `mapstructure:"port"`
} `mapstructure:"http"`
Sensors struct {
Wort string `mapstructure:"wort"`
Chamber string `mapstructure:"chamber"`
Ambient string `mapstructure:"ambient"`
Weight float64 `mapstructure:"weight"`
} `mapstructure:"sensors"`
FermentationTemperature float64 `mapstructure:"fermentation_temp"`
DeltaTemperatureCool float64 `mapstructure:"delta_temp_cool"`
DeltaTemperatureHeat float64 `mapstructure:"delta_temp_heat"`
HeaterDutyCycle time.Duration `mapstructure:"heater_duty_cycle"`
Limits struct {
MinChamberTemperature float64 `mapstructure:"min_chamber_temp"`
MaxChamberTemperature float64 `mapstructure:"max_chamber_temp"`
MinCoolerRuntimeSecs float64 `mapstructure:"min_cooler_runtime_secs"`
MaxCoolerRuntimeSecs float64 `mapstructure:"max_cooler_runtime_secs"`
MinCoolerCooldownSecs float64 `mapstructure:"min_cooler_cooldown_secs"`
HeaterGraceTimeSecs float64 `mapstructure:"heater_grace_time_secs"`
} `mapstructure:"limits"`
PID struct {
Kp float64 `mapstructure:"kp"` // 2.0
Ki float64 `mapstructure:"ki"` // 0.0001
Kd float64 `mapstructure:"kd"` // 2.0
} `mapstructure:"pid"`
}
func LoadConfiguration() *Configuration {
hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second)
viper.SetDefault("heater_duty_cycle", 1*time.Second)
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)
viper.AddConfigPath("/etc")
viper.AddConfigPath("/usr/local/etc")
viper.AddConfigPath(".")
viper.SetConfigName("fermentord")
viper.SetConfigType("toml")
if err := viper.ReadInConfig(); err != nil {
log.Printf("Error loading configuration: %v", err)
}
config := &Configuration{}
if err := viper.Unmarshal(config); err != nil {
hub.CaptureException(err)
log.Fatal(err)
}
return config
}