fix(pkg/config): converter for humidity and temperature sensors

This commit is contained in:
Markus Pesch 2019-06-15 13:56:41 +02:00
parent 662e1dcfa9
commit 2843d96756
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
7 changed files with 50 additions and 39 deletions

View File

@ -10,9 +10,9 @@ import (
) )
var listSensorCmd = &cobra.Command{ var listSensorCmd = &cobra.Command{
Use: "ls", Use: "list",
Short: "List Sensors", Short: "List Sensors",
Aliases: []string{"list"}, Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// read configuration // read configuration
fc, err := config.Read(cfg) fc, err := config.Read(cfg)

View File

@ -8,10 +8,10 @@ import (
) )
var rmSensorCmd = &cobra.Command{ var rmSensorCmd = &cobra.Command{
Use: "rm", Use: "remove",
Short: "Remove Sensor", Short: "Remove Sensor",
Example: "flucky sensor rm outdoor", Example: "flucky sensor rm outdoor",
Aliases: []string{"remove"}, Aliases: []string{"rm"},
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// read configuration // read configuration

View File

@ -11,6 +11,17 @@ import (
"github.com/volker-raschek/flucky/pkg/types" "github.com/volker-raschek/flucky/pkg/types"
) )
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
types.DHT11: types.DHT11,
types.DHT22: types.DHT22,
}
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
types.DHT11: types.DHT11,
types.DHT22: types.DHT22,
types.DS18B20: types.DS18B20,
}
// Configuration of flucky // Configuration of flucky
type Configuration struct { type Configuration struct {
Device *types.Device `json:"device"` Device *types.Device `json:"device"`
@ -119,27 +130,25 @@ func (c *Configuration) EnableSensor(name string) error {
// GetHumiditySensors returns a list of humidity sensors // GetHumiditySensors returns a list of humidity sensors
func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySensor { func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySensor {
humiditySensors := c.convertHumiditySensors(c.Sensors) humiditySensors := c.getHumiditySensors()
switch option { switch option {
case ENABLED: case ENABLED:
for i, humiditySensor := range humiditySensors { for i, humiditySensor := range humiditySensors {
if !humiditySensor.GetEnabled() { if !humiditySensor.SensorEnabled {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...) humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
} }
} }
return humiditySensors return c.convertHumiditySensors(humiditySensors)
case DISABLED: case DISABLED:
for i, humiditySensor := range humiditySensors { for i, humiditySensor := range humiditySensors {
if humiditySensor.GetEnabled() { if humiditySensor.SensorEnabled {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...) humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
} }
} }
return humiditySensors return c.convertHumiditySensors(humiditySensors)
case ALL:
return humiditySensors
default: default:
return humiditySensors return c.convertHumiditySensors(humiditySensors)
} }
} }
@ -149,7 +158,7 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Humidi
configHumiditySensors := make(map[string]*types.Sensor, 0) configHumiditySensors := make(map[string]*types.Sensor, 0)
for _, name := range names { for _, name := range names {
for _, s := range c.Sensors { for _, s := range c.getHumiditySensors() {
switch name { switch name {
case s.SensorID: case s.SensorID:
configHumiditySensors[s.SensorID] = s configHumiditySensors[s.SensorID] = s
@ -171,27 +180,25 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Humidi
// GetTemperatureSensors returns a list of temperature sensors // GetTemperatureSensors returns a list of temperature sensors
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor { func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor {
temperatureSensors := c.convertTemperatureSensors(c.Sensors) temperatureSensors := c.getTemperatureSensors()
switch option { switch option {
case ENABLED: case ENABLED:
for i, temperatureSensor := range temperatureSensors { for i, temperatureSensor := range temperatureSensors {
if !temperatureSensor.GetEnabled() { if !temperatureSensor.SensorEnabled {
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
} }
} }
return temperatureSensors return c.convertTemperatureSensors(temperatureSensors)
case DISABLED: case DISABLED:
for i, temperatureSensor := range temperatureSensors { for i, temperatureSensor := range temperatureSensors {
if temperatureSensor.GetEnabled() { if temperatureSensor.SensorEnabled {
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
} }
} }
return temperatureSensors return c.convertTemperatureSensors(temperatureSensors)
case ALL:
return temperatureSensors
default: default:
return temperatureSensors return c.convertTemperatureSensors(temperatureSensors)
} }
} }
@ -201,7 +208,7 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Tem
configTemperatureSensors := make(map[string]*types.Sensor, 0) configTemperatureSensors := make(map[string]*types.Sensor, 0)
for _, name := range names { for _, name := range names {
for _, s := range c.Sensors { for _, s := range c.getTemperatureSensors() {
switch name { switch name {
case s.SensorID: case s.SensorID:
configTemperatureSensors[s.SensorID] = s configTemperatureSensors[s.SensorID] = s
@ -272,6 +279,7 @@ func (c *Configuration) convertHumiditySensors(sensors []*types.Sensor) []sensor
func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor { func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor {
temperatureSensors := make([]sensor.TemperatureSensor, 0) temperatureSensors := make([]sensor.TemperatureSensor, 0)
for _, s := range sensors { for _, s := range sensors {
switch s.SensorModel { switch s.SensorModel {
case types.DHT11: case types.DHT11:
@ -290,3 +298,23 @@ func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sen
} }
return temperatureSensors return temperatureSensors
} }
func (c *Configuration) getHumiditySensors() []*types.Sensor {
humiditySensors := make([]*types.Sensor, 0)
for _, s := range c.Sensors {
if _, ok := humiditySensorModels[s.SensorModel]; ok {
humiditySensors = append(humiditySensors, s)
}
}
return humiditySensors
}
func (c *Configuration) getTemperatureSensors() []*types.Sensor {
temperatureSensors := make([]*types.Sensor, 0)
for _, s := range c.Sensors {
if _, ok := temperatureSensorModels[s.SensorModel]; ok {
temperatureSensors = append(temperatureSensors, s)
}
}
return temperatureSensors
}

View File

@ -14,11 +14,6 @@ type DHT11 struct {
*types.Sensor *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 // GetSensorModel returns the sensor model
func (s *DHT11) GetSensorModel() types.SensorModel { func (s *DHT11) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel return s.Sensor.SensorModel

View File

@ -14,11 +14,6 @@ type DHT22 struct {
*types.Sensor *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 // GetSensorModel returns the sensor model
func (s *DHT22) GetSensorModel() types.SensorModel { func (s *DHT22) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel return s.Sensor.SensorModel

View File

@ -14,11 +14,6 @@ type DS18B20 struct {
*types.Sensor *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 // GetSensorModel returns the sensor model
func (s *DS18B20) GetSensorModel() types.SensorModel { func (s *DS18B20) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel return s.Sensor.SensorModel

View File

@ -4,14 +4,12 @@ import "github.com/volker-raschek/flucky/pkg/types"
// HumiditySensor is a interface to describe required functions to measure humidities // HumiditySensor is a interface to describe required functions to measure humidities
type HumiditySensor interface { type HumiditySensor interface {
GetEnabled() bool
GetSensorModel() types.SensorModel GetSensorModel() types.SensorModel
ReadHumidity() (*types.Humidity, error) ReadHumidity() (*types.Humidity, error)
} }
// TemperatureSensor is a interface to describe required functions to measure temperatures // TemperatureSensor is a interface to describe required functions to measure temperatures
type TemperatureSensor interface { type TemperatureSensor interface {
GetEnabled() bool
GetSensorModel() types.SensorModel GetSensorModel() types.SensorModel
ReadTemperature() (*types.Temperature, error) ReadTemperature() (*types.Temperature, error)
} }