Remove text DB

This commit is contained in:
Søren Rasmussen 2022-07-19 10:46:30 +02:00
parent 0622341878
commit cee6149f70
3 changed files with 1 additions and 106 deletions

View file

@ -1,93 +0,0 @@
package main
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"git.joco.dk/sng/fermentord/internal/configuration"
"git.joco.dk/sng/fermentord/internal/controllers"
"git.joco.dk/sng/fermentord/pkg/temperature"
"github.com/getsentry/sentry-go"
)
var (
f *os.File
)
func persistData(ctx context.Context, wg *sync.WaitGroup, chState <-chan controllers.ChamberState, chTemp <-chan temperature.TemperatureReading, config *configuration.ControllerConfig) {
var err error
hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second)
defer wg.Done()
f, err = os.OpenFile(config.DataPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
hub.CaptureException(err)
log.Fatal(err)
}
defer f.Close()
for {
select {
case state, ok := <-chState:
if !ok {
break
}
err := datlog("%v STATE %v", time.Now().Unix(), controllers.ChamberStateMap[state])
if err != nil {
hub.CaptureException(err)
log.Printf("Error persisting state change: %v", err)
}
err = f.Sync()
if err != nil {
hub.CaptureException(err)
log.Printf("Error flushing data file: %v", err)
}
case t, ok := <-chTemp:
if !ok {
break
}
err := datlog("%v TEMP %v %v", t.Time.Unix(), t.Sensor, t.Degrees)
if err != nil {
hub.CaptureException(err)
log.Printf("Error persisting temperature reading: %v", err)
}
err = f.Sync()
if err != nil {
hub.CaptureException(err)
log.Printf("Error flushing data file: %v", err)
}
case <-ctx.Done():
if err := datlog("%v STATE OFF", time.Now().Unix()); err != nil {
hub.CaptureException(err)
log.Printf("Error persisting state change: %v", err)
}
return
}
}
}
func datlog(format string, data ...interface{}) error {
_, err := fmt.Fprintf(f, format, data...)
if err != nil {
return err
}
_, err = fmt.Fprintln(f)
if err != nil {
return err
}
return nil
}

View file

@ -47,13 +47,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
} }
defer gpio.Close() defer gpio.Close()
chDbTemp := make(chan temperature.TemperatureReading, 10) wg.Add(2)
chDbState := make(chan controllers.ChamberState, 10)
defer close(chDbTemp)
defer close(chDbState)
wg.Add(3)
go persistData(ctx, wg, chDbState, chDbTemp, config)
go ctrl.Run(ctx, wg) go ctrl.Run(ctx, wg)
go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight) go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight)
@ -61,7 +55,6 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
select { select {
case reading := <-temperature.C: case reading := <-temperature.C:
chCtrlTemp <- reading chCtrlTemp <- reading
chDbTemp <- reading
b, err := json.Marshal(reading) b, err := json.Marshal(reading)
if err != nil { if err != nil {
@ -98,8 +91,6 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
gpio.StartHeater() gpio.StartHeater()
} }
chDbState <- state
// Publish to NATS // Publish to NATS
_, err = js.Publish(config.NATS.Subject.State, []byte(controllers.ChamberStateMap[state])) _, err = js.Publish(config.NATS.Subject.State, []byte(controllers.ChamberStateMap[state]))
if err != nil { if err != nil {

View file

@ -22,8 +22,6 @@ type ControllerConfig struct {
Port int16 `mapstructure:"port"` Port int16 `mapstructure:"port"`
} `mapstructure:"http"` } `mapstructure:"http"`
DataPath string `mapstructure:"data_path"`
Sensors struct { Sensors struct {
Wort string `mapstructure:"wort"` Wort string `mapstructure:"wort"`
Chamber string `mapstructure:"chamber"` Chamber string `mapstructure:"chamber"`
@ -56,7 +54,6 @@ func LoadConfiguration() *ControllerConfig {
defer hub.Flush(10 * time.Second) defer hub.Flush(10 * time.Second)
viper.SetDefault("http.port", 8000) viper.SetDefault("http.port", 8000)
viper.SetDefault("main.data_path", "./data.txt")
viper.SetDefault("nats.stream", "FERMENTOR") viper.SetDefault("nats.stream", "FERMENTOR")
viper.SetDefault("nats.subject.state", "FERMENTOR.state") viper.SetDefault("nats.subject.state", "FERMENTOR.state")
viper.SetDefault("nats.subject.temp", "FERMENTOR.temp") viper.SetDefault("nats.subject.temp", "FERMENTOR.temp")