fix(pkg/config): option to select all, enable or disable temperature sensors
This commit is contained in:
parent
525a250f3c
commit
7a3b3c6218
@ -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)
|
||||
|
@ -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
|
||||
|
14
pkg/config/option.go
Normal file
14
pkg/config/option.go
Normal 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
|
||||
)
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user