From df6355e91a40e96158a816b67b747f095f31e0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Sun, 6 Mar 2022 22:52:21 +0100 Subject: [PATCH] Fix trigger and other small stuff --- README.md | 4 ++-- cmd/fermentord/db.go | 12 ++++++++---- cmd/fermentord/main.go | 4 +++- pkg/temperature/ds18b20.go | 9 +++------ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2b86cc1..5e0dda7 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ to your /boot/config.txt. The default data pin is GPIO4, but that can be changed from 4 to x with `dtoverlay=w1-gpio,gpiopin=x`. ```bash -echo dtoverlay=w1-gpio,pgiopin=4 >> /boot/config.txt +echo dtoverlay=w1-gpio,gpiopin=4,pullup=0 >> /boot/config.txt modprobe wire modprobe w1-gpio @@ -78,5 +78,5 @@ modprobe w1-therm ```bash cd cmd/fermentord -GOARCH=arm GOOS=linux go build +GOARCH=arm GOARM=6 GOOS=linux go build ``` diff --git a/cmd/fermentord/db.go b/cmd/fermentord/db.go index 6612cf0..25752e7 100644 --- a/cmd/fermentord/db.go +++ b/cmd/fermentord/db.go @@ -17,14 +17,14 @@ var ( f *os.File ) -func persistData(ctx context.Context, wg *sync.WaitGroup, chState <-chan controllers.ChamberState, chTemp <-chan temperature.TemperatureReading) { +func persistData(ctx context.Context, wg *sync.WaitGroup, chState <-chan controllers.ChamberState, chTemp <-chan temperature.TemperatureReading, config *controllers.ControllerConfig) { var err error + hub := sentry.CurrentHub().Clone() + defer hub.Flush(10 * time.Second) defer wg.Done() - hub := sentry.CurrentHub().Clone() - - f, err = os.OpenFile("data.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640) + 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) @@ -68,6 +68,10 @@ func persistData(ctx context.Context, wg *sync.WaitGroup, chState <-chan control } 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 } } diff --git a/cmd/fermentord/main.go b/cmd/fermentord/main.go index fe43d1c..c452161 100644 --- a/cmd/fermentord/main.go +++ b/cmd/fermentord/main.go @@ -55,7 +55,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config defer close(chDbState) wg.Add(3) - go persistData(ctx, wg, chDbState, chDbTemp) + go persistData(ctx, wg, chDbState, chDbTemp, config) go ctrl.Run(ctx, wg) go temperature.PollSensors(ctx, wg, 1*time.Second) @@ -175,6 +175,7 @@ func main() { go srv.ListenAndServe() daemon.WaitForSignalsDefault() + log.Print("Shutting down") // Initiate graceful shutdown. wg.Add(1) @@ -182,6 +183,7 @@ func main() { shutdown() nc.Close() wg.Wait() + log.Print("Shutdown complete") } func shutdownHTTP(ctx context.Context, wg *sync.WaitGroup, srv *http.Server) { diff --git a/pkg/temperature/ds18b20.go b/pkg/temperature/ds18b20.go index aa5ced4..e44084a 100644 --- a/pkg/temperature/ds18b20.go +++ b/pkg/temperature/ds18b20.go @@ -90,7 +90,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D } // Read all sensors. - readSensors() + readSensors(hub) case c := <-ConfigUpdates: sensors = c @@ -102,10 +102,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D } } -func readSensors() { - hub := sentry.CurrentHub().Clone() - defer hub.Flush(10 * time.Second) - +func readSensors(hub *sentry.Hub) { for _, sensor := range sensors { start := time.Now() t, err := read(sensor) @@ -145,7 +142,7 @@ func triggerBulkRead() error { } defer f.Close() - _, err = f.WriteString("trigger") + _, err = f.WriteString("trigger\n") return err }