fix(pkg/sensor): read values
This commit is contained in:
		| @@ -27,6 +27,10 @@ func (s *DS18B20) GetSensorModel() types.SensorModel { | ||||
| // Read measured values | ||||
| func (s *DS18B20) Read() ([]types.MeasuredValue, error) { | ||||
|  | ||||
| 	if s.WireID == nil { | ||||
| 		return nil, fmt.Errorf("WireID is not specified for sensor %v", s.Name()) | ||||
| 	} | ||||
|  | ||||
| 	data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave")) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("Can not read data from sensor %v", s.SensorName) | ||||
|   | ||||
| @@ -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