Fix trigger and other small stuff

This commit is contained in:
Søren Rasmussen 2022-03-06 22:52:21 +01:00
parent 9576f8e664
commit df6355e91a
4 changed files with 16 additions and 13 deletions

View file

@ -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`. changed from 4 to x with `dtoverlay=w1-gpio,gpiopin=x`.
```bash ```bash
echo dtoverlay=w1-gpio,pgiopin=4 >> /boot/config.txt echo dtoverlay=w1-gpio,gpiopin=4,pullup=0 >> /boot/config.txt
modprobe wire modprobe wire
modprobe w1-gpio modprobe w1-gpio
@ -78,5 +78,5 @@ modprobe w1-therm
```bash ```bash
cd cmd/fermentord cd cmd/fermentord
GOARCH=arm GOOS=linux go build GOARCH=arm GOARM=6 GOOS=linux go build
``` ```

View file

@ -17,14 +17,14 @@ var (
f *os.File 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 var err error
hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second)
defer wg.Done() defer wg.Done()
hub := sentry.CurrentHub().Clone() f, err = os.OpenFile(config.DataPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
f, err = os.OpenFile("data.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil { if err != nil {
hub.CaptureException(err) hub.CaptureException(err)
log.Fatal(err) log.Fatal(err)
@ -68,6 +68,10 @@ func persistData(ctx context.Context, wg *sync.WaitGroup, chState <-chan control
} }
case <-ctx.Done(): 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 return
} }
} }

View file

@ -55,7 +55,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
defer close(chDbState) defer close(chDbState)
wg.Add(3) wg.Add(3)
go persistData(ctx, wg, chDbState, chDbTemp) 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) go temperature.PollSensors(ctx, wg, 1*time.Second)
@ -175,6 +175,7 @@ func main() {
go srv.ListenAndServe() go srv.ListenAndServe()
daemon.WaitForSignalsDefault() daemon.WaitForSignalsDefault()
log.Print("Shutting down")
// Initiate graceful shutdown. // Initiate graceful shutdown.
wg.Add(1) wg.Add(1)
@ -182,6 +183,7 @@ func main() {
shutdown() shutdown()
nc.Close() nc.Close()
wg.Wait() wg.Wait()
log.Print("Shutdown complete")
} }
func shutdownHTTP(ctx context.Context, wg *sync.WaitGroup, srv *http.Server) { func shutdownHTTP(ctx context.Context, wg *sync.WaitGroup, srv *http.Server) {

View file

@ -90,7 +90,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
} }
// Read all sensors. // Read all sensors.
readSensors() readSensors(hub)
case c := <-ConfigUpdates: case c := <-ConfigUpdates:
sensors = c sensors = c
@ -102,10 +102,7 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
} }
} }
func readSensors() { func readSensors(hub *sentry.Hub) {
hub := sentry.CurrentHub().Clone()
defer hub.Flush(10 * time.Second)
for _, sensor := range sensors { for _, sensor := range sensors {
start := time.Now() start := time.Now()
t, err := read(sensor) t, err := read(sensor)
@ -145,7 +142,7 @@ func triggerBulkRead() error {
} }
defer f.Close() defer f.Close()
_, err = f.WriteString("trigger") _, err = f.WriteString("trigger\n")
return err return err
} }