fermentord/pkg/tilt/main.go

49 lines
783 B
Go
Raw Normal View History

2022-04-29 19:02:33 +00:00
package tilt
import (
"context"
"log"
"sync"
"time"
)
2022-07-23 14:10:34 +00:00
var (
C chan Tilt
)
func init() {
C = make(chan Tilt, 10)
}
2022-04-29 19:02:33 +00:00
func PollSensors(ctx context.Context, wg *sync.WaitGroup, interval time.Duration, scanDuration time.Duration) {
if interval < scanDuration {
log.Fatal("Unable to use interval < scanDuration")
}
defer wg.Done()
ticker := time.NewTicker(interval)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
scan(ctx, scanDuration)
}
}
}
func scan(ctx context.Context, timeout time.Duration) {
scanner := NewScanner()
scanner.Scan(ctx, timeout)
for _, t := range scanner.Tilts() {
color := string(t.Color())
metricGravity.WithLabelValues(color).Set(t.Gravity())
metricTemp.WithLabelValues(color).Set(t.Celsius())
2022-07-23 14:10:34 +00:00
C <- t
2022-04-29 19:02:33 +00:00
}
}