fermentord/internal/configuration/config.go

98 lines
3.2 KiB
Go
Raw Normal View History

2022-03-11 20:06:33 +00:00
package configuration
import (
"log"
"time"
"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
)
2022-03-15 16:20:47 +00:00
type Configuration struct {
2022-03-11 20:06:33 +00:00
NATS struct {
URL string `mapstructure:"url"`
Stream string `mapstructure:"stream"`
Subject struct {
Event string `mapstructure:"event"`
2022-03-11 20:06:33 +00:00
State string `mapstructure:"state"`
Temp string `mapstructure:"temp"`
2022-07-23 14:10:34 +00:00
Tilt string `mapstructure:"tilt"`
2022-03-11 20:06:33 +00:00
} `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"`
HeaterEnabled bool `mapstructure:"heater_enabled"`
CoolerEnabled bool `mapstructure:"cooler_enabled"`
2022-03-11 20:06:33 +00:00
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"`
}
2022-03-15 16:20:47 +00:00
func LoadConfiguration() *Configuration {
2022-03-11 20:06:33 +00:00
hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second)
viper.SetDefault("cooler_enabled", true)
viper.SetDefault("heater_enabled", true)
2022-03-11 20:06:33 +00:00
viper.SetDefault("http.port", 8000)
2022-07-23 14:39:56 +00:00
viper.SetDefault("nats.stream", "DWJONDAHL")
2022-07-23 16:44:05 +00:00
viper.SetDefault("nats.subject.event", "DWJONDAHL.ingest.fermentor.ingest_event")
2022-07-23 14:39:56 +00:00
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")
2022-03-11 20:06:33 +00:00
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)
}
2022-03-15 16:20:47 +00:00
config := &Configuration{}
2022-03-11 20:06:33 +00:00
if err := viper.Unmarshal(config); err != nil {
hub.CaptureException(err)
log.Fatal(err)
}
return config
}