fermentord/pkg/tilt/main.go

58 lines
903 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() {
2022-07-23 23:16:40 +00:00
C = make(chan Tilt, 100)
2022-07-23 14:10:34 +00:00
}
2022-04-29 19:02:33 +00:00
func PollSensors(ctx context.Context, wg *sync.WaitGroup, interval time.Duration, scanDuration time.Duration) {
2022-07-23 23:16:40 +00:00
defer wg.Done()
2022-04-29 19:02:33 +00:00
if interval < scanDuration {
log.Fatal("Unable to use interval < scanDuration")
}
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
scan(ctx, scanDuration)
2022-07-23 23:16:40 +00:00
case <-ctx.Done():
return
2022-04-29 19:02:33 +00:00
}
}
}
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
2022-07-24 06:28:16 +00:00
// Log tilt readings
t.Print()
2022-07-23 23:16:40 +00:00
select {
case C <- t:
// Message sent
default:
// No recipients on channel
}
2022-04-29 19:02:33 +00:00
}
}