Refactor configuration

This commit is contained in:
Søren Rasmussen 2022-03-15 17:20:47 +01:00
parent d28db0788f
commit 7adca39509
6 changed files with 11 additions and 11 deletions

View file

@ -24,7 +24,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.ControllerConfig) { func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) {
hub := sentry.CurrentHub().Clone() hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
defer wg.Done() defer wg.Done()

View file

@ -11,7 +11,7 @@ import (
) )
type API struct { type API struct {
config *configuration.ControllerConfig config *configuration.Configuration
configChangeCallback func() configChangeCallback func()
nc *nats.Conn nc *nats.Conn
} }

View file

@ -8,7 +8,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type ControllerConfig struct { type Configuration struct {
NATS struct { NATS struct {
URL string `mapstructure:"url"` URL string `mapstructure:"url"`
Stream string `mapstructure:"stream"` Stream string `mapstructure:"stream"`
@ -49,7 +49,7 @@ type ControllerConfig struct {
} `mapstructure:"pid"` } `mapstructure:"pid"`
} }
func LoadConfiguration() *ControllerConfig { func LoadConfiguration() *Configuration {
hub := sentry.CurrentHub().Clone() hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
@ -79,7 +79,7 @@ func LoadConfiguration() *ControllerConfig {
log.Printf("Error loading configuration: %v", err) log.Printf("Error loading configuration: %v", err)
} }
config := &ControllerConfig{} config := &Configuration{}
if err := viper.Unmarshal(config); err != nil { if err := viper.Unmarshal(config); err != nil {
hub.CaptureException(err) hub.CaptureException(err)
log.Fatal(err) log.Fatal(err)

View file

@ -27,8 +27,8 @@ var ChamberStateMap = map[ChamberState]string{
} }
type ChamberController struct { type ChamberController struct {
ConfigUpdates chan configuration.ControllerConfig ConfigUpdates chan configuration.Configuration
config configuration.ControllerConfig config configuration.Configuration
// Current state. // Current state.
chamberState ChamberState chamberState ChamberState
@ -49,7 +49,7 @@ type ChamberController struct {
hub *sentry.Hub hub *sentry.Hub
} }
func NewChamberController(name string, config configuration.ControllerConfig) *ChamberController { func NewChamberController(name string, config configuration.Configuration) *ChamberController {
return &ChamberController{ return &ChamberController{
C: make(chan ChamberState), C: make(chan ChamberState),
name: name, name: name,
@ -57,7 +57,7 @@ func NewChamberController(name string, config configuration.ControllerConfig) *C
pid: NewPIDController(config.PID.Kp, config.PID.Ki, config.PID.Kd), pid: NewPIDController(config.PID.Kp, config.PID.Ki, config.PID.Kd),
chTemp: make(chan temperature.TemperatureReading), chTemp: make(chan temperature.TemperatureReading),
chamberState: ChamberStateIdle, chamberState: ChamberStateIdle,
ConfigUpdates: make(chan configuration.ControllerConfig, 1), ConfigUpdates: make(chan configuration.Configuration, 1),
hub: sentry.CurrentHub().Clone(), hub: sentry.CurrentHub().Clone(),
} }
} }

View file

@ -7,7 +7,7 @@ import (
"git.joco.dk/sng/fermentord/pkg/temperature" "git.joco.dk/sng/fermentord/pkg/temperature"
) )
func ReloadConfiguration(config *configuration.ControllerConfig, ctrl *ChamberController) { func ReloadConfiguration(config *configuration.Configuration, ctrl *ChamberController) {
log.Printf("Reloading configuration") log.Printf("Reloading configuration")
temperature.ConfigUpdates <- []string{ temperature.ConfigUpdates <- []string{

View file

@ -34,7 +34,7 @@ func (p *DWIngest) AddState(state controllers.ChamberState) {
p.chState <- state p.chState <- state
} }
func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.ControllerConfig) { func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) {
hub := sentry.CurrentHub().Clone() hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
defer wg.Done() defer wg.Done()