fix(pkg/daemon): use measuredValue interface instead of different structs
This commit is contained in:
@ -3,7 +3,6 @@ package sensor
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -22,8 +21,9 @@ func (s *DHT11) GetSensorModel() types.SensorModel {
|
||||
return s.Sensor.SensorModel
|
||||
}
|
||||
|
||||
// ReadHumidity measure the humidity
|
||||
func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
|
||||
// Read measured values
|
||||
func (s *DHT11) Read() ([]types.MeasuredValue, error) {
|
||||
|
||||
err := dht.HostInit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||
@ -39,117 +39,59 @@ func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
|
||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||
}
|
||||
|
||||
humidityValue, _, err := dht.Read()
|
||||
humidityValue, temperatureValue, err := dht.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read error: %v", err)
|
||||
}
|
||||
|
||||
if round != 0 {
|
||||
humidityValue = math.Round(humidityValue/round) * round
|
||||
measuredValues := []types.MeasuredValue{
|
||||
&types.Humidity{
|
||||
HumidityID: uuid.NewV4().String(),
|
||||
HumidityValue: humidityValue,
|
||||
HumidityFromDate: time.Now(),
|
||||
HumidityTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
},
|
||||
&types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
},
|
||||
}
|
||||
|
||||
humidity := &types.Humidity{
|
||||
HumidityID: uuid.NewV4().String(),
|
||||
HumidityValue: humidityValue,
|
||||
HumidityFromDate: time.Now(),
|
||||
HumidityTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
}
|
||||
|
||||
return humidity, nil
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// ReadHumidityWriteIntoChannel and write values into a channel
|
||||
func (s *DHT11) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||
// channel.
|
||||
func (s *DHT11) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
humidity, err := s.ReadHumidity(round)
|
||||
measuredValues, err := s.Read()
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
humidityChannel <- humidity
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValueChannel <- measuredValue
|
||||
}
|
||||
}
|
||||
|
||||
// ReadHumidityContinously into a channel until context closed
|
||||
func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
||||
// ReadContinously reads the measured values continously from the sensor and
|
||||
// writes them to a channel.
|
||||
func (s *DHT11) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadTemperature measure the temperature
|
||||
func (s *DHT11) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
||||
err := dht.HostInit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||
}
|
||||
|
||||
gpio, err := types.GPIOToString(*s.GPIONumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||
}
|
||||
|
||||
_, temperatureValue, err := dht.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read error: %v", err)
|
||||
}
|
||||
|
||||
// Convert temperature degree
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
if round != 0 {
|
||||
temperatureValue = math.Round(temperatureValue/round) * round
|
||||
}
|
||||
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
}
|
||||
|
||||
return temperature, nil
|
||||
}
|
||||
|
||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
||||
func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
temperature, err := s.ReadTemperature(degree, round)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
temperatureChannel <- temperature
|
||||
}
|
||||
|
||||
// ReadTemperatureContinously into a channel until context closed
|
||||
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
||||
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package sensor
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -22,8 +21,9 @@ func (s *DHT22) GetSensorModel() types.SensorModel {
|
||||
return s.Sensor.SensorModel
|
||||
}
|
||||
|
||||
// ReadHumidity measure the humidity
|
||||
func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
|
||||
// Read measured values
|
||||
func (s *DHT22) Read() ([]types.MeasuredValue, error) {
|
||||
|
||||
err := dht.HostInit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||
@ -39,118 +39,59 @@ func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
|
||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||
}
|
||||
|
||||
humidityValue, _, err := dht.Read()
|
||||
humidityValue, temperatureValue, err := dht.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read error: %v", err)
|
||||
}
|
||||
|
||||
if round != 0 {
|
||||
humidityValue = math.Round(humidityValue/round) * round
|
||||
measuredValues := []types.MeasuredValue{
|
||||
&types.Humidity{
|
||||
HumidityID: uuid.NewV4().String(),
|
||||
HumidityValue: humidityValue,
|
||||
HumidityFromDate: time.Now(),
|
||||
HumidityTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
},
|
||||
&types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
},
|
||||
}
|
||||
|
||||
humidity := &types.Humidity{
|
||||
HumidityID: uuid.NewV4().String(),
|
||||
HumidityValue: humidityValue,
|
||||
HumidityFromDate: time.Now(),
|
||||
HumidityTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
}
|
||||
|
||||
return humidity, nil
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// ReadHumidityWriteIntoChannel and write values into a channel
|
||||
func (s *DHT22) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||
// channel.
|
||||
func (s *DHT22) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
humidity, err := s.ReadHumidity(round)
|
||||
measuredValues, err := s.Read()
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
humidityChannel <- humidity
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValueChannel <- measuredValue
|
||||
}
|
||||
}
|
||||
|
||||
// ReadHumidityContinously into a channel until context closed
|
||||
func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
||||
// ReadContinously reads the measured values continously from the sensor and
|
||||
// writes them to a channel.
|
||||
func (s *DHT22) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadTemperature measure the temperature
|
||||
func (s *DHT22) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
||||
err := dht.HostInit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||
}
|
||||
|
||||
gpio, err := types.GPIOToString(*s.GPIONumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||
}
|
||||
|
||||
_, temperatureValue, err := dht.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read error: %v", err)
|
||||
}
|
||||
|
||||
// Convert temperature degree
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
// round
|
||||
if round != 0 {
|
||||
temperatureValue = math.Round(temperatureValue/round) * round
|
||||
}
|
||||
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
}
|
||||
|
||||
return temperature, nil
|
||||
}
|
||||
|
||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
||||
func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
temperature, err := s.ReadTemperature(degree, round)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
temperatureChannel <- temperature
|
||||
}
|
||||
|
||||
// ReadTemperatureContinously into a channel until context closed
|
||||
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
||||
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -25,13 +24,8 @@ func (s *DS18B20) GetSensorModel() types.SensorModel {
|
||||
return s.Sensor.SensorModel
|
||||
}
|
||||
|
||||
// GetSensor return the sensor struct
|
||||
func (s *DS18B20) GetSensor() *types.Sensor {
|
||||
return s.Sensor
|
||||
}
|
||||
|
||||
// ReadTemperature measure the temperature
|
||||
func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
||||
// Read measured values
|
||||
func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
|
||||
|
||||
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
|
||||
if err != nil {
|
||||
@ -52,50 +46,49 @@ func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (
|
||||
|
||||
temperatureValue := c / 1000
|
||||
|
||||
// Convert temperature degree
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
// round
|
||||
if round != 0 {
|
||||
temperatureValue = math.Round(temperatureValue/round) * round
|
||||
measuredValues := []types.MeasuredValue{
|
||||
&types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
},
|
||||
}
|
||||
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
SensorID: s.SensorID,
|
||||
}
|
||||
|
||||
return temperature, nil
|
||||
return measuredValues, nil
|
||||
|
||||
}
|
||||
|
||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
||||
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||
// channel.
|
||||
func (s *DS18B20) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
temperature, err := s.ReadTemperature(degree, round)
|
||||
measuredValues, err := s.Read()
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
temperatureChannel <- temperature
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValueChannel <- measuredValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ReadTemperatureContinously into a channel until context closed
|
||||
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
||||
// ReadContinously reads the measured values continously from the sensor and
|
||||
// writes them to a channel.
|
||||
func (s *DS18B20) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
||||
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,18 +7,9 @@ import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// HumiditySensor is a interface to describe required functions to measure humidities
|
||||
type HumiditySensor interface {
|
||||
type Sensor interface {
|
||||
GetSensorModel() types.SensorModel
|
||||
ReadHumidity(round float64) (*types.Humidity, error)
|
||||
ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup)
|
||||
ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error)
|
||||
}
|
||||
|
||||
// TemperatureSensor is a interface to describe required functions to measure temperatures
|
||||
type TemperatureSensor interface {
|
||||
GetSensorModel() types.SensorModel
|
||||
ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error)
|
||||
ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
|
||||
ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
|
||||
Read() ([]types.MeasuredValue, error)
|
||||
ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup)
|
||||
ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error)
|
||||
}
|
||||
|
@ -10,16 +10,16 @@ import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// ReadHumidities returns a list of measured humidities by humidity sensors
|
||||
func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.Humidity, error) {
|
||||
humidityChannel := make(chan *types.Humidity, len(humiditySensors))
|
||||
errorChannel := make(chan error, len(humiditySensors))
|
||||
// Read measured values from sensors
|
||||
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
|
||||
measuredValueChannel := make(chan types.MeasuredValue, len(sensors))
|
||||
errorChannel := make(chan error, len(sensors))
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(len(humiditySensors))
|
||||
wg.Add(len(sensors))
|
||||
|
||||
for _, humiditySensor := range humiditySensors {
|
||||
go humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
||||
for _, sensor := range sensors {
|
||||
go sensor.ReadContinously(ctx, measuredValueChannel, errorChannel)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
@ -29,132 +29,29 @@ func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.H
|
||||
return nil, prittyprint.FormatErrors(errors)
|
||||
}
|
||||
|
||||
humidities := collect.Humidities(humidityChannel)
|
||||
measuredValues := collect.MeasuredValues(measuredValueChannel)
|
||||
|
||||
return humidities, nil
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// ReadHumiditiesWriteIntoChannel reads the humidity values of humidity sensors and writes them into a channel
|
||||
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
for _, humiditySensor := range humiditySensors {
|
||||
humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
||||
// 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) {
|
||||
for _, sensor := range sensors {
|
||||
sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel
|
||||
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
||||
// ReadContinuously reads the measured values continously from sensors and writes
|
||||
// them to a channel.
|
||||
func ReadContinuously(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
||||
return
|
||||
default:
|
||||
ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, round, humidityChannel, errorChannel, nil)
|
||||
ReadChannel(ctx, sensors, measuredValueChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadTemperatures returns a list of measured temperatures by temperature sensors
|
||||
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64) ([]*types.Temperature, error) {
|
||||
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
|
||||
errorChannel := make(chan error, len(temperatureSensors))
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(len(temperatureSensors))
|
||||
|
||||
for _, temperatureSensor := range temperatureSensors {
|
||||
go temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
errors := collect.Errors(errorChannel)
|
||||
if len(errors) > 0 {
|
||||
return nil, prittyprint.FormatErrors(errors)
|
||||
}
|
||||
|
||||
temperatures := collect.Temperatures(temperatureChannel)
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
|
||||
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
for _, temperatureSensor := range temperatureSensors {
|
||||
temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
|
||||
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
||||
return
|
||||
default:
|
||||
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, degree, round, temperatureChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func convertTemperatureMeasurementUnit(value float64, fromDegree types.TemperatureUnit, toDegree types.TemperatureUnit) float64 {
|
||||
|
||||
switch fromDegree {
|
||||
// Celsius
|
||||
case types.TemperatureUnitCelsius:
|
||||
switch toDegree {
|
||||
// Celsius -> Celsius
|
||||
case types.TemperatureUnitCelsius:
|
||||
return value
|
||||
// Celsius -> Fahrenheit
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return (value * 9 / 5) + 32
|
||||
// Celsius -> Kelvin
|
||||
case types.TemperatureUnitKelvin:
|
||||
return value + 273.15
|
||||
}
|
||||
|
||||
// Fahrenheit
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
switch toDegree {
|
||||
// Fahrenheit -> Celsius
|
||||
case types.TemperatureUnitCelsius:
|
||||
return (value - 32) * 5 / 9
|
||||
// Fahrenheit -> Fahrenheit
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return value
|
||||
// Fahrenheit -> Kelvin
|
||||
case types.TemperatureUnitKelvin:
|
||||
return (value-32)*5/9 + 273.15
|
||||
}
|
||||
|
||||
case types.TemperatureUnitKelvin:
|
||||
switch toDegree {
|
||||
// Kelvin -> Celsius
|
||||
case types.TemperatureUnitCelsius:
|
||||
return value - 273.15
|
||||
// Kelvin -> Fahrenheit
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return (value-273.15)*9/5 + 32
|
||||
// Kevin -> Kelvin
|
||||
case types.TemperatureUnitKelvin:
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
|
||||
switch unit {
|
||||
case "celsius":
|
||||
return types.TemperatureUnitCelsius, nil
|
||||
case "fahrenheit":
|
||||
return types.TemperatureUnitFahrenheit, nil
|
||||
case "kelvin":
|
||||
return types.TemperatureUnitKelvin, nil
|
||||
default:
|
||||
return "", fmt.Errorf("Can not determine temperature measurement unit")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user