Support NATS cluster

This commit is contained in:
Søren Rasmussen 2022-07-24 08:25:02 +02:00
parent d873538b70
commit 0b3d35721e
4 changed files with 11 additions and 9 deletions

View file

@ -34,7 +34,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
defer display.Close() defer display.Close()
// Controller // Controller
ctrl := controllers.NewChamberController("Chamber 1", *config) ctrl := controllers.NewChamberController(*config)
// Configuration reload // Configuration reload
loadConfiguration := func() { loadConfiguration := func() {

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -31,7 +32,9 @@ func main() {
config := configuration.LoadConfiguration() config := configuration.LoadConfiguration()
// NATS // 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -10,9 +10,11 @@ import (
type Configuration struct { type Configuration struct {
NATS struct { NATS struct {
URL string `mapstructure:"url"` Servers []string `mapstructure:"servers"`
Stream string `mapstructure:"stream"` Username string `mapstructure:"username"`
Subject struct { Password string `mapstructure:"password"`
Stream string `mapstructure:"stream"`
Subject struct {
Event string `mapstructure:"event"` Event string `mapstructure:"event"`
State string `mapstructure:"state"` State string `mapstructure:"state"`
Temp string `mapstructure:"temp"` Temp string `mapstructure:"temp"`

View file

@ -36,8 +36,6 @@ type ChamberController struct {
C chan ChamberState C chan ChamberState
isPaused bool isPaused bool
name string
// Current temperature readings. // Current temperature readings.
ambientTemperature float64 ambientTemperature float64
chamberTemperature float64 chamberTemperature float64
@ -50,10 +48,9 @@ type ChamberController struct {
hub *sentry.Hub hub *sentry.Hub
} }
func NewChamberController(name string, config configuration.Configuration) *ChamberController { func NewChamberController(config configuration.Configuration) *ChamberController {
return &ChamberController{ return &ChamberController{
C: make(chan ChamberState), C: make(chan ChamberState),
name: name,
config: config, config: config,
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),