fermentord/pkg/tilt/main.go
Søren Rasmussen 56d33782c8
All checks were successful
continuous-integration/drone/push Build is passing
WIP: Stabilize
2022-07-24 01:16:40 +02:00

54 lines
867 B
Go

package tilt
import (
"context"
"log"
"sync"
"time"
)
var (
C chan Tilt
)
func init() {
C = make(chan Tilt, 100)
}
func PollSensors(ctx context.Context, wg *sync.WaitGroup, interval time.Duration, scanDuration time.Duration) {
defer wg.Done()
if interval < scanDuration {
log.Fatal("Unable to use interval < scanDuration")
}
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
scan(ctx, scanDuration)
case <-ctx.Done():
return
}
}
}
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())
select {
case C <- t:
// Message sent
default:
// No recipients on channel
}
}
}