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

View File

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

View File

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

View File

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

View File

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

View File

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

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