2018-12-04 18:11:50 +00:00
|
|
|
package humidity
|
|
|
|
|
|
|
|
import (
|
2020-01-13 21:26:41 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/cli"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/sensor"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/storage"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2018-12-04 18:11:50 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
var (
|
|
|
|
compression bool
|
2020-01-13 21:26:41 +00:00
|
|
|
configFile string
|
|
|
|
save bool
|
2019-09-01 10:27:06 +00:00
|
|
|
round float64
|
|
|
|
)
|
2018-12-04 18:11:50 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
// InitCmd initialize all humidity subcommands
|
|
|
|
func InitCmd(cmd *cobra.Command) error {
|
|
|
|
humidityCmd := &cobra.Command{
|
|
|
|
Use: "humidity",
|
|
|
|
Short: "Operates with humidity values",
|
|
|
|
}
|
2018-12-04 18:11:50 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
listHumiditiesCmd := &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List humidity values from specified or all sensors",
|
|
|
|
Example: fmt.Sprintf("flucky humidity list"),
|
|
|
|
RunE: listHumidities,
|
|
|
|
}
|
2018-12-04 18:11:50 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
readHumiditiesCmd := &cobra.Command{
|
|
|
|
Use: "read",
|
|
|
|
Short: "Read humidity values from specified or all sensors",
|
|
|
|
Example: fmt.Sprintf("flucky humidity read"),
|
|
|
|
RunE: readHumidities,
|
|
|
|
}
|
|
|
|
readHumiditiesCmd.Flags().BoolVar(&save, "save", true, "Save humidities")
|
|
|
|
readHumiditiesCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
|
|
|
readHumiditiesCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
|
|
|
|
|
|
|
humidityCmd.AddCommand(listHumiditiesCmd)
|
|
|
|
humidityCmd.AddCommand(readHumiditiesCmd)
|
2018-12-04 18:11:50 +00:00
|
|
|
cmd.AddCommand(humidityCmd)
|
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listHumidities(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No config file defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// read configuration
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
storageEndpoint, err := cnf.GetStorageEndpointURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues, err := storage.Read(ctx, storageEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeHumidity, measuredValues)
|
|
|
|
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readHumidities(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No config file defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all temperature sensors or sensors by args
|
|
|
|
sensors := make([]sensor.Sensor, 0)
|
|
|
|
if len(args) == 0 {
|
|
|
|
sensors = cnf.GetHumiditySensors(config.ENABLED)
|
|
|
|
} else {
|
|
|
|
sensors = cnf.GetHumiditySensorsByName(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sensors) == 0 {
|
|
|
|
return fmt.Errorf("No sensors matched")
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues, err := sensor.Read(sensors, types.MeasuredValueTypeHumidity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeHumidity, measuredValues)
|
|
|
|
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
|
|
|
|
|
|
|
if save {
|
|
|
|
storageEndpoint, err := cnf.GetStorageEndpointURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = storage.Write(ctx, measuredValues, storageEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2018-12-04 18:11:50 +00:00
|
|
|
}
|