diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index be74a35..98a3d81 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -1,10 +1,13 @@ package temperature import ( + "context" "fmt" "log" "os" + "github.com/go-flucky/flucky/pkg/types/typeswitch" + "github.com/go-flucky/flucky/pkg/rgbled" "github.com/go-flucky/flucky/pkg/cli" @@ -40,14 +43,17 @@ var readTemperatureCmd = &cobra.Command{ sensors = cnf.GetTemperatureSensorsByName(args) } - measuredValues, err := sensor.Read(sensors) + ctx := context.Background() + measuredValues, err := sensor.Read(ctx, sensors) if err != nil { rgbled.Error(rgbLEDs) log.Fatalln(err) } + temperatures := typeswitch.TemperatureValues(measuredValues) + // print temperatures on stdout - cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) + cli.PrintMeasuredValues(temperatures, cnf, os.Stdout) if logs { logfile := logfile.New(cnf.Device.Logfile) diff --git a/pkg/sensor/ds18b20.go b/pkg/sensor/ds18b20.go index e37e340..9ed73bf 100644 --- a/pkg/sensor/ds18b20.go +++ b/pkg/sensor/ds18b20.go @@ -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) diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index 554ccbd..4f7ca48 100644 --- a/pkg/sensor/sensor.go +++ b/pkg/sensor/sensor.go @@ -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) }