2019-02-24 21:46:36 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
2019-06-23 19:33:45 +00:00
|
|
|
"time"
|
2019-02-24 21:46:36 +00:00
|
|
|
|
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-23 19:33:45 +00:00
|
|
|
// GetSensorsByTemperatures returns commulated list of sensors by temperature values
|
|
|
|
func GetSensorsByHumidities(humidities []*types.Humidity, cnf *config.Configuration) []*types.Sensor {
|
2019-02-28 22:23:21 +00:00
|
|
|
sensors := []*types.Sensor{}
|
|
|
|
for _, humidity := range humidities {
|
2019-06-23 19:33:45 +00:00
|
|
|
duplicated := false
|
|
|
|
foundSensor := &types.Sensor{}
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
|
|
|
if humidity.SensorID == cnfSensor.SensorID {
|
|
|
|
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 {
|
|
|
|
sensors = append(sensors, &types.Sensor{SensorID: humidity.SensorID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sensors
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensorsByTemperatures returns commulated list of sensors by temperature values
|
|
|
|
func GetSensorsByTemperatures(temperatures []*types.Temperature, cnf *config.Configuration) []*types.Sensor {
|
|
|
|
sensors := []*types.Sensor{}
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
duplicated := false
|
|
|
|
foundSensor := &types.Sensor{}
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
|
|
|
if temperature.SensorID == cnfSensor.SensorID {
|
|
|
|
foundSensor = cnfSensor
|
|
|
|
|
|
|
|
// compare if id has already been added to list
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
if cnfSensor.SensorID == sensor.SensorID {
|
|
|
|
duplicated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 22:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 19:33:45 +00:00
|
|
|
if duplicated {
|
|
|
|
continue
|
2019-02-28 22:23:21 +00:00
|
|
|
}
|
2019-06-23 19:33:45 +00:00
|
|
|
if foundSensor != nil {
|
|
|
|
sensors = append(sensors, foundSensor)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
sensors = append(sensors, &types.Sensor{SensorID: temperature.SensorID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sensors
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperaturesBetweenTimeRange returns a list of temperatures between a given time range
|
|
|
|
func GetTemperaturesBetweenTimeRange(from time.Time, till *time.Time, temperatures []*types.Temperature) ([]*types.Temperature, error) {
|
|
|
|
|
|
|
|
if till != nil && from.After(*till) {
|
|
|
|
fFormat := from.Format("2006-01-02 15:04:05")
|
|
|
|
tFormat := till.Format("2006-01-02 15:04:05")
|
|
|
|
return nil, fmt.Errorf("%v is after %v", fFormat, tFormat)
|
2019-02-28 22:23:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 19:33:45 +00:00
|
|
|
cachedTemperatures := []*types.Temperature{}
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
|
|
|
|
if temperature.TemperatureFromDate.After(from) && till == nil {
|
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if temperature.TemperatureFromDate.After(from) && temperature.TemperatureTillDate.Before(*till) {
|
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedTemperatures, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperaturesBySensors returns a list of temperatures given by the sensor id.
|
|
|
|
// If the sensor name, wire-id or id can not found in configured sensors, it would be skiped!
|
|
|
|
func GetTemperaturesBySensors(sensorNamesOrIDs []string, temperatures []*types.Temperature, cnf *config.Configuration) []*types.Temperature {
|
|
|
|
cachedSensors := []*types.Sensor{}
|
|
|
|
cachedTemperatures := []*types.Temperature{}
|
|
|
|
|
|
|
|
// match sensor name, wire-id or sensor id with configured sensors
|
|
|
|
// and append the matched sensor to the list
|
|
|
|
for _, sensor := range sensorNamesOrIDs {
|
|
|
|
for _, cs := range cnf.Sensors {
|
|
|
|
if cs.SensorID == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
if cs.WireID != nil && *cs.WireID == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
if cs.SensorName == sensor {
|
|
|
|
cachedSensors = append(cachedSensors, cs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// march all temperatures which matched with the sensor id
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
for _, cachedSensor := range cachedSensors {
|
|
|
|
if temperature.SensorID == cachedSensor.SensorID {
|
|
|
|
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedTemperatures
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintHumidities displays a list of humidities
|
|
|
|
func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w io.Writer) {
|
|
|
|
|
|
|
|
sensors := GetSensorsByHumidities(humidities, cnf)
|
|
|
|
|
2019-02-28 22:23:21 +00:00
|
|
|
// 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")
|
|
|
|
}
|
2019-06-23 19:33:45 +00:00
|
|
|
|
2019-02-28 22:23:21 +00:00
|
|
|
}
|
2019-06-23 19:33:45 +00:00
|
|
|
fmt.Fprint(tw, "\n")
|
2019-02-28 22:23:21 +00:00
|
|
|
}
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
2019-06-23 19:33:45 +00:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
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-06-13 19:25:32 +00:00
|
|
|
func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configuration, w io.Writer) {
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-23 19:33:45 +00:00
|
|
|
sensors := 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")
|
|
|
|
}
|
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()
|
|
|
|
}
|