fermentord/internal/dwingest/nats.go

103 lines
2.3 KiB
Go
Raw Normal View History

2022-07-19 09:11:50 +00:00
package dwingest
import (
"context"
"encoding/json"
2022-07-25 08:55:25 +00:00
"fmt"
2022-07-19 09:11:50 +00:00
"log"
"sync"
"time"
"git.joco.dk/sng/fermentord/internal/configuration"
"git.joco.dk/sng/fermentord/internal/controllers"
"git.joco.dk/sng/fermentord/pkg/temperature"
2022-07-23 14:10:34 +00:00
"git.joco.dk/sng/fermentord/pkg/tilt"
2022-07-19 09:11:50 +00:00
"github.com/getsentry/sentry-go"
"github.com/nats-io/nats.go"
)
type DWIngest struct {
chTemperatureReading chan temperature.TemperatureReading
chState chan controllers.ChamberState
2022-07-25 08:55:25 +00:00
hub *sentry.Hub
2022-07-19 09:11:50 +00:00
}
func NewDWIngest() *DWIngest {
return &DWIngest{
chTemperatureReading: make(chan temperature.TemperatureReading, 3600),
chState: make(chan controllers.ChamberState, 100),
2022-07-25 08:55:25 +00:00
hub: sentry.CurrentHub().Clone(),
2022-07-19 09:11:50 +00:00
}
}
func (p *DWIngest) AddReading(reading temperature.TemperatureReading) {
2022-07-24 07:38:40 +00:00
select {
case p.chTemperatureReading <- reading:
2022-07-25 08:55:25 +00:00
break
2022-07-24 07:38:40 +00:00
default:
2022-07-25 08:55:25 +00:00
err := fmt.Errorf("channel overflow on dwingest temperature channel")
p.hub.CaptureException(err)
log.Print(err)
2022-07-24 07:38:40 +00:00
}
2022-07-19 09:11:50 +00:00
}
func (p *DWIngest) AddState(state controllers.ChamberState) {
2022-07-24 07:38:40 +00:00
select {
case p.chState <- state:
2022-07-25 08:55:25 +00:00
break
2022-07-24 07:38:40 +00:00
default:
2022-07-25 08:55:25 +00:00
err := fmt.Errorf("channel overflow on dwingest state channel")
p.hub.CaptureException(err)
log.Print(err)
2022-07-24 07:38:40 +00:00
}
2022-07-19 09:11:50 +00:00
}
2022-03-15 16:20:47 +00:00
func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) {
2022-07-19 09:11:50 +00:00
defer wg.Done()
2022-07-25 08:55:25 +00:00
defer p.hub.Flush(10 * time.Second)
2022-07-19 09:11:50 +00:00
for {
select {
case reading := <-p.chTemperatureReading:
2022-07-25 08:55:25 +00:00
publish(config.NATS.Subject.Temp, js, p.hub, reading)
2022-07-19 09:11:50 +00:00
2022-07-23 14:10:34 +00:00
case state := <-p.chState:
2022-07-25 08:55:25 +00:00
publish(config.NATS.Subject.State, js, p.hub, State{
2022-07-23 14:39:56 +00:00
Time: time.Now().UTC(),
2022-07-23 14:10:34 +00:00
State: controllers.ChamberStateMap[state],
2022-07-25 08:55:25 +00:00
})
2022-07-19 09:11:50 +00:00
2022-07-23 14:10:34 +00:00
case t := <-tilt.C:
2022-07-25 08:55:25 +00:00
publish(config.NATS.Subject.Tilt, js, p.hub, Tilt{
2022-07-23 14:39:56 +00:00
Time: time.Now().UTC(),
2022-07-23 14:10:34 +00:00
Color: string(t.Color()),
Gravity: t.Gravity(),
Temperature: t.Celsius(),
2022-07-25 08:55:25 +00:00
})
2022-07-19 09:11:50 +00:00
case <-ctx.Done():
return
}
}
}
2022-07-23 14:10:34 +00:00
func publish(subject string, js nats.JetStream, hub *sentry.Hub, reading any) error {
b, err := json.Marshal(reading)
if err != nil {
hub.CaptureException(err)
log.Printf("Error marshaling JSON: %v", err)
return err
}
2022-07-29 21:30:49 +00:00
_, err = js.Publish(subject, b, nats.AckWait(5*time.Second))
2022-07-23 14:10:34 +00:00
if err != nil {
hub.CaptureException(err)
2022-07-26 10:59:17 +00:00
log.Print(err)
2022-07-23 14:10:34 +00:00
}
return err
}