From 3bb10a4f78d86a82020fa86f53e71e7ce3a7203d Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 23 Jun 2019 13:33:09 +0200 Subject: [PATCH] fix(pkg/sensor): temperature measurement unit --- cmd/temperature/read.go | 3 +- pkg/daemon/daemon.go | 2 +- pkg/logfile/csv.go | 29 +++++--- pkg/logfile/errors.go | 1 + pkg/logfile/logfile_test.go | 4 +- pkg/logfile/test/csv/validTemperatures_01.csv | 4 +- pkg/logfile/test/csv/validTemperatures_02.csv | 4 +- .../test/json/faultyTemperatures_01.json | 1 + .../test/json/faultyTemperatures_02.json | 1 + .../test/json/faultyTemperatures_03.json | 1 + .../test/json/faultyTemperatures_04.json | 1 + .../test/json/faultyTemperatures_05.json | 1 + .../test/json/validTemperatures_01.json | 2 + .../test/json/validTemperatures_02.json | 2 + pkg/logfile/test/xml/validTemperatures_01.xml | 2 + pkg/logfile/test/xml/validTemperatures_02.xml | 2 + pkg/sensor/dht11.go | 14 ++-- pkg/sensor/dht22.go | 14 ++-- pkg/sensor/ds18b20.go | 14 ++-- pkg/sensor/interfaces.go | 6 +- pkg/sensor/sensor.go | 73 +++++++++++++++++-- pkg/types/temperature.go | 15 ++++ 22 files changed, 154 insertions(+), 42 deletions(-) diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index dc95719..07ff983 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -9,6 +9,7 @@ import ( "github.com/go-flucky/flucky/pkg/config" "github.com/go-flucky/flucky/pkg/logfile" "github.com/go-flucky/flucky/pkg/sensor" + "github.com/go-flucky/flucky/pkg/types" "github.com/spf13/cobra" ) @@ -34,7 +35,7 @@ var readTemperatureCmd = &cobra.Command{ temperatureSensors = cnf.GetTemperatureSensorsByName(args) } - temperatures, err := sensor.ReadTemperatures(temperatureSensors, round) + temperatures, err := sensor.ReadTemperatures(temperatureSensors, types.DegreeCelsius, round) if err != nil { log.Fatalln(err) } diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 8691d65..c481db8 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -38,7 +38,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress childContext, cancel := context.WithCancel(ctx) // go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel) - go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), round, temperatureChannel, errorChannel) + go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), types.DegreeCelsius, round, temperatureChannel, errorChannel) temperatureCache := make([]*types.Temperature, 0) diff --git a/pkg/logfile/csv.go b/pkg/logfile/csv.go index 9fa27b5..99937d5 100644 --- a/pkg/logfile/csv.go +++ b/pkg/logfile/csv.go @@ -8,6 +8,7 @@ import ( "strconv" "time" + "github.com/go-flucky/flucky/pkg/sensor" "github.com/go-flucky/flucky/pkg/types" ) @@ -63,7 +64,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) { for _, record := range records { times := make([]time.Time, 0) - for _, j := range []int{2, 3} { + for _, j := range []int{3, 4} { time, err := time.Parse(timeFormat, record[j]) if err != nil { return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err) @@ -76,25 +77,31 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) { return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err) } + measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2]) + if err != nil { + return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err) + } + temperature := &types.Temperature{ - TemperatureID: record[0], - TemperatureValue: temperatureValue, - TemperatureFromDate: times[0], - TemperatureTillDate: times[1], - SensorID: record[4], + TemperatureID: record[0], // 0 + TemperatureValue: temperatureValue, // 1 + TemperatureDegree: measurementUnit, // 2 + TemperatureFromDate: times[0], // 3 + TemperatureTillDate: times[1], // 4 + SensorID: record[5], // 5 } // Creation date - temperatureCreationDate, err := time.Parse(timeFormat, record[5]) + temperatureCreationDate, err := time.Parse(timeFormat, record[6]) if err != nil { return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err) } temperature.CreationDate = &temperatureCreationDate - if record[6] != "" { - temperatureUpdateDate, err := time.Parse(timeFormat, record[6]) + if record[7] != "" { + temperatureUpdateDate, err := time.Parse(timeFormat, record[7]) if err != nil { - return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err) + return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err) } temperature.UpdateDate = &temperatureUpdateDate @@ -151,6 +158,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error record = []string{ fmt.Sprintf("%v", temperature.TemperatureID), fmt.Sprintf("%v", temperature.TemperatureValue), + fmt.Sprintf("%v", temperature.TemperatureDegree), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.SensorID), @@ -161,6 +169,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error record = []string{ fmt.Sprintf("%v", temperature.TemperatureID), fmt.Sprintf("%v", temperature.TemperatureValue), + fmt.Sprintf("%v", temperature.TemperatureDegree), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.SensorID), diff --git a/pkg/logfile/errors.go b/pkg/logfile/errors.go index 9f17e66..9b7c86f 100644 --- a/pkg/logfile/errors.go +++ b/pkg/logfile/errors.go @@ -13,6 +13,7 @@ var errorLogfileUnmarshal = errors.New("Can not unmarshal values") var errorLogfileWrite = errors.New("Can not write with given writer") var errorParseFloat = errors.New("Can not parse float") +var errorParseMeasurementUnit = errors.New("Can not parse mesaurement unit") var errorParseTime = errors.New("Can not parse time") var errorNoValidHumidityID = errors.New("No valid humidity id detected or available") diff --git a/pkg/logfile/logfile_test.go b/pkg/logfile/logfile_test.go index 4a3ed9f..5ac459a 100644 --- a/pkg/logfile/logfile_test.go +++ b/pkg/logfile/logfile_test.go @@ -27,8 +27,8 @@ func TestConvert(t *testing.T) { markupLanguages := []string{"csv", "json", "xml"} for _, markupLanguageFrom := range markupLanguages { - testDir, cleanup := createTestDir(t) - defer cleanup() + testDir, _ := createTestDir(t) + //defer cleanup() for _, markupLanguageTo := range markupLanguages { diff --git a/pkg/logfile/test/csv/validTemperatures_01.csv b/pkg/logfile/test/csv/validTemperatures_01.csv index e7ee05d..da2c99d 100644 --- a/pkg/logfile/test/csv/validTemperatures_01.csv +++ b/pkg/logfile/test/csv/validTemperatures_01.csv @@ -1,2 +1,2 @@ -a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200 -5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:18:07.463893776 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587701 +0200 +a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,celsius,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200 +5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,celsius,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:18:07.463893776 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587701 +0200 diff --git a/pkg/logfile/test/csv/validTemperatures_02.csv b/pkg/logfile/test/csv/validTemperatures_02.csv index e92fd04..5a4af88 100644 --- a/pkg/logfile/test/csv/validTemperatures_02.csv +++ b/pkg/logfile/test/csv/validTemperatures_02.csv @@ -1,2 +1,2 @@ -a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200 -5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:15:28.583856443 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200, +a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,celsius,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200 +5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,celsius,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:15:28.583856443 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200, diff --git a/pkg/logfile/test/json/faultyTemperatures_01.json b/pkg/logfile/test/json/faultyTemperatures_01.json index b238aa8..9e027c8 100644 --- a/pkg/logfile/test/json/faultyTemperatures_01.json +++ b/pkg/logfile/test/json/faultyTemperatures_01.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-10-01T00:00:00+02:00", "temperature_till_date": "2019-05-01T00:00:00+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", diff --git a/pkg/logfile/test/json/faultyTemperatures_02.json b/pkg/logfile/test/json/faultyTemperatures_02.json index 05c8336..21f0044 100644 --- a/pkg/logfile/test/json/faultyTemperatures_02.json +++ b/pkg/logfile/test/json/faultyTemperatures_02.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", diff --git a/pkg/logfile/test/json/faultyTemperatures_03.json b/pkg/logfile/test/json/faultyTemperatures_03.json index 0fa6e01..a95ace1 100644 --- a/pkg/logfile/test/json/faultyTemperatures_03.json +++ b/pkg/logfile/test/json/faultyTemperatures_03.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00", "sensor_id": "", diff --git a/pkg/logfile/test/json/faultyTemperatures_04.json b/pkg/logfile/test/json/faultyTemperatures_04.json index 87d28a0..56e1e6d 100644 --- a/pkg/logfile/test/json/faultyTemperatures_04.json +++ b/pkg/logfile/test/json/faultyTemperatures_04.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "0", + "temperature_degree": "celsius", "temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", diff --git a/pkg/logfile/test/json/faultyTemperatures_05.json b/pkg/logfile/test/json/faultyTemperatures_05.json index eae9d02..0db351a 100644 --- a/pkg/logfile/test/json/faultyTemperatures_05.json +++ b/pkg/logfile/test/json/faultyTemperatures_05.json @@ -2,6 +2,7 @@ { "temperature_id": "", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", diff --git a/pkg/logfile/test/json/validTemperatures_01.json b/pkg/logfile/test/json/validTemperatures_01.json index 633715e..438f185 100644 --- a/pkg/logfile/test/json/validTemperatures_01.json +++ b/pkg/logfile/test/json/validTemperatures_01.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-06-14T21:15:28.504051541+02:00", "temperature_till_date": "2019-06-14T21:18:07.384104493+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", @@ -11,6 +12,7 @@ { "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_value": "24.375", + "temperature_degree": "celsius", "temperature_from_date": "2019-06-14T21:15:28.583856443+02:00", "temperature_till_date": "2019-06-14T21:18:07.463893776+02:00", "sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8", diff --git a/pkg/logfile/test/json/validTemperatures_02.json b/pkg/logfile/test/json/validTemperatures_02.json index 8014710..6dd1354 100644 --- a/pkg/logfile/test/json/validTemperatures_02.json +++ b/pkg/logfile/test/json/validTemperatures_02.json @@ -2,6 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", + "temperature_degree": "celsius", "temperature_from_date": "2019-06-14T21:15:28.504051541+02:00", "temperature_till_date": "2019-06-14T21:18:07.384104493+02:00", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", @@ -11,6 +12,7 @@ { "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_value": "24.375", + "temperature_degree": "celsius", "temperature_from_date": "2019-06-14T21:15:28.583856443+02:00", "temperature_till_date": "2019-06-14T21:15:28.583856443+02:00", "sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8", diff --git a/pkg/logfile/test/xml/validTemperatures_01.xml b/pkg/logfile/test/xml/validTemperatures_01.xml index 3cfa0bb..ff2df53 100644 --- a/pkg/logfile/test/xml/validTemperatures_01.xml +++ b/pkg/logfile/test/xml/validTemperatures_01.xml @@ -2,6 +2,7 @@ a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 + celsius 2019-06-14T21:15:28.504051541+02:00 2019-06-14T21:18:07.384104493+02:00 84eac248-6927-4db6-b6f9-7891ce2d301e @@ -11,6 +12,7 @@ 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 + celsius 2019-06-14T21:15:28.583856443+02:00 2019-06-14T21:18:07.463893776+02:00 efcd755e-82d1-4789-a50b-355b8735b8d8 diff --git a/pkg/logfile/test/xml/validTemperatures_02.xml b/pkg/logfile/test/xml/validTemperatures_02.xml index 66cb6bb..5fe6f5c 100644 --- a/pkg/logfile/test/xml/validTemperatures_02.xml +++ b/pkg/logfile/test/xml/validTemperatures_02.xml @@ -2,6 +2,7 @@ a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 + celsius 2019-06-14T21:15:28.504051541+02:00 2019-06-14T21:18:07.384104493+02:00 84eac248-6927-4db6-b6f9-7891ce2d301e @@ -11,6 +12,7 @@ 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 + celsius 2019-06-14T21:15:28.583856443+02:00 2019-06-14T21:15:28.583856443+02:00 efcd755e-82d1-4789-a50b-355b8735b8d8 diff --git a/pkg/sensor/dht11.go b/pkg/sensor/dht11.go index 3d178f4..9fc10af 100644 --- a/pkg/sensor/dht11.go +++ b/pkg/sensor/dht11.go @@ -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) } } } diff --git a/pkg/sensor/dht22.go b/pkg/sensor/dht22.go index 0b04c96..1f9532c 100644 --- a/pkg/sensor/dht22.go +++ b/pkg/sensor/dht22.go @@ -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) } } } diff --git a/pkg/sensor/ds18b20.go b/pkg/sensor/ds18b20.go index a9a71a1..5b4330b 100644 --- a/pkg/sensor/ds18b20.go +++ b/pkg/sensor/ds18b20.go @@ -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) } } } diff --git a/pkg/sensor/interfaces.go b/pkg/sensor/interfaces.go index 2f96c5e..bdb358d 100644 --- a/pkg/sensor/interfaces.go +++ b/pkg/sensor/interfaces.go @@ -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) } diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index be49ad4..b663006 100644 --- a/pkg/sensor/sensor.go +++ b/pkg/sensor/sensor.go @@ -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") + } +} diff --git a/pkg/types/temperature.go b/pkg/types/temperature.go index 3b0f0dd..88b4864 100644 --- a/pkg/types/temperature.go +++ b/pkg/types/temperature.go @@ -8,9 +8,24 @@ import ( type Temperature struct { TemperatureID string `json:"temperature_id" xml:"temperature_id"` TemperatureValue float64 `json:"temperature_value,string" xml:"temperature_value,string"` + TemperatureDegree Degree `json:"temperature_degree" xml:"temperature_degree"` TemperatureFromDate time.Time `json:"temperature_from_date" xml:"temperature_from_date"` TemperatureTillDate time.Time `json:"temperature_till_date" xml:"temperature_till_date"` SensorID string `json:"sensor_id" xml:"sensor_id"` CreationDate *time.Time `json:"creation_date" xml:"creation_date"` UpdateDate *time.Time `json:"update_date" xml:"update_date"` } + +// Degree unit of measurement for temperature +type Degree string + +const ( + // DegreeCelsius indicates the temperature in Celsius + DegreeCelsius Degree = "celsius" + + // DegreeFahrenheit indicates the temperature in Fahrenheit + DegreeFahrenheit = "fahrenheit" + + // DegreeKelvin indicates the temperature in Kelvin + DegreeKelvin = "kelvin" +)