fix(pkg/sensor): read values
This commit is contained in:
		@@ -1,10 +1,13 @@
 | 
				
			|||||||
package temperature
 | 
					package temperature
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/go-flucky/flucky/pkg/types/typeswitch"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-flucky/flucky/pkg/rgbled"
 | 
						"github.com/go-flucky/flucky/pkg/rgbled"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-flucky/flucky/pkg/cli"
 | 
						"github.com/go-flucky/flucky/pkg/cli"
 | 
				
			||||||
@@ -40,14 +43,17 @@ var readTemperatureCmd = &cobra.Command{
 | 
				
			|||||||
			sensors = cnf.GetTemperatureSensorsByName(args)
 | 
								sensors = cnf.GetTemperatureSensorsByName(args)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		measuredValues, err := sensor.Read(sensors)
 | 
							ctx := context.Background()
 | 
				
			||||||
 | 
							measuredValues, err := sensor.Read(ctx, sensors)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			rgbled.Error(rgbLEDs)
 | 
								rgbled.Error(rgbLEDs)
 | 
				
			||||||
			log.Fatalln(err)
 | 
								log.Fatalln(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							temperatures := typeswitch.TemperatureValues(measuredValues)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// print temperatures on stdout
 | 
							// print temperatures on stdout
 | 
				
			||||||
		cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
 | 
							cli.PrintMeasuredValues(temperatures, cnf, os.Stdout)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if logs {
 | 
							if logs {
 | 
				
			||||||
			logfile := logfile.New(cnf.Device.Logfile)
 | 
								logfile := logfile.New(cnf.Device.Logfile)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,6 +27,10 @@ func (s *DS18B20) GetSensorModel() types.SensorModel {
 | 
				
			|||||||
// Read measured values
 | 
					// Read measured values
 | 
				
			||||||
func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
 | 
					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"))
 | 
						data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, fmt.Errorf("Can not read data from sensor %v", s.SensorName)
 | 
							return nil, fmt.Errorf("Can not read data from sensor %v", s.SensorName)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,38 +5,30 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-flucky/flucky/pkg/internal/collect"
 | 
					 | 
				
			||||||
	"github.com/go-flucky/flucky/pkg/internal/prittyprint"
 | 
					 | 
				
			||||||
	"github.com/go-flucky/flucky/pkg/types"
 | 
						"github.com/go-flucky/flucky/pkg/types"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Read measured values from sensors
 | 
					// Read measured values from sensors
 | 
				
			||||||
func Read(sensors []Sensor) ([]types.MeasuredValue, error) {
 | 
					func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
 | 
				
			||||||
	measuredValueChannel := make(chan types.MeasuredValue, 0)
 | 
					 | 
				
			||||||
	errorChannel := make(chan error, 0)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wg := new(sync.WaitGroup)
 | 
						// TODO: Execute Read with go function
 | 
				
			||||||
	wg.Add(len(sensors))
 | 
						cachesMeasuredValues := make([]types.MeasuredValue, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, sensor := range sensors {
 | 
						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()
 | 
						return cachesMeasuredValues, nil
 | 
				
			||||||
 | 
					 | 
				
			||||||
	errors := collect.Errors(errorChannel)
 | 
					 | 
				
			||||||
	if len(errors) > 0 {
 | 
					 | 
				
			||||||
		return nil, prittyprint.FormatErrors(errors)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	measuredValues := collect.MeasuredValues(measuredValueChannel)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return measuredValues, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ReadChannel reads the measured values from sensors and writes them to a
 | 
					// ReadChannel reads the measured values from sensors and writes them to a
 | 
				
			||||||
// channel.
 | 
					// channel.
 | 
				
			||||||
func ReadChannel(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
 | 
					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 {
 | 
						for _, sensor := range sensors {
 | 
				
			||||||
		sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
 | 
							sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user