From 5e03d987c5714e532867d4d0ddd8fb5554668822 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 23 Jun 2019 14:17:57 +0200 Subject: [PATCH] refac(pkg/sensor): temperature measurement unit --- cmd/daemon/daemon.go | 12 +++++- cmd/temperature/read.go | 10 ++++- pkg/daemon/daemon.go | 5 ++- pkg/logfile/csv.go | 6 +-- .../test/json/faultyTemperatures_01.json | 2 +- .../test/json/faultyTemperatures_02.json | 2 +- .../test/json/faultyTemperatures_03.json | 2 +- .../test/json/faultyTemperatures_04.json | 2 +- .../test/json/faultyTemperatures_05.json | 2 +- .../test/json/validTemperatures_01.json | 4 +- .../test/json/validTemperatures_02.json | 4 +- pkg/logfile/test/xml/validTemperatures_01.xml | 4 +- pkg/logfile/test/xml/validTemperatures_02.xml | 4 +- pkg/sensor/dht11.go | 10 ++--- pkg/sensor/dht22.go | 10 ++--- pkg/sensor/ds18b20.go | 10 ++--- pkg/sensor/interfaces.go | 6 +-- pkg/sensor/sensor.go | 40 +++++++++---------- pkg/types/temperature.go | 32 +++++++-------- 19 files changed, 91 insertions(+), 76 deletions(-) diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index c98cfc7..841c5ab 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -7,13 +7,15 @@ import ( "github.com/go-flucky/flucky/pkg/config" "github.com/go-flucky/flucky/pkg/daemon" "github.com/go-flucky/flucky/pkg/logger" + "github.com/go-flucky/flucky/pkg/sensor" "github.com/spf13/cobra" ) +var cleanCacheInterval string var compression bool var configFile string -var cleanCacheInterval string var round float64 +var temperatureUnit string var daemonCmd = &cobra.Command{ Use: "daemon", @@ -32,7 +34,12 @@ var daemonCmd = &cobra.Command{ logger := logger.NewDefaultLogger(logger.LogLevelDebug, false) - daemon.Start(cnf, duration, compression, round, logger) + measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit) + if err != nil { + log.Fatalf("Can not parse temperature unit: %v", temperatureUnit) + } + + daemon.Start(cnf, duration, compression, measurementUnit, round, logger) }, } @@ -43,4 +50,5 @@ func InitCmd(cmd *cobra.Command, cnfFile string) { daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values") daemonCmd.Flags().StringVar(&cleanCacheInterval, "clean-cache-interval", "5m", "Minute intervall to clean cache and write measured values into logfile") daemonCmd.Flags().Float64Var(&round, "round", 0.25, "Round values. The value 0 deactivates the function") + daemonCmd.Flags().StringVar(&temperatureUnit, "temperature-unit", "celsius", "Temperature unit") } diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 07ff983..6f16560 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -9,11 +9,11 @@ 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" ) var logs bool +var temperatureUnit string var readTemperatureCmd = &cobra.Command{ Use: "read", @@ -35,7 +35,12 @@ var readTemperatureCmd = &cobra.Command{ temperatureSensors = cnf.GetTemperatureSensorsByName(args) } - temperatures, err := sensor.ReadTemperatures(temperatureSensors, types.DegreeCelsius, round) + measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit) + if err != nil { + log.Fatalf("Can not parse temperature unit: %v", temperatureUnit) + } + + temperatures, err := sensor.ReadTemperatures(temperatureSensors, measurementUnit, round) if err != nil { log.Fatalln(err) } @@ -58,4 +63,5 @@ func init() { readTemperatureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature") readTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures") readTemperatureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function") + readTemperatureCmd.Flags().StringVar(&temperatureUnit, "temperature-unit", "celsius", "Temperature unit") } diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index c481db8..d048bab 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -16,12 +16,13 @@ import ( ) // Start the daemon -func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) { +func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, degree types.TemperatureUnit, round float64, logger logger.Logger) { // Info logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String()) logger.Info("Use compression: %v", compression) logger.Info("Round values: %v", round) + logger.Info("Temperature unit: %v", degree) temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile) @@ -38,7 +39,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), types.DegreeCelsius, round, temperatureChannel, errorChannel) + go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), degree, round, temperatureChannel, errorChannel) temperatureCache := make([]*types.Temperature, 0) diff --git a/pkg/logfile/csv.go b/pkg/logfile/csv.go index 99937d5..a762c1f 100644 --- a/pkg/logfile/csv.go +++ b/pkg/logfile/csv.go @@ -85,7 +85,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) { temperature := &types.Temperature{ TemperatureID: record[0], // 0 TemperatureValue: temperatureValue, // 1 - TemperatureDegree: measurementUnit, // 2 + TemperatureUnit: measurementUnit, // 2 TemperatureFromDate: times[0], // 3 TemperatureTillDate: times[1], // 4 SensorID: record[5], // 5 @@ -158,7 +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.TemperatureUnit), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.SensorID), @@ -169,7 +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.TemperatureUnit), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.SensorID), diff --git a/pkg/logfile/test/json/faultyTemperatures_01.json b/pkg/logfile/test/json/faultyTemperatures_01.json index 9e027c8..8149145 100644 --- a/pkg/logfile/test/json/faultyTemperatures_01.json +++ b/pkg/logfile/test/json/faultyTemperatures_01.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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 21f0044..718fc43 100644 --- a/pkg/logfile/test/json/faultyTemperatures_02.json +++ b/pkg/logfile/test/json/faultyTemperatures_02.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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 a95ace1..0cfce29 100644 --- a/pkg/logfile/test/json/faultyTemperatures_03.json +++ b/pkg/logfile/test/json/faultyTemperatures_03.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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 56e1e6d..2730c30 100644 --- a/pkg/logfile/test/json/faultyTemperatures_04.json +++ b/pkg/logfile/test/json/faultyTemperatures_04.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "0", - "temperature_degree": "celsius", + "temperature_unit": "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 0db351a..2699251 100644 --- a/pkg/logfile/test/json/faultyTemperatures_05.json +++ b/pkg/logfile/test/json/faultyTemperatures_05.json @@ -2,7 +2,7 @@ { "temperature_id": "", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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 438f185..99655b5 100644 --- a/pkg/logfile/test/json/validTemperatures_01.json +++ b/pkg/logfile/test/json/validTemperatures_01.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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", @@ -12,7 +12,7 @@ { "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_value": "24.375", - "temperature_degree": "celsius", + "temperature_unit": "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 6dd1354..9955304 100644 --- a/pkg/logfile/test/json/validTemperatures_02.json +++ b/pkg/logfile/test/json/validTemperatures_02.json @@ -2,7 +2,7 @@ { "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_value": "24.562", - "temperature_degree": "celsius", + "temperature_unit": "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", @@ -12,7 +12,7 @@ { "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_value": "24.375", - "temperature_degree": "celsius", + "temperature_unit": "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 ff2df53..205c034 100644 --- a/pkg/logfile/test/xml/validTemperatures_01.xml +++ b/pkg/logfile/test/xml/validTemperatures_01.xml @@ -2,7 +2,7 @@ a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 - celsius + celsius 2019-06-14T21:15:28.504051541+02:00 2019-06-14T21:18:07.384104493+02:00 84eac248-6927-4db6-b6f9-7891ce2d301e @@ -12,7 +12,7 @@ 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 - celsius + 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 5fe6f5c..6ac6089 100644 --- a/pkg/logfile/test/xml/validTemperatures_02.xml +++ b/pkg/logfile/test/xml/validTemperatures_02.xml @@ -2,7 +2,7 @@ a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 - celsius + celsius 2019-06-14T21:15:28.504051541+02:00 2019-06-14T21:18:07.384104493+02:00 84eac248-6927-4db6-b6f9-7891ce2d301e @@ -12,7 +12,7 @@ 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 - celsius + 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 9fc10af..8792e98 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(degree types.Degree, round float64) (*types.Temperature, error) { +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) @@ -109,7 +109,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp } // Convert temperature degree - temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) + temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree) if round != 0 { temperatureValue = math.Round(temperatureValue/round) * round @@ -118,7 +118,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp temperature := &types.Temperature{ TemperatureID: uuid.NewV4().String(), TemperatureValue: temperatureValue, - TemperatureDegree: degree, + TemperatureUnit: degree, TemperatureFromDate: time.Now(), TemperatureTillDate: time.Now(), SensorID: s.SensorID, @@ -128,7 +128,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp } // ReadTemperatureWriteIntoChannel and write values into a channel -func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { +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() } @@ -142,7 +142,7 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float } // ReadTemperatureContinously into a channel until context closed -func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { +func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { for { select { case <-ctx.Done(): diff --git a/pkg/sensor/dht22.go b/pkg/sensor/dht22.go index 1f9532c..56e95b5 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(degree types.Degree, round float64) (*types.Temperature, error) { +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) @@ -109,7 +109,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp } // Convert temperature degree - temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) + temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree) // round if round != 0 { @@ -119,7 +119,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp temperature := &types.Temperature{ TemperatureID: uuid.NewV4().String(), TemperatureValue: temperatureValue, - TemperatureDegree: degree, + TemperatureUnit: degree, TemperatureFromDate: time.Now(), TemperatureTillDate: time.Now(), SensorID: s.SensorID, @@ -129,7 +129,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp } // ReadTemperatureWriteIntoChannel and write values into a channel -func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { +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() } @@ -143,7 +143,7 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float } // ReadTemperatureContinously into a channel until context closed -func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { +func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { for { select { case <-ctx.Done(): diff --git a/pkg/sensor/ds18b20.go b/pkg/sensor/ds18b20.go index 5b4330b..b84b8d4 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(degree types.Degree, round float64) (*types.Temperature, error) { +func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) { data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave")) if err != nil { @@ -53,7 +53,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te temperatureValue := c / 1000 // Convert temperature degree - temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) + temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree) // round if round != 0 { @@ -63,7 +63,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te temperature := &types.Temperature{ TemperatureID: uuid.NewV4().String(), TemperatureValue: temperatureValue, - TemperatureDegree: degree, + TemperatureUnit: degree, TemperatureFromDate: time.Now(), TemperatureTillDate: time.Now(), SensorID: s.SensorID, @@ -74,7 +74,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te } // ReadTemperatureWriteIntoChannel and write values into a channel -func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { +func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { if wg != nil { defer wg.Done() } @@ -88,7 +88,7 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round flo } // ReadTemperatureContinously into a channel until context closed -func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { +func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { for { select { case <-ctx.Done(): diff --git a/pkg/sensor/interfaces.go b/pkg/sensor/interfaces.go index bdb358d..9f2adaa 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(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) + 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) } diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index b663006..4df1795 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, 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") } diff --git a/pkg/types/temperature.go b/pkg/types/temperature.go index 88b4864..34013b1 100644 --- a/pkg/types/temperature.go +++ b/pkg/types/temperature.go @@ -6,26 +6,26 @@ import ( // Temperature ... 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"` + TemperatureID string `json:"temperature_id" xml:"temperature_id"` + TemperatureValue float64 `json:"temperature_value,string" xml:"temperature_value,string"` + TemperatureUnit TemperatureUnit `json:"temperature_unit" xml:"temperature_unit"` + 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 +// TemperatureUnit of measurement for temperature +type TemperatureUnit string const ( - // DegreeCelsius indicates the temperature in Celsius - DegreeCelsius Degree = "celsius" + // TemperatureUnitCelsius indicates the temperature in Celsius + TemperatureUnitCelsius TemperatureUnit = "celsius" - // DegreeFahrenheit indicates the temperature in Fahrenheit - DegreeFahrenheit = "fahrenheit" + // TemperatureUnitFahrenheit indicates the temperature in Fahrenheit + TemperatureUnitFahrenheit = "fahrenheit" - // DegreeKelvin indicates the temperature in Kelvin - DegreeKelvin = "kelvin" + // TemperatureUnitKelvin indicates the temperature in Kelvin + TemperatureUnitKelvin = "kelvin" )