fix(pkg/cli): GetSensorIDsByMeasuredValues

This commit is contained in:
Markus Pesch 2020-01-11 12:10:26 +01:00
parent f1a4ade402
commit b595cf1ac8
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 23 additions and 32 deletions

View File

@ -10,32 +10,11 @@ import (
) )
// GetSensorsByMeasuredValues returns commulated list of sensors by measured values // GetSensorsByMeasuredValues returns commulated list of sensors by measured values
func GetSensorsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) []*types.Sensor { func GetSensorIDsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) map[string]*types.Sensor {
sensors := []*types.Sensor{} sensors := make(map[string]*types.Sensor)
for _, measuredValue := range measuredValues { for _, measuredValue := range measuredValues {
duplicated := false if _, ok := sensors[measuredValue.SensorID]; !ok {
foundSensor := &types.Sensor{} sensors[measuredValue.SensorID] = cnf.GetSensorByID(measuredValue.SensorID)
for _, cnfSensor := range cnf.Sensors {
if measuredValue.ID == cnfSensor.ID {
foundSensor = cnfSensor
// compare if id has already been added to list
for _, sensor := range sensors {
if cnfSensor.ID == sensor.ID {
duplicated = true
break
}
}
}
}
if duplicated {
continue
}
if foundSensor != nil {
sensors = append(sensors, foundSensor)
continue
} else {
sensors = append(sensors, &types.Sensor{ID: measuredValue.ID})
} }
} }
return sensors return sensors
@ -109,26 +88,28 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
// PrintMeasuredValues displays a list of measured values // PrintMeasuredValues displays a list of measured values
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) { func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) {
sensors := GetSensorsByMeasuredValues(measuredValues, cnf) sensors := GetSensorIDsByMeasuredValues(measuredValues, cnf)
// sort measured values for every sensor // sort measured values for every sensor
orderedMeasuredValues := make(map[string][]*types.MeasuredValue) orderedMeasuredValues := make(map[string][]*types.MeasuredValue)
for _, measuredValue := range measuredValues { for _, measuredValue := range measuredValues {
orderedMeasuredValues[measuredValue.ID] = append(orderedMeasuredValues[measuredValue.ID], measuredValue) orderedMeasuredValues[measuredValue.SensorID] = append(orderedMeasuredValues[measuredValue.SensorID], measuredValue)
} }
// declare tabwriter // declare tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
// headlines // headlines
for i, sensor := range sensors { i := 0
for _, sensor := range sensors {
fmt.Fprintf(tw, "%v\t", sensor.FullName()) fmt.Fprintf(tw, "%v\t", sensor.FullName())
if i == len(sensors)-1 { if i == len(sensors)-1 {
fmt.Fprintf(tw, "\n") fmt.Fprintf(tw, "\n")
} }
i++
} }
// find sensor with maximum temperature values // find sensor with the most measured values
maxLength := 0 maxLength := 0
for _, orderedMeasuredValue := range orderedMeasuredValues { for _, orderedMeasuredValue := range orderedMeasuredValues {
if len(orderedMeasuredValue) > maxLength { if len(orderedMeasuredValue) > maxLength {

View File

@ -82,8 +82,8 @@ func (c *Configuration) AddSensor(sensor *types.Sensor) error {
} }
// check if sensor has a valid device id // check if sensor has a valid device id
if sensor.ID != c.Device.ID { if sensor.DeviceID != c.Device.ID {
sensor.ID = c.Device.ID sensor.DeviceID = c.Device.ID
} }
// overwrite creation date // overwrite creation date
@ -362,6 +362,17 @@ func (c *Configuration) GetRGBLEDsByName(names []string) []rgbled.RGBLED {
return c.convertRGBLEDs(rgbLEDs) return c.convertRGBLEDs(rgbLEDs)
} }
// GetSensorByID returns a sensor matched by his id. If no sensor has this id,
// the function returns nil
func (c *Configuration) GetSensorByID(id string) *types.Sensor {
for _, sensor := range c.Sensors {
if sensor.ID == id {
return sensor
}
}
return nil
}
// GetSensors returns a list of humidity sensors // GetSensors returns a list of humidity sensors
func (c *Configuration) GetSensors(option Option) []sensor.Sensor { func (c *Configuration) GetSensors(option Option) []sensor.Sensor {
cachedSensors := make([]*types.Sensor, 0) cachedSensors := make([]*types.Sensor, 0)

View File

@ -37,7 +37,6 @@ func SelectMeasuredValues(measuredValueType MeasuredValueType, measuredValues []
cachedMeasuredValues = append(cachedMeasuredValues, measuredValue) cachedMeasuredValues = append(cachedMeasuredValues, measuredValue)
} }
} }
return cachedMeasuredValues return cachedMeasuredValues
} }