fix(pkg/sensor): use context to terminate go routines
This commit is contained in:
		@@ -1,6 +1,9 @@
 | 
			
		||||
package sensor
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-flucky/flucky/pkg/internal/collect"
 | 
			
		||||
@@ -9,81 +12,102 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ReadHumidities returns a list of measured humidities by humidity sensors
 | 
			
		||||
func ReadHumidities(humiditySensors []HumiditySensor) ([]*types.Humidity, error) {
 | 
			
		||||
	errorChannel := make(chan error, len(humiditySensors))
 | 
			
		||||
func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.Humidity, error) {
 | 
			
		||||
	humidityChannel := make(chan *types.Humidity, len(humiditySensors))
 | 
			
		||||
	ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel)
 | 
			
		||||
	errorChannel := make(chan error, len(humiditySensors))
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
		go humiditySensor.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, wg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
 | 
			
		||||
	errors := collect.Errors(errorChannel)
 | 
			
		||||
	if len(errors) > 0 {
 | 
			
		||||
		return nil, prittyprint.FormatErrors(errors)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	humidities := collect.Humidities(humidityChannel)
 | 
			
		||||
 | 
			
		||||
	if round != 0 {
 | 
			
		||||
		for _, humidity := range humidities {
 | 
			
		||||
			humidity.HumidityValue = math.Round(humidity.HumidityValue/round) * round
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return humidities, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadHumiditiesWriteIntoChannel reads the humidity values of humidity sensors and writes them into a channel
 | 
			
		||||
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
 | 
			
		||||
	for _, humiditySensor := range humiditySensors {
 | 
			
		||||
		humiditySensor.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, wg)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
 | 
			
		||||
	for {
 | 
			
		||||
		ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel)
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ctx.Done():
 | 
			
		||||
			errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
 | 
			
		||||
			return
 | 
			
		||||
		default:
 | 
			
		||||
			ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, humidityChannel, errorChannel, nil)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadTemperatures returns a list of measured temperatures by temperature sensors
 | 
			
		||||
func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) {
 | 
			
		||||
	errorChannel := make(chan error, len(temperatureSensors))
 | 
			
		||||
func ReadTemperatures(temperatureSensors []TemperatureSensor, round float64) ([]*types.Temperature, error) {
 | 
			
		||||
	temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
 | 
			
		||||
	ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel)
 | 
			
		||||
	errorChannel := make(chan error, len(temperatureSensors))
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
		go temperatureSensor.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, wg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
 | 
			
		||||
	errors := collect.Errors(errorChannel)
 | 
			
		||||
	if len(errors) > 0 {
 | 
			
		||||
		return nil, prittyprint.FormatErrors(errors)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	temperatures := collect.Temperatures(temperatureChannel)
 | 
			
		||||
 | 
			
		||||
	if round != 0 {
 | 
			
		||||
		for _, temperature := range temperatures {
 | 
			
		||||
			temperature.TemperatureValue = math.Round(temperature.TemperatureValue/round) * round
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return temperatures, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
 | 
			
		||||
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
 | 
			
		||||
	for _, temperatureSensor := range temperatureSensors {
 | 
			
		||||
		temperatureSensor.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, wg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
 | 
			
		||||
	for {
 | 
			
		||||
		ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel)
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ctx.Done():
 | 
			
		||||
			errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
 | 
			
		||||
			return
 | 
			
		||||
		default:
 | 
			
		||||
			ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, temperatureChannel, errorChannel, nil)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user