From 7a3b3c6218335af8e2fc0dfda0043db9220ee303 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 15 Jun 2019 11:49:46 +0200 Subject: [PATCH] fix(pkg/config): option to select all, enable or disable temperature sensors --- cmd/temperature/read.go | 2 +- pkg/config/flucky.go | 33 ++++++++++++++++++++++++++------- pkg/config/option.go | 14 ++++++++++++++ pkg/logfile/logfile.go | 4 ---- pkg/sensor/dht11.go | 5 +++++ pkg/sensor/dht22.go | 5 +++++ pkg/sensor/ds18b20.go | 5 +++++ pkg/sensor/interfaces.go | 2 ++ 8 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 pkg/config/option.go diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 150a182..8599b78 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -28,7 +28,7 @@ var readTemperatureCmd = &cobra.Command{ } // fetch all temperature sensors - temperatureSensors := cnf.GetTemperatureSensors() + temperatureSensors := cnf.GetTemperatureSensors(config.ENABLED) // read temperature from sensors temperatures, err := sensor.ReadTemperatures(temperatureSensors) diff --git a/pkg/config/flucky.go b/pkg/config/flucky.go index dc31dee..2dee399 100644 --- a/pkg/config/flucky.go +++ b/pkg/config/flucky.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "time" @@ -119,15 +118,35 @@ func (c *Configuration) EnableSensor(name string) error { } // GetHumiditySensors returns a list of humidity sensors -func (c *Configuration) GetHumiditySensors() []sensor.HumiditySensor{ - humiditySensors, _ := c.splitSensors() - return humiditySensors +func (c *Configuration) GetHumiditySensors() []sensor.HumiditySensor { + humiditySensors, _ := c.splitSensors() + return humiditySensors } // GetTemperatureSensors returns a list of humidity sensors -func (c *Configuration) GetTemperatureSensors() []sensor.TemperatureSensor{ - _, temperatureSensors := c.splitSensors() - return temperatureSensors +func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor { + _, temperatureSensors := c.splitSensors() + + switch option { + case ENABLED: + for i, temperatureSensor := range temperatureSensors { + if !temperatureSensor.GetEnabled() { + temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) + } + } + return temperatureSensors + case DISABLED: + for i, temperatureSensor := range temperatureSensors { + if temperatureSensor.GetEnabled() { + temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) + } + } + return temperatureSensors + case ALL: + return temperatureSensors + default: + return temperatureSensors + } } // RemoveSensor deletes a sensor by its name or its unique UUID diff --git a/pkg/config/option.go b/pkg/config/option.go new file mode 100644 index 0000000..3f465f6 --- /dev/null +++ b/pkg/config/option.go @@ -0,0 +1,14 @@ +package config + +type Option int + +const ( + // ALL specified enabled and disabled items + ALL Option = iota + 1 + + // ENABLED items + ENABLED + + // DISABLED items + DISABLED +) diff --git a/pkg/logfile/logfile.go b/pkg/logfile/logfile.go index 3c327b3..064bd4b 100644 --- a/pkg/logfile/logfile.go +++ b/pkg/logfile/logfile.go @@ -48,10 +48,6 @@ func CompressTemperature(temperatures []*types.Temperature) []*types.Temperature // Copy all remaining entries from the map into the array for _, lastTemperatureBySensor := range lastTemperatureBySensors { - if lastTemperatureBySensor.UpdateDate == nil { - now := time.Now() - lastTemperatureBySensor.UpdateDate = &now - } compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensor) } diff --git a/pkg/sensor/dht11.go b/pkg/sensor/dht11.go index 945f2e2..bb984bc 100644 --- a/pkg/sensor/dht11.go +++ b/pkg/sensor/dht11.go @@ -14,6 +14,11 @@ type DHT11 struct { *types.Sensor } +// GetEnabled returns a boolen if the sensor is enabled or disabled +func (s *DHT11) GetEnabled() bool { + return s.SensorEnabled +} + // GetSensorModel returns the sensor model func (s *DHT11) GetSensorModel() types.SensorModel { return s.Sensor.SensorModel diff --git a/pkg/sensor/dht22.go b/pkg/sensor/dht22.go index 1740ee1..5f542de 100644 --- a/pkg/sensor/dht22.go +++ b/pkg/sensor/dht22.go @@ -14,6 +14,11 @@ type DHT22 struct { *types.Sensor } +// GetEnabled returns a boolen if the sensor is enabled or disabled +func (s *DHT22) GetEnabled() bool { + return s.SensorEnabled +} + // GetSensorModel returns the sensor model func (s *DHT22) GetSensorModel() types.SensorModel { return s.Sensor.SensorModel diff --git a/pkg/sensor/ds18b20.go b/pkg/sensor/ds18b20.go index 4aaae16..d73200b 100644 --- a/pkg/sensor/ds18b20.go +++ b/pkg/sensor/ds18b20.go @@ -14,6 +14,11 @@ type DS18B20 struct { *types.Sensor } +// GetEnabled returns a boolen if the sensor is enabled or disabled +func (s *DS18B20) GetEnabled() bool { + return s.SensorEnabled +} + // GetSensorModel returns the sensor model func (s *DS18B20) GetSensorModel() types.SensorModel { return s.Sensor.SensorModel diff --git a/pkg/sensor/interfaces.go b/pkg/sensor/interfaces.go index 16c1f2f..533a28c 100644 --- a/pkg/sensor/interfaces.go +++ b/pkg/sensor/interfaces.go @@ -4,12 +4,14 @@ import "github.com/volker-raschek/flucky/pkg/types" // HumiditySensor is a interface to describe required functions to measure humidities type HumiditySensor interface { + GetEnabled() bool GetSensorModel() types.SensorModel ReadHumidity() (*types.Humidity, error) } // TemperatureSensor is a interface to describe required functions to measure temperatures type TemperatureSensor interface { + GetEnabled() bool GetSensorModel() types.SensorModel ReadTemperature() (*types.Temperature, error) }