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()
// Controller
ctrl := controllers.NewChamberController("Chamber 1", *config)
ctrl := controllers.NewChamberController(*config)
// Configuration reload
loadConfiguration := func() {

View file

@ -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)
}

View file

@ -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"`

View file

@ -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),