fix(pkg/config): option to select all, enable or disable temperature sensors

This commit is contained in:
Markus Pesch 2019-06-15 11:49:46 +02:00
parent 525a250f3c
commit 7a3b3c6218
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
8 changed files with 58 additions and 12 deletions

View File

@ -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)

View File

@ -1,7 +1,6 @@
package config
import (
"fmt"
"time"
@ -125,9 +124,29 @@ func (c *Configuration) GetHumiditySensors() []sensor.HumiditySensor{
}
// GetTemperatureSensors returns a list of humidity sensors
func (c *Configuration) GetTemperatureSensors() []sensor.TemperatureSensor{
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

14
pkg/config/option.go Normal file
View File

@ -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
)

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
}