From 2843d96756cc2e0b96238cf51d7ff1a4df912d45 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 15 Jun 2019 13:56:41 +0200 Subject: [PATCH] fix(pkg/config): converter for humidity and temperature sensors --- cmd/sensor/list.go | 4 +-- cmd/sensor/{rm.go => remove.go} | 4 +-- pkg/config/flucky.go | 64 +++++++++++++++++++++++---------- pkg/sensor/dht11.go | 5 --- pkg/sensor/dht22.go | 5 --- pkg/sensor/ds18b20.go | 5 --- pkg/sensor/interfaces.go | 2 -- 7 files changed, 50 insertions(+), 39 deletions(-) rename cmd/sensor/{rm.go => remove.go} (93%) diff --git a/cmd/sensor/list.go b/cmd/sensor/list.go index 3aa9132..e3e8e2c 100644 --- a/cmd/sensor/list.go +++ b/cmd/sensor/list.go @@ -10,9 +10,9 @@ import ( ) var listSensorCmd = &cobra.Command{ - Use: "ls", + Use: "list", Short: "List Sensors", - Aliases: []string{"list"}, + Aliases: []string{"ls"}, Run: func(cmd *cobra.Command, args []string) { // read configuration fc, err := config.Read(cfg) diff --git a/cmd/sensor/rm.go b/cmd/sensor/remove.go similarity index 93% rename from cmd/sensor/rm.go rename to cmd/sensor/remove.go index 2b8dd18..d266e1a 100644 --- a/cmd/sensor/rm.go +++ b/cmd/sensor/remove.go @@ -8,10 +8,10 @@ import ( ) var rmSensorCmd = &cobra.Command{ - Use: "rm", + Use: "remove", Short: "Remove Sensor", Example: "flucky sensor rm outdoor", - Aliases: []string{"remove"}, + Aliases: []string{"rm"}, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { // read configuration diff --git a/pkg/config/flucky.go b/pkg/config/flucky.go index 27cc573..636e3d5 100644 --- a/pkg/config/flucky.go +++ b/pkg/config/flucky.go @@ -11,6 +11,17 @@ import ( "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 type Configuration struct { Device *types.Device `json:"device"` @@ -119,27 +130,25 @@ func (c *Configuration) EnableSensor(name string) error { // GetHumiditySensors returns a list of humidity sensors func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySensor { - humiditySensors := c.convertHumiditySensors(c.Sensors) + humiditySensors := c.getHumiditySensors() switch option { case ENABLED: for i, humiditySensor := range humiditySensors { - if !humiditySensor.GetEnabled() { + if !humiditySensor.SensorEnabled { humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...) } } - return humiditySensors + return c.convertHumiditySensors(humiditySensors) case DISABLED: for i, humiditySensor := range humiditySensors { - if humiditySensor.GetEnabled() { + if humiditySensor.SensorEnabled { humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...) } } - return humiditySensors - case ALL: - return humiditySensors + return c.convertHumiditySensors(humiditySensors) 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) for _, name := range names { - for _, s := range c.Sensors { + for _, s := range c.getHumiditySensors() { switch name { case s.SensorID: configHumiditySensors[s.SensorID] = s @@ -171,27 +180,25 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Humidi // GetTemperatureSensors returns a list of temperature sensors func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor { - temperatureSensors := c.convertTemperatureSensors(c.Sensors) + temperatureSensors := c.getTemperatureSensors() switch option { case ENABLED: for i, temperatureSensor := range temperatureSensors { - if !temperatureSensor.GetEnabled() { + if !temperatureSensor.SensorEnabled { temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) } } - return temperatureSensors + return c.convertTemperatureSensors(temperatureSensors) case DISABLED: for i, temperatureSensor := range temperatureSensors { - if temperatureSensor.GetEnabled() { + if temperatureSensor.SensorEnabled { temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...) } } - return temperatureSensors - case ALL: - return temperatureSensors + return c.convertTemperatureSensors(temperatureSensors) 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) for _, name := range names { - for _, s := range c.Sensors { + for _, s := range c.getTemperatureSensors() { switch name { case s.SensorID: 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 { temperatureSensors := make([]sensor.TemperatureSensor, 0) + for _, s := range sensors { switch s.SensorModel { case types.DHT11: @@ -290,3 +298,23 @@ func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sen } 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 +} diff --git a/pkg/sensor/dht11.go b/pkg/sensor/dht11.go index bb984bc..945f2e2 100644 --- a/pkg/sensor/dht11.go +++ b/pkg/sensor/dht11.go @@ -14,11 +14,6 @@ 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 5f542de..1740ee1 100644 --- a/pkg/sensor/dht22.go +++ b/pkg/sensor/dht22.go @@ -14,11 +14,6 @@ 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 d73200b..4aaae16 100644 --- a/pkg/sensor/ds18b20.go +++ b/pkg/sensor/ds18b20.go @@ -14,11 +14,6 @@ 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 533a28c..16c1f2f 100644 --- a/pkg/sensor/interfaces.go +++ b/pkg/sensor/interfaces.go @@ -4,14 +4,12 @@ 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) }