From 93c08d81476b459315e302640a0ca3600fc23b99 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Tue, 2 Jul 2019 22:40:00 +0200 Subject: [PATCH] fix(pkg/config): add functions to get pressure sensors --- pkg/config/flucky.go | 68 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/pkg/config/flucky.go b/pkg/config/flucky.go index ee7fb68..27ef1f7 100644 --- a/pkg/config/flucky.go +++ b/pkg/config/flucky.go @@ -13,16 +13,20 @@ import ( ) var humiditySensorModels = map[types.SensorModel]types.SensorModel{ + types.BME280: types.BME280, types.DHT11: types.DHT11, types.DHT22: types.DHT22, +} + +var pressureSensorModels = map[types.SensorModel]types.SensorModel{ types.BME280: types.BME280, } var temperatureSensorModels = map[types.SensorModel]types.SensorModel{ + types.BME280: types.BME280, types.DHT11: types.DHT11, types.DHT22: types.DHT22, types.DS18B20: types.DS18B20, - types.BME280: types.BME280, } // Configuration of flucky @@ -253,7 +257,7 @@ func (c *Configuration) GetHumiditySensors(option Option) []sensor.Sensor { } } -// GetHumiditySensorsByName returns a list of temperature sensors by name, +// GetHumiditySensorsByName returns a list of humidity sensors by name, // uuid or wire-id func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor { configHumiditySensors := make(map[string]*types.Sensor, 0) @@ -277,6 +281,56 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor return c.convertSensors(humiditySensors) } +// GetPressureSensors returns a list of pressure sensors +func (c *Configuration) GetPressureSensors(option Option) []sensor.Sensor { + sensors := c.getPressureSensors() + + cachedSensors := make([]*types.Sensor, 0) + + switch option { + case ENABLED: + for _, sensor := range sensors { + if sensor.SensorEnabled { + cachedSensors = append(cachedSensors, sensor) + } + } + return c.convertSensors(cachedSensors) + case DISABLED: + for _, sensor := range sensors { + if !sensor.SensorEnabled { + cachedSensors = append(cachedSensors, sensor) + } + } + return c.convertSensors(cachedSensors) + default: + return c.convertSensors(cachedSensors) + } +} + +// GetPressureSensorsByName returns a list of pressure sensors by name, +// uuid or wire-id +func (c *Configuration) GetPressureSensorsByName(names []string) []sensor.Sensor { + configPressureSensors := make(map[string]*types.Sensor, 0) + + for _, name := range names { + for _, s := range c.getPressureSensors() { + switch name { + case s.SensorID: + configPressureSensors[s.SensorID] = s + case s.SensorName: + configPressureSensors[s.SensorID] = s + } + } + } + + pressureSensors := make([]*types.Sensor, 0) + for _, cs := range configPressureSensors { + pressureSensors = append(pressureSensors, cs) + } + + return c.convertSensors(pressureSensors) +} + func (c *Configuration) GetRGBLEDs(option Option) []rgbled.RGBLED { rgbLEDs := c.RGBLEDs @@ -506,6 +560,16 @@ func (c *Configuration) getHumiditySensors() []*types.Sensor { return humiditySensors } +func (c *Configuration) getPressureSensors() []*types.Sensor { + pressureSensors := make([]*types.Sensor, 0) + for _, s := range c.Sensors { + if _, ok := pressureSensorModels[s.SensorModel]; ok { + pressureSensors = append(pressureSensors, s) + } + } + return pressureSensors +} + func (c *Configuration) getTemperatureSensors() []*types.Sensor { temperatureSensors := make([]*types.Sensor, 0) for _, s := range c.Sensors {