fix(pkg/sensor): temperature measurement unit

This commit is contained in:
2019-06-23 13:33:09 +02:00
parent 1e25b55789
commit 3bb10a4f78
22 changed files with 154 additions and 42 deletions

View File

@ -87,7 +87,7 @@ func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humi
}
// ReadTemperature measure the temperature
func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -108,6 +108,9 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
}
@ -115,6 +118,7 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -124,12 +128,12 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT11) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -138,14 +142,14 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(round float64, temperatureChanne
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.Degree, 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(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -87,7 +87,7 @@ func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humi
}
// ReadTemperature measure the temperature
func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -108,6 +108,9 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
@ -116,6 +119,7 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -125,12 +129,12 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -139,14 +143,14 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChanne
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.Degree, 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(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -31,7 +31,7 @@ func (s *DS18B20) GetSensor() *types.Sensor {
}
// ReadTemperature measure the temperature
func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
if err != nil {
@ -52,6 +52,9 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
temperatureValue := c / 1000
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
@ -60,6 +63,7 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -70,12 +74,12 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DS18B20) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -84,14 +88,14 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(round float64, temperatureChan
}
// ReadTemperatureContinously into a channel until context closed
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.Degree, 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(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -18,7 +18,7 @@ type HumiditySensor interface {
// TemperatureSensor is a interface to describe required functions to measure temperatures
type TemperatureSensor interface {
GetSensorModel() types.SensorModel
ReadTemperature(round float64) (*types.Temperature, error)
ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error)
ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
}

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, round float64) ([]*types.Temperature, error) {
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.Degree, round float64) ([]*types.Temperature, error) {
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
errorChannel := make(chan error, len(temperatureSensors))
@ -63,7 +63,7 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, round float64) ([]
wg.Add(len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors {
go temperatureSensor.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, wg)
go temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
}
wg.Wait()
@ -79,21 +79,82 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, round float64) ([]
}
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
for _, temperatureSensor := range temperatureSensors {
temperatureSensor.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, wg)
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, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.Degree, 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, round, temperatureChannel, errorChannel, nil)
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, degree, round, temperatureChannel, errorChannel, nil)
}
}
}
func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, toDegree types.Degree) float64 {
switch fromDegree {
// Celsius
case types.DegreeCelsius:
switch toDegree {
// Celsius -> Celsius
case types.DegreeCelsius:
return value
// Celsius -> Fahrenheit
case types.DegreeFahrenheit:
return (value * 9 / 5) + 32
// Celsius -> Kelvin
case types.DegreeKelvin:
return value + 273.15
}
// Fahrenheit
case types.DegreeFahrenheit:
switch toDegree {
// Fahrenheit -> Celsius
case types.DegreeCelsius:
return (value - 32) * 5 / 9
// Fahrenheit -> Fahrenheit
case types.DegreeFahrenheit:
return value
// Fahrenheit -> Kelvin
case types.DegreeKelvin:
return (value-32)*5/9 + 273.15
}
case types.DegreeKelvin:
switch toDegree {
// Kelvin -> Celsius
case types.DegreeCelsius:
return value - 273.15
// Kelvin -> Fahrenheit
case types.DegreeFahrenheit:
return (value-273.15)*9/5 + 32
// Kevin -> Kelvin
case types.DegreeKelvin:
return value
}
}
return value
}
func SelectTemperatureMeasurementUnit(unit string) (types.Degree, error) {
switch unit {
case "celsius":
return types.DegreeCelsius, nil
case "fahrenheit":
return types.DegreeFahrenheit, nil
case "kelvin":
return types.DegreeKelvin, nil
default:
return "", fmt.Errorf("Can not determine temperature measurement unit")
}
}