PKGBUILD/pkg/cli/cli.go

163 lines
4.2 KiB
Go
Raw Normal View History

2019-02-24 21:46:36 +00:00
package cli
import (
"fmt"
"io"
"text/tabwriter"
2019-03-04 20:19:33 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/logger"
2019-03-04 16:50:41 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/internal/temperature"
2019-02-24 21:46:36 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
// PrintHumidities displays a list of humidities
func PrintHumidities(humidities []*types.Humidity, cnf *config.FluckyConfig, w io.Writer) {
// determine all humidity sensors based on the humidiy values
sensors := []*types.Sensor{}
for _, humidity := range humidities {
// Search for the sensor that has acquired the measured value
for _, sensor := range cnf.Sensors {
if sensor.SensorID == humidity.SensorID {
sensors = append(sensors, sensor)
break
}
}
// If it was not found, pass only the sensor with the UUID on
sensor := &types.Sensor{
SensorID: humidity.SensorID,
}
sensors = append(sensors, sensor)
}
// sort temperature values for every sensor
orderedHumidities := make(map[string][]*types.Humidity)
for _, humidity := range humidities {
orderedHumidities[humidity.SensorID] = append(orderedHumidities[humidity.SensorID], humidity)
}
// 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 _, orderedHumidity := range orderedHumidities {
if len(orderedHumidity) > maxLength {
maxLength = len(orderedHumidity)
}
}
// body
for i := 0; i < maxLength; i++ {
for _, sensor := range sensors {
if len(orderedHumidities[sensor.SensorID]) > i {
fmt.Fprintf(tw, "%3.3f\t", orderedHumidities[sensor.SensorID][i].HumidityValue)
} else {
fmt.Fprint(tw, "\t")
}
fmt.Fprint(tw, "\n")
}
}
tw.Flush()
}
2019-03-04 20:19:33 +00:00
func PrintLoggers(cnf *config.FluckyConfig, w io.Writer) {
// declare tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
fmt.Fprintf(w, "type\tvalue\t\tlocation\n")
for _, logfile := range cnf.LogFiles[logger.LogTemperature] {
fmt.Fprintf(w, "file\t%v\t%v\n", logger.LogTemperature, logfile)
}
tw.Flush()
}
// PrintRemotes displays a list with all configured remote addresses
func PrintRemotes(cnf *config.FluckyConfig, w io.Writer) error {
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
fmt.Fprint(tw, "name\taddress\tenabled\tregistered\n")
for _, remote := range cnf.Remotes {
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", remote.Name, remote.Address, remote.Enabled, remote.Registered)
}
tw.Flush()
return nil
}
// PrintSensors displays a list with all configured sensors
func PrintSensors(cnf *config.FluckyConfig, w io.Writer) error {
// declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n")
for _, sensor := range cnf.Sensors {
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled)
}
tw.Flush()
return nil
}
// PrintTemperatures displays a list of temperatures
2019-02-24 21:46:36 +00:00
func PrintTemperatures(temperatures []*types.Temperature, cnf *config.FluckyConfig, w io.Writer) {
2019-03-04 16:50:41 +00:00
sensors := temperature.GetSensorsByTemperatures(temperatures, cnf)
2019-02-24 21:46:36 +00:00
// 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
2019-02-28 18:48:25 +00:00
for i, sensor := range sensors {
2019-02-24 21:46:36 +00:00
fmt.Fprintf(tw, "%v\t", sensor.Name())
2019-02-28 18:48:25 +00:00
if i == len(sensors)-1 {
fmt.Fprintf(tw, "\n")
}
2019-02-24 21:46:36 +00:00
}
// 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()
}