PKGBUILD/pkg/sensor/sensor.go

50 lines
1.4 KiB
Go

package sensor
import (
"context"
"fmt"
"sync"
"github.com/go-flucky/flucky/pkg/types"
)
// Read measured values from sensors
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
// TODO: Execute Read with go function
cachesMeasuredValues := make([]types.MeasuredValue, 0)
for _, sensor := range sensors {
measuredValues, err := sensor.Read()
if err != nil {
return nil, err
}
cachesMeasuredValues = append(cachesMeasuredValues, measuredValues...)
}
return cachesMeasuredValues, nil
}
// ReadChannel reads the measured values from sensors and writes them to a
// channel.
func ReadChannel(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
// TODO: Execute ReadCallel with go function if wg is available
for _, sensor := range sensors {
sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
}
}
// ReadContinuously reads the measured values continously from sensors and writes
// them to a channel.
func ReadContinuously(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
return
default:
ReadChannel(ctx, sensors, measuredValueChannel, errorChannel, nil)
}
}
}