package humidity import ( "context" "fmt" "os" "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" "github.com/spf13/cobra" ) var ( compression bool configFile string save bool round float64 ) // InitCmd initialize all humidity subcommands func InitCmd(cmd *cobra.Command) error { humidityCmd := &cobra.Command{ Use: "humidity", Short: "Operates with humidity values", } listHumiditiesCmd := &cobra.Command{ Use: "list", Short: "List humidity values from specified or all sensors", Example: fmt.Sprintf("flucky humidity list"), RunE: listHumidities, } 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) cmd.AddCommand(humidityCmd) 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 }