2018-11-20 21:55:06 +00:00
|
|
|
package sensor
|
|
|
|
|
2019-02-24 21:46:36 +00:00
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-02-24 21:46:36 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/internal/collect"
|
|
|
|
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-02-24 21:46:36 +00:00
|
|
|
)
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// ReadHumidities returns a list of measured humidities by humidity sensors
|
2019-06-17 21:37:48 +00:00
|
|
|
func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.Humidity, error) {
|
2019-06-13 19:25:32 +00:00
|
|
|
humidityChannel := make(chan *types.Humidity, len(humiditySensors))
|
2019-06-17 21:37:48 +00:00
|
|
|
errorChannel := make(chan error, len(humiditySensors))
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(len(humiditySensors))
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
for _, humiditySensor := range humiditySensors {
|
2019-06-18 21:02:11 +00:00
|
|
|
go humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return nil, prittyprint.FormatErrors(errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
humidities := collect.Humidities(humidityChannel)
|
|
|
|
|
|
|
|
return humidities, nil
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
// ReadHumiditiesWriteIntoChannel reads the humidity values of humidity sensors and writes them into a channel
|
2019-06-18 21:02:11 +00:00
|
|
|
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-02-24 21:46:36 +00:00
|
|
|
for _, humiditySensor := range humiditySensors {
|
2019-06-18 21:02:11 +00:00
|
|
|
humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel
|
2019-06-18 21:02:11 +00:00
|
|
|
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
2019-06-13 19:25:32 +00:00
|
|
|
for {
|
2019-06-17 21:37:48 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
2019-06-18 21:02:11 +00:00
|
|
|
ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, round, humidityChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// ReadTemperatures returns a list of measured temperatures by temperature sensors
|
2019-06-23 12:17:57 +00:00
|
|
|
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64) ([]*types.Temperature, error) {
|
2019-06-13 19:25:32 +00:00
|
|
|
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
|
2019-06-17 21:37:48 +00:00
|
|
|
errorChannel := make(chan error, len(temperatureSensors))
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(len(temperatureSensors))
|
|
|
|
|
|
|
|
for _, temperatureSensor := range temperatureSensors {
|
2019-06-23 11:33:09 +00:00
|
|
|
go temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
errors := collect.Errors(errorChannel)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return nil, prittyprint.FormatErrors(errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatures := collect.Temperatures(temperatureChannel)
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
return temperatures, nil
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
|
2019-06-23 12:17:57 +00:00
|
|
|
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-02-24 21:46:36 +00:00
|
|
|
for _, temperatureSensor := range temperatureSensors {
|
2019-06-23 11:33:09 +00:00
|
|
|
temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
|
2019-06-23 12:17:57 +00:00
|
|
|
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
2019-02-24 21:46:36 +00:00
|
|
|
for {
|
2019-06-17 21:37:48 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
2019-06-23 11:33:09 +00:00
|
|
|
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, degree, round, temperatureChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 11:33:09 +00:00
|
|
|
|
2019-06-23 12:17:57 +00:00
|
|
|
func convertTemperatureMeasurementUnit(value float64, fromDegree types.TemperatureUnit, toDegree types.TemperatureUnit) float64 {
|
2019-06-23 11:33:09 +00:00
|
|
|
|
|
|
|
switch fromDegree {
|
|
|
|
// Celsius
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitCelsius:
|
2019-06-23 11:33:09 +00:00
|
|
|
switch toDegree {
|
|
|
|
// Celsius -> Celsius
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitCelsius:
|
2019-06-23 11:33:09 +00:00
|
|
|
return value
|
|
|
|
// Celsius -> Fahrenheit
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitFahrenheit:
|
2019-06-23 11:33:09 +00:00
|
|
|
return (value * 9 / 5) + 32
|
|
|
|
// Celsius -> Kelvin
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitKelvin:
|
2019-06-23 11:33:09 +00:00
|
|
|
return value + 273.15
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fahrenheit
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitFahrenheit:
|
2019-06-23 11:33:09 +00:00
|
|
|
switch toDegree {
|
|
|
|
// Fahrenheit -> Celsius
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitCelsius:
|
2019-06-23 11:33:09 +00:00
|
|
|
return (value - 32) * 5 / 9
|
|
|
|
// Fahrenheit -> Fahrenheit
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitFahrenheit:
|
2019-06-23 11:33:09 +00:00
|
|
|
return value
|
|
|
|
// Fahrenheit -> Kelvin
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitKelvin:
|
2019-06-23 11:33:09 +00:00
|
|
|
return (value-32)*5/9 + 273.15
|
|
|
|
}
|
|
|
|
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitKelvin:
|
2019-06-23 11:33:09 +00:00
|
|
|
switch toDegree {
|
|
|
|
// Kelvin -> Celsius
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitCelsius:
|
2019-06-23 11:33:09 +00:00
|
|
|
return value - 273.15
|
|
|
|
// Kelvin -> Fahrenheit
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitFahrenheit:
|
2019-06-23 11:33:09 +00:00
|
|
|
return (value-273.15)*9/5 + 32
|
|
|
|
// Kevin -> Kelvin
|
2019-06-23 12:17:57 +00:00
|
|
|
case types.TemperatureUnitKelvin:
|
2019-06-23 11:33:09 +00:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-06-23 12:17:57 +00:00
|
|
|
func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
|
2019-06-23 11:33:09 +00:00
|
|
|
switch unit {
|
|
|
|
case "celsius":
|
2019-06-23 12:17:57 +00:00
|
|
|
return types.TemperatureUnitCelsius, nil
|
2019-06-23 11:33:09 +00:00
|
|
|
case "fahrenheit":
|
2019-06-23 12:17:57 +00:00
|
|
|
return types.TemperatureUnitFahrenheit, nil
|
2019-06-23 11:33:09 +00:00
|
|
|
case "kelvin":
|
2019-06-23 12:17:57 +00:00
|
|
|
return types.TemperatureUnitKelvin, nil
|
2019-06-23 11:33:09 +00:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Can not determine temperature measurement unit")
|
|
|
|
}
|
|
|
|
}
|