PKGBUILD/pkg/sensor/sensor.go

50 lines
1.4 KiB
Go
Raw Normal View History

package sensor
2019-02-24 21:46:36 +00:00
import (
"context"
"fmt"
2019-02-24 21:46:36 +00:00
"sync"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/types"
2019-02-24 21:46:36 +00:00
)
// Read measured values from sensors
2019-06-27 07:17:34 +00:00
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
2019-06-27 07:17:34 +00:00
// TODO: Execute Read with go function
cachesMeasuredValues := make([]types.MeasuredValue, 0)
2019-02-24 21:46:36 +00:00
for _, sensor := range sensors {
2019-06-27 07:17:34 +00:00
measuredValues, err := sensor.Read()
if err != nil {
return nil, err
}
cachesMeasuredValues = append(cachesMeasuredValues, measuredValues...)
}
2019-06-27 07:17:34 +00:00
return cachesMeasuredValues, nil
}
2019-02-24 21:46:36 +00:00
// 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) {
2019-06-27 07:17:34 +00:00
// TODO: Execute ReadCallel with go function if wg is available
for _, sensor := range sensors {
sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
2019-02-24 21:46:36 +00:00
}
}
2019-02-24 21:46:36 +00:00
// 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)
}
2019-02-24 21:46:36 +00:00
}
}