package sensor import ( "sync" "github.com/go-flucky/flucky/pkg/internal/collect" "github.com/go-flucky/flucky/pkg/internal/prittyprint" "github.com/go-flucky/flucky/pkg/types" ) // ReadHumidities returns a list of measured humidities by humidity sensors func ReadHumidities(humiditySensors []HumiditySensor) ([]*types.Humidity, error) { errorChannel := make(chan error, len(humiditySensors)) humidityChannel := make(chan *types.Humidity, len(humiditySensors)) ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel) errorList := collect.Errors(errorChannel) if len(errorList) != 0 { return nil, prittyprint.FormatErrors(errorList) } humidityList := collect.Humidities(humidityChannel) return humidityList, nil } // ReadHumiditiesIntoChannel reads the humidity values of humidity sensors and writes them into a channel func ReadHumiditiesIntoChannel(humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) { wg := new(sync.WaitGroup) wg.Add(len(humiditySensors)) for _, humiditySensor := range humiditySensors { go func(hs HumiditySensor) { defer wg.Done() humidity, err := hs.ReadHumidity() if err != nil { errorChannel <- err return } humidityChannel <- humidity }(humiditySensor) } wg.Wait() } // ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel func ReadHumiditiesContinuously(humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) { for { ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel) } } // ReadTemperatures returns a list of measured temperatures by temperature sensors func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) { errorChannel := make(chan error, len(temperatureSensors)) temperatureChannel := make(chan *types.Temperature, len(temperatureSensors)) ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel) errorList := collect.Errors(errorChannel) if len(errorList) != 0 { return nil, prittyprint.FormatErrors(errorList) } temperatureList := collect.Temperatures(temperatureChannel) return temperatureList, nil } // ReadTemperaturesIntoChannel reads the temperature values of temperature sensors and writes them into a channel func ReadTemperaturesIntoChannel(temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { wg := new(sync.WaitGroup) wg.Add(len(temperatureSensors)) for _, temperatureSensor := range temperatureSensors { go func(ts TemperatureSensor) { defer wg.Done() temperature, err := ts.ReadTemperature() if err != nil { errorChannel <- err return } temperatureChannel <- temperature }(temperatureSensor) } wg.Wait() } // ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann func ReadTemperaturesContinuously(temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { for { ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel) } }