From 0b3d35721efaa8062ab47de5f28eed0de60d1746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Sun, 24 Jul 2022 08:25:02 +0200 Subject: [PATCH] Support NATS cluster --- cmd/fermentord/loop.go | 2 +- cmd/fermentord/main.go | 5 ++++- internal/configuration/config.go | 8 +++++--- internal/controllers/chamber.go | 5 +---- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/fermentord/loop.go b/cmd/fermentord/loop.go index d874f80..f021398 100644 --- a/cmd/fermentord/loop.go +++ b/cmd/fermentord/loop.go @@ -34,7 +34,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config defer display.Close() // Controller - ctrl := controllers.NewChamberController("Chamber 1", *config) + ctrl := controllers.NewChamberController(*config) // Configuration reload loadConfiguration := func() { diff --git a/cmd/fermentord/main.go b/cmd/fermentord/main.go index 0a5bd27..dfeb933 100644 --- a/cmd/fermentord/main.go +++ b/cmd/fermentord/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "strings" "sync" "time" @@ -31,7 +32,9 @@ func main() { config := configuration.LoadConfiguration() // NATS - nc, err := nats.Connect(config.NATS.URL) + servers := strings.Join(config.NATS.Servers, ",") + userInfo := nats.UserInfo(config.NATS.Username, config.NATS.Password) + nc, err := nats.Connect(servers, userInfo) if err != nil { log.Fatal(err) } diff --git a/internal/configuration/config.go b/internal/configuration/config.go index 20e9b83..9d45ab2 100644 --- a/internal/configuration/config.go +++ b/internal/configuration/config.go @@ -10,9 +10,11 @@ import ( type Configuration struct { NATS struct { - URL string `mapstructure:"url"` - Stream string `mapstructure:"stream"` - Subject struct { + Servers []string `mapstructure:"servers"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Stream string `mapstructure:"stream"` + Subject struct { Event string `mapstructure:"event"` State string `mapstructure:"state"` Temp string `mapstructure:"temp"` diff --git a/internal/controllers/chamber.go b/internal/controllers/chamber.go index 820421d..a2079fb 100644 --- a/internal/controllers/chamber.go +++ b/internal/controllers/chamber.go @@ -36,8 +36,6 @@ type ChamberController struct { C chan ChamberState isPaused bool - name string - // Current temperature readings. ambientTemperature float64 chamberTemperature float64 @@ -50,10 +48,9 @@ type ChamberController struct { hub *sentry.Hub } -func NewChamberController(name string, config configuration.Configuration) *ChamberController { +func NewChamberController(config configuration.Configuration) *ChamberController { return &ChamberController{ C: make(chan ChamberState), - name: name, config: config, pid: NewPIDController(config.PID.Kp, config.PID.Ki, config.PID.Kd), chTemp: make(chan temperature.TemperatureReading),