48 lines
783 B
Go
48 lines
783 B
Go
package tilt
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
C chan Tilt
|
|
)
|
|
|
|
func init() {
|
|
C = make(chan Tilt, 10)
|
|
}
|
|
|
|
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())
|
|
|
|
C <- t
|
|
}
|
|
}
|