fix(pkg/config): select temperature and humidity sensors by name, uuid or wire-id

This commit is contained in:
Markus Pesch 2019-06-15 13:02:52 +02:00
parent 314f5a41e5
commit 662e1dcfa9
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 101 additions and 14 deletions

View File

@ -27,8 +27,13 @@ var readTemperatureCmd = &cobra.Command{
log.Fatalln(err)
}
// fetch all temperature sensors
temperatureSensors := cnf.GetTemperatureSensors(config.ENABLED)
// fetch all temperature sensors or sensors by args
temperatureSensors := make([]sensor.TemperatureSensor, 0)
if len(args) == 0 {
temperatureSensors = cnf.GetTemperatureSensors(config.ENABLED)
} else {
temperatureSensors = cnf.GetTemperatureSensorsByName(args)
}
// read temperature from sensors
temperatures, err := sensor.ReadTemperatures(temperatureSensors)

View File

@ -118,14 +118,60 @@ 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(option Option) []sensor.HumiditySensor {
humiditySensors := c.convertHumiditySensors(c.Sensors)
switch option {
case ENABLED:
for i, humiditySensor := range humiditySensors {
if !humiditySensor.GetEnabled() {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
}
}
return humiditySensors
case DISABLED:
for i, humiditySensor := range humiditySensors {
if humiditySensor.GetEnabled() {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
}
}
return humiditySensors
case ALL:
return humiditySensors
default:
return humiditySensors
}
}
// GetTemperatureSensors returns a list of humidity sensors
// GetHumiditySensorsByName returns a list of temperature sensors by name,
// uuid or wire-id
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.HumiditySensor {
configHumiditySensors := make(map[string]*types.Sensor, 0)
for _, name := range names {
for _, s := range c.Sensors {
switch name {
case s.SensorID:
configHumiditySensors[s.SensorID] = s
case *s.WireID:
configHumiditySensors[s.SensorID] = s
case s.SensorName:
configHumiditySensors[s.SensorID] = s
}
}
}
humiditySensors := make([]*types.Sensor, 0)
for _, cs := range configHumiditySensors {
humiditySensors = append(humiditySensors, cs)
}
return c.convertHumiditySensors(humiditySensors)
}
// GetTemperatureSensors returns a list of temperature sensors
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor {
_, temperatureSensors := c.splitSensors()
temperatureSensors := c.convertTemperatureSensors(c.Sensors)
switch option {
case ENABLED:
@ -149,6 +195,32 @@ func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Temperatur
}
}
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
// uuid or wire-id
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.TemperatureSensor {
configTemperatureSensors := make(map[string]*types.Sensor, 0)
for _, name := range names {
for _, s := range c.Sensors {
switch name {
case s.SensorID:
configTemperatureSensors[s.SensorID] = s
case *s.WireID:
configTemperatureSensors[s.SensorID] = s
case s.SensorName:
configTemperatureSensors[s.SensorID] = s
}
}
}
temperatureSensors := make([]*types.Sensor, 0)
for _, cs := range configTemperatureSensors {
temperatureSensors = append(temperatureSensors, cs)
}
return c.convertTemperatureSensors(temperatureSensors)
}
// RemoveSensor deletes a sensor by its name or its unique UUID
func (c *Configuration) RemoveSensor(name string) error {
for i, sensor := range c.Sensors {
@ -180,23 +252,33 @@ func (c *Configuration) RenameSensor(oldName, newName string) error {
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
}
func (c *Configuration) splitSensors() ([]sensor.HumiditySensor, []sensor.TemperatureSensor) {
func (c *Configuration) convertHumiditySensors(sensors []*types.Sensor) []sensor.HumiditySensor {
humiditySensors := make([]sensor.HumiditySensor, 0)
temperatureSensors := make([]sensor.TemperatureSensor, 0)
for _, s := range c.Sensors {
for _, s := range sensors {
switch s.SensorModel {
case types.DHT11:
humiditySensors = append(humiditySensors, &sensor.DHT11{
Sensor: s,
})
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
Sensor: s,
})
case types.DHT22:
humiditySensors = append(humiditySensors, &sensor.DHT22{
Sensor: s,
})
}
}
return humiditySensors
}
func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor {
temperatureSensors := make([]sensor.TemperatureSensor, 0)
for _, s := range sensors {
switch s.SensorModel {
case types.DHT11:
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
Sensor: s,
})
case types.DHT22:
temperatureSensors = append(temperatureSensors, &sensor.DHT22{
Sensor: s,
})
@ -206,5 +288,5 @@ func (c *Configuration) splitSensors() ([]sensor.HumiditySensor, []sensor.Temper
})
}
}
return humiditySensors, temperatureSensors
return temperatureSensors
}