2019-02-24 21:46:36 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-02-24 21:46:36 +00:00
|
|
|
)
|
|
|
|
|
2019-06-25 20:56:09 +00:00
|
|
|
// GetSensorsByMeasuredValues returns commulated list of sensors by measured values
|
2019-07-02 20:33:01 +00:00
|
|
|
func GetSensorsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) []*types.Sensor {
|
2019-02-28 22:23:21 +00:00
|
|
|
sensors := []*types.Sensor{}
|
2019-06-25 20:56:09 +00:00
|
|
|
for _, measuredValue := range measuredValues {
|
2019-06-23 19:33:45 +00:00
|
|
|
duplicated := false
|
|
|
|
foundSensor := &types.Sensor{}
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
2019-07-02 20:33:01 +00:00
|
|
|
if measuredValue.SensorID == cnfSensor.SensorID {
|
2019-06-23 19:33:45 +00:00
|
|
|
foundSensor = cnfSensor
|
|
|
|
|
|
|
|
// compare if id has already been added to list
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
if cnfSensor.SensorID == sensor.SensorID {
|
|
|
|
duplicated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if duplicated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if foundSensor != nil {
|
|
|
|
sensors = append(sensors, foundSensor)
|
|
|
|
continue
|
|
|
|
} else {
|
2019-07-02 20:33:01 +00:00
|
|
|
sensors = append(sensors, &types.Sensor{SensorID: measuredValue.SensorID})
|
2019-06-23 19:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sensors
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintRGBLEDs displays a list with all configured RGBLEDs
|
2019-06-16 11:00:50 +00:00
|
|
|
func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
|
|
|
|
|
|
|
|
// declare tabwriter
|
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
|
|
|
|
|
|
|
// headline
|
2019-06-24 20:57:29 +00:00
|
|
|
fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled\taction")
|
2019-06-16 11:00:50 +00:00
|
|
|
|
|
|
|
for _, rgbled := range cnf.RGBLEDs {
|
2019-06-24 20:57:29 +00:00
|
|
|
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t", rgbled.RGBLEDName, rgbled.RGBLEDLocation, *rgbled.BaseColorsToGPIO[types.BaseColorBlue], *rgbled.BaseColorsToGPIO[types.BaseColorGreen], *rgbled.BaseColorsToGPIO[types.BaseColorRed], rgbled.RGBLEDEnabled)
|
|
|
|
|
|
|
|
for action, color := range rgbled.ActionMapping {
|
|
|
|
fmt.Fprintf(tw, "%v=%v,", action, color)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(tw, "\n")
|
2019-06-16 11:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:31:01 +00:00
|
|
|
// PrintSensors displays a list with all configured sensors
|
2019-06-13 19:25:32 +00:00
|
|
|
func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
2019-03-04 10:31:01 +00:00
|
|
|
|
|
|
|
// declar tabwriter
|
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
|
|
|
|
2019-07-24 15:18:56 +00:00
|
|
|
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\tenabled\n")
|
2019-03-04 10:31:01 +00:00
|
|
|
|
|
|
|
for _, sensor := range cnf.Sensors {
|
2019-07-24 15:18:56 +00:00
|
|
|
fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel)
|
|
|
|
|
|
|
|
if sensor.WireID != nil {
|
|
|
|
fmt.Fprintf(tw, "%v\t", *sensor.WireID)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(tw, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sensor.I2CBus != nil {
|
|
|
|
fmt.Fprintf(tw, "%v\t", *sensor.I2CBus)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(tw, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sensor.I2CAddress != nil {
|
|
|
|
fmt.Fprintf(tw, "%v\t", *sensor.I2CAddress)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(tw, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sensor.GPIONumber != nil {
|
|
|
|
fmt.Fprintf(tw, "%v\t", *sensor.GPIONumber)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(tw, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(tw, "%v\n", sensor.SensorEnabled)
|
2019-03-04 10:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:56:09 +00:00
|
|
|
// PrintMeasuredValues displays a list of measured values
|
2019-07-02 20:33:01 +00:00
|
|
|
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) {
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-25 20:56:09 +00:00
|
|
|
sensors := GetSensorsByMeasuredValues(measuredValues, cnf)
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-25 20:56:09 +00:00
|
|
|
// sort measured values for every sensor
|
2019-07-02 20:33:01 +00:00
|
|
|
orderedMeasuredValues := make(map[string][]*types.MeasuredValue)
|
2019-06-25 20:56:09 +00:00
|
|
|
for _, measuredValue := range measuredValues {
|
2019-07-02 20:33:01 +00:00
|
|
|
orderedMeasuredValues[measuredValue.SensorID] = append(orderedMeasuredValues[measuredValue.SensorID], measuredValue)
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-06-25 20:56:09 +00:00
|
|
|
for _, orderedMeasuredValue := range orderedMeasuredValues {
|
|
|
|
if len(orderedMeasuredValue) > maxLength {
|
|
|
|
maxLength = len(orderedMeasuredValue)
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// body
|
|
|
|
for i := 0; i < maxLength; i++ {
|
|
|
|
for _, sensor := range sensors {
|
2019-06-25 20:56:09 +00:00
|
|
|
if len(orderedMeasuredValues[sensor.SensorID]) > i {
|
2019-07-02 20:33:01 +00:00
|
|
|
fmt.Fprintf(tw, "%3.3f\t", orderedMeasuredValues[sensor.SensorID][i].Value)
|
2019-02-24 21:46:36 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprint(tw, "\t")
|
|
|
|
}
|
2019-03-06 19:17:28 +00:00
|
|
|
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
2019-03-06 19:17:28 +00:00
|
|
|
fmt.Fprint(tw, "\n")
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
tw.Flush()
|
|
|
|
}
|