From 16705d1164eb75fc93981e1435b16c24eef65b17 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 23 Jun 2019 21:33:45 +0200 Subject: [PATCH] feat(cli/humidity): add new subcommand to read humidity values --- cmd/cmd.go | 2 + cmd/humidity/get.go | 25 ----- cmd/humidity/humidity.go | 2 + cmd/humidity/read.go | 57 ++++++++++ pkg/cli/cli.go | 140 +++++++++++++++++++++--- pkg/internal/temperature/temperature.go | 99 ----------------- 6 files changed, 186 insertions(+), 139 deletions(-) delete mode 100644 cmd/humidity/get.go create mode 100644 cmd/humidity/read.go delete mode 100644 pkg/internal/temperature/temperature.go diff --git a/cmd/cmd.go b/cmd/cmd.go index dbf4079..72db61b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,6 +6,7 @@ import ( "time" "github.com/go-flucky/flucky/cmd/daemon" + "github.com/go-flucky/flucky/cmd/humidity" "github.com/go-flucky/flucky/cmd/rgbled" "github.com/go-flucky/flucky/cmd/sensor" "github.com/go-flucky/flucky/cmd/temperature" @@ -57,6 +58,7 @@ func Execute(version string) { rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file") daemon.InitCmd(rootCmd, configFile) + humidity.InitCmd(rootCmd, configFile) rgbled.InitCmd(rootCmd, configFile) sensor.InitCmd(rootCmd, configFile) temperature.InitCmd(rootCmd, configFile) diff --git a/cmd/humidity/get.go b/cmd/humidity/get.go deleted file mode 100644 index 2770d50..0000000 --- a/cmd/humidity/get.go +++ /dev/null @@ -1,25 +0,0 @@ -package humidity - -import ( - "github.com/spf13/cobra" -) - -var follow, push bool - -var getHumidityCmd = &cobra.Command{ - Use: "get", - Short: "get humidity from sensor", - Run: func(cmd *cobra.Command, args []string) { - - // if err := humidity.Get(); err != nil { - // log.Fatal(err) - // } - - }, -} - -func init() { - humidityCmd.AddCommand(getHumidityCmd) - // getTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output") - // getTemperatureCmd.Flags().BoolVarP(&push, "push", "p", false, "Push to remote server") -} diff --git a/cmd/humidity/humidity.go b/cmd/humidity/humidity.go index 308ee64..3c9b596 100644 --- a/cmd/humidity/humidity.go +++ b/cmd/humidity/humidity.go @@ -4,7 +4,9 @@ import ( "github.com/spf13/cobra" ) +var compression bool var configFile string +var round float64 var humidityCmd = &cobra.Command{ Use: "humidity", diff --git a/cmd/humidity/read.go b/cmd/humidity/read.go new file mode 100644 index 0000000..e43d531 --- /dev/null +++ b/cmd/humidity/read.go @@ -0,0 +1,57 @@ +package humidity + +import ( + "log" + "os" + + "github.com/go-flucky/flucky/pkg/cli" + "github.com/go-flucky/flucky/pkg/config" + "github.com/go-flucky/flucky/pkg/sensor" + "github.com/spf13/cobra" +) + +var logs bool + +var readHumidityCmd = &cobra.Command{ + Use: "read", + Short: "read humidity from sensor", + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // fetch all temperature sensors or sensors by args + humiditySensors := make([]sensor.HumiditySensor, 0) + if len(args) == 0 { + humiditySensors = cnf.GetHumiditySensors(config.ENABLED) + } else { + humiditySensors = cnf.GetHumiditySensorsByName(args) + } + + humidities, err := sensor.ReadHumidities(humiditySensors, round) + if err != nil { + log.Fatalln(err) + } + + // print humidities on stdout + cli.PrintHumidities(humidities, cnf, os.Stdout) + + // if logs { + // humiditiyLogfile := logfile.New(cnf.Device.HumidityLogfile) + // err := logfile.AppendTemperatures(humiditiyLogfile, compression, humidities) + // if err != nil { + // log.Fatalln(err) + // } + // } + }, +} + +func init() { + humidityCmd.AddCommand(readHumidityCmd) + readHumidityCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature") + readHumidityCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures") + readHumidityCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function") +} diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index d83dd26..6505aa1 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -4,30 +4,138 @@ import ( "fmt" "io" "text/tabwriter" + "time" "github.com/go-flucky/flucky/pkg/config" - "github.com/go-flucky/flucky/pkg/internal/temperature" "github.com/go-flucky/flucky/pkg/types" ) -// PrintHumidities displays a list of humidities -func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w io.Writer) { - // determine all humidity sensors based on the humidiy values +// GetSensorsByTemperatures returns commulated list of sensors by temperature values +func GetSensorsByHumidities(humidities []*types.Humidity, cnf *config.Configuration) []*types.Sensor { 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 + 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 it was not found, pass only the sensor with the UUID on - sensor := &types.Sensor{ - SensorID: humidity.SensorID, + if duplicated { + continue + } + if foundSensor != nil { + sensors = append(sensors, foundSensor) + continue + } else { + sensors = append(sensors, &types.Sensor{SensorID: humidity.SensorID}) } - sensors = append(sensors, sensor) } + 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 + } + } + } + } + if duplicated { + continue + } + 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) + } + + 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) // sort temperature values for every sensor orderedHumidities := make(map[string][]*types.Humidity) @@ -62,12 +170,14 @@ func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w } else { fmt.Fprint(tw, "\t") } - fmt.Fprint(tw, "\n") + } + fmt.Fprint(tw, "\n") } tw.Flush() } +// PrintRGBLEDs displays a list with all configured RGBLEDs func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) { // declare tabwriter @@ -103,7 +213,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error { // PrintTemperatures displays a list of temperatures func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configuration, w io.Writer) { - sensors := temperature.GetSensorsByTemperatures(temperatures, cnf) + sensors := GetSensorsByTemperatures(temperatures, cnf) // sort temperature values for every sensor orderedTemperatures := make(map[string][]*types.Temperature) diff --git a/pkg/internal/temperature/temperature.go b/pkg/internal/temperature/temperature.go deleted file mode 100644 index 8889aa1..0000000 --- a/pkg/internal/temperature/temperature.go +++ /dev/null @@ -1,99 +0,0 @@ -package temperature - -import ( - "fmt" - "time" - - "github.com/go-flucky/flucky/pkg/config" - "github.com/go-flucky/flucky/pkg/types" -) - -// 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 - } - } - } - } - if duplicated { - continue - } - 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) - } - - 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 -}