fix(pkg/sensor): read values
This commit is contained in:
		@@ -5,38 +5,30 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-flucky/flucky/pkg/internal/collect"
 | 
			
		||||
	"github.com/go-flucky/flucky/pkg/internal/prittyprint"
 | 
			
		||||
	"github.com/go-flucky/flucky/pkg/types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Read measured values from sensors
 | 
			
		||||
func Read(sensors []Sensor) ([]types.MeasuredValue, error) {
 | 
			
		||||
	measuredValueChannel := make(chan types.MeasuredValue, 0)
 | 
			
		||||
	errorChannel := make(chan error, 0)
 | 
			
		||||
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
 | 
			
		||||
 | 
			
		||||
	wg := new(sync.WaitGroup)
 | 
			
		||||
	wg.Add(len(sensors))
 | 
			
		||||
	// TODO: Execute Read with go function
 | 
			
		||||
	cachesMeasuredValues := make([]types.MeasuredValue, 0)
 | 
			
		||||
 | 
			
		||||
	for _, sensor := range sensors {
 | 
			
		||||
		go sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
 | 
			
		||||
		measuredValues, err := sensor.Read()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		cachesMeasuredValues = append(cachesMeasuredValues, measuredValues...)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
 | 
			
		||||
	errors := collect.Errors(errorChannel)
 | 
			
		||||
	if len(errors) > 0 {
 | 
			
		||||
		return nil, prittyprint.FormatErrors(errors)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	measuredValues := collect.MeasuredValues(measuredValueChannel)
 | 
			
		||||
 | 
			
		||||
	return measuredValues, nil
 | 
			
		||||
	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)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user