Publish events to dwingest

This commit is contained in:
Søren Rasmussen 2022-07-23 16:39:56 +02:00
parent fe94a51acd
commit dd8a48905a
4 changed files with 21 additions and 10 deletions

View file

@ -80,20 +80,25 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
display.SetTemperature(reading) display.SetTemperature(reading)
case ev := <-gpio.C: case ev := <-gpio.C:
var evs string
switch ev { switch ev {
case hw.DoorClosed: case hw.DoorClosed:
gpio.LightsOff() gpio.LightsOff()
gpio.StartFan() gpio.StartFan()
ctrl.Resume() ctrl.Resume()
evs = "DOOR_CLOSED"
case hw.DoorOpened: case hw.DoorOpened:
ctrl.Pause() ctrl.Pause()
gpio.LightsOn() gpio.LightsOn()
gpio.StopFan() gpio.StopFan()
evs = "DOOR_OPENED"
} }
b, err := json.Marshal(map[string]interface{}{ b, err := json.Marshal(map[string]interface{}{
"event": ev, "time": time.Now().UTC(),
"event": evs,
}) })
if err != nil { if err != nil {
hub.CaptureException(err) hub.CaptureException(err)

View file

@ -56,11 +56,11 @@ func LoadConfiguration() *Configuration {
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
viper.SetDefault("http.port", 8000) viper.SetDefault("http.port", 8000)
viper.SetDefault("nats.stream", "FERMENTOR") viper.SetDefault("nats.stream", "DWJONDAHL")
viper.SetDefault("nats.subject.event", "FERMENTOR.event") viper.SetDefault("nats.subject.event", "DWJONDAHL.ingest.fermentor.event")
viper.SetDefault("nats.subject.state", "FERMENTOR.state") viper.SetDefault("nats.subject.state", "DWJONDAHL.ingest.fermentor.ingest_state")
viper.SetDefault("nats.subject.temp", "FERMENTOR.temp") viper.SetDefault("nats.subject.temp", "DWJONDAHL.ingest.fermentor.ingest_temperature_reading")
viper.SetDefault("nats.subject.tilt", "FERMENTOR.tilt") viper.SetDefault("nats.subject.tilt", "DWJONDAHL.ingest.fermentor.ingest_tilt_reading")
viper.SetDefault("nats.url", "nats.service.consul") viper.SetDefault("nats.url", "nats.service.consul")
viper.SetDefault("pid.kd", 2.0) viper.SetDefault("pid.kd", 2.0)
viper.SetDefault("pid.ki", 0.0001) viper.SetDefault("pid.ki", 0.0001)

View file

@ -47,12 +47,14 @@ func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStrea
case state := <-p.chState: case state := <-p.chState:
reading := State{ reading := State{
Time: time.Now().UTC(),
State: controllers.ChamberStateMap[state], State: controllers.ChamberStateMap[state],
} }
publish(config.NATS.Subject.State, js, hub, reading) publish(config.NATS.Subject.State, js, hub, reading)
case t := <-tilt.C: case t := <-tilt.C:
reading := Tilt{ reading := Tilt{
Time: time.Now().UTC(),
Color: string(t.Color()), Color: string(t.Color()),
Gravity: t.Gravity(), Gravity: t.Gravity(),
Temperature: t.Celsius(), Temperature: t.Celsius(),

View file

@ -1,11 +1,15 @@
package dwingest package dwingest
import "time"
type State struct { type State struct {
State string `json:"state"` Time time.Time `json:"time"`
State string `json:"state"`
} }
type Tilt struct { type Tilt struct {
Color string `json:"color"` Time time.Time `json:"time"`
Gravity float64 `json:"gravity"` Color string `json:"color"`
Temperature float64 `json:"temperature"` Gravity float64 `json:"gravity"`
Temperature float64 `json:"temperature"`
} }