fermentord/pkg/tilt/main.go

39 lines
709 B
Go
Raw Normal View History

2022-04-29 19:02:33 +00:00
package tilt
import (
"context"
"log"
"sync"
"time"
)
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())
}
}