76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"text/tabwriter"
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
|
)
|
|
|
|
func PrintTemperatures(temperatures []*types.Temperature, cnf *config.FluckyConfig, w io.Writer) {
|
|
|
|
sensors := []*types.Sensor{}
|
|
|
|
// Search after sensors
|
|
for _, temperature := range temperatures {
|
|
found := false
|
|
|
|
// Search for the sensor that has acquired the measured value
|
|
for _, sensor := range cnf.Sensors {
|
|
if sensor.SensorID == temperature.SensorID {
|
|
sensors = append(sensors, sensor)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
// If it was not found, pass only the sensor with the UUID on
|
|
if !found {
|
|
sensor := &types.Sensor{
|
|
SensorID: temperature.SensorID,
|
|
}
|
|
sensors = append(sensors, sensor)
|
|
}
|
|
}
|
|
|
|
// sort temperature values for every sensor
|
|
orderedTemperatures := make(map[string][]*types.Temperature)
|
|
for _, temperature := range temperatures {
|
|
orderedTemperatures[temperature.SensorID] = append(orderedTemperatures[temperature.SensorID], temperature)
|
|
}
|
|
|
|
// declare tabwriter
|
|
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
|
|
|
// headlines
|
|
for i, sensor := range sensors {
|
|
fmt.Fprintf(tw, "%v\t", sensor.Name())
|
|
if i == len(sensors)-1 {
|
|
fmt.Fprintf(tw, "\n")
|
|
}
|
|
}
|
|
|
|
// find sensor with maximum temperature values
|
|
maxLength := 0
|
|
for _, orderedTemperature := range orderedTemperatures {
|
|
if len(orderedTemperature) > maxLength {
|
|
maxLength = len(orderedTemperature)
|
|
}
|
|
}
|
|
|
|
// body
|
|
for i := 0; i < maxLength; i++ {
|
|
for _, sensor := range sensors {
|
|
if len(orderedTemperatures[sensor.SensorID]) > i {
|
|
fmt.Fprintf(tw, "%3.3f\t", orderedTemperatures[sensor.SensorID][i].TemperatureValue)
|
|
} else {
|
|
fmt.Fprint(tw, "\t")
|
|
}
|
|
fmt.Fprint(tw, "\n")
|
|
}
|
|
}
|
|
tw.Flush()
|
|
}
|