refac(pkg/sensor): temperature measurement unit

This commit is contained in:
2019-06-23 14:17:57 +02:00
parent 3bb10a4f78
commit 5e03d987c5
19 changed files with 91 additions and 76 deletions

View File

@ -55,7 +55,7 @@ func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumidityS
}
// ReadTemperatures returns a list of measured temperatures by temperature sensors
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.Degree, round float64) ([]*types.Temperature, error) {
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))
@ -79,14 +79,14 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.Degre
}
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
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.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
@ -98,47 +98,47 @@ func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []Temp
}
}
func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, toDegree types.Degree) float64 {
func convertTemperatureMeasurementUnit(value float64, fromDegree types.TemperatureUnit, toDegree types.TemperatureUnit) float64 {
switch fromDegree {
// Celsius
case types.DegreeCelsius:
case types.TemperatureUnitCelsius:
switch toDegree {
// Celsius -> Celsius
case types.DegreeCelsius:
case types.TemperatureUnitCelsius:
return value
// Celsius -> Fahrenheit
case types.DegreeFahrenheit:
case types.TemperatureUnitFahrenheit:
return (value * 9 / 5) + 32
// Celsius -> Kelvin
case types.DegreeKelvin:
case types.TemperatureUnitKelvin:
return value + 273.15
}
// Fahrenheit
case types.DegreeFahrenheit:
case types.TemperatureUnitFahrenheit:
switch toDegree {
// Fahrenheit -> Celsius
case types.DegreeCelsius:
case types.TemperatureUnitCelsius:
return (value - 32) * 5 / 9
// Fahrenheit -> Fahrenheit
case types.DegreeFahrenheit:
case types.TemperatureUnitFahrenheit:
return value
// Fahrenheit -> Kelvin
case types.DegreeKelvin:
case types.TemperatureUnitKelvin:
return (value-32)*5/9 + 273.15
}
case types.DegreeKelvin:
case types.TemperatureUnitKelvin:
switch toDegree {
// Kelvin -> Celsius
case types.DegreeCelsius:
case types.TemperatureUnitCelsius:
return value - 273.15
// Kelvin -> Fahrenheit
case types.DegreeFahrenheit:
case types.TemperatureUnitFahrenheit:
return (value-273.15)*9/5 + 32
// Kevin -> Kelvin
case types.DegreeKelvin:
case types.TemperatureUnitKelvin:
return value
}
}
@ -146,14 +146,14 @@ func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, t
return value
}
func SelectTemperatureMeasurementUnit(unit string) (types.Degree, error) {
func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
switch unit {
case "celsius":
return types.DegreeCelsius, nil
return types.TemperatureUnitCelsius, nil
case "fahrenheit":
return types.DegreeFahrenheit, nil
return types.TemperatureUnitFahrenheit, nil
case "kelvin":
return types.DegreeKelvin, nil
return types.TemperatureUnitKelvin, nil
default:
return "", fmt.Errorf("Can not determine temperature measurement unit")
}