package pressure 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 pressure subcommands func InitCmd(cmd *cobra.Command) error { pressureCmd := &cobra.Command{ Use: "pressure", Short: "Operates with pressure values", } listPressuresCmd := &cobra.Command{ Use: "list", Short: "List pressure values from specified or all sensors", Example: fmt.Sprintf("flucky pressure list"), RunE: listHumidities, } readPressuresCmd := &cobra.Command{ Use: "read", Short: "Read pressure values from specified or all sensors", Example: fmt.Sprintf("flucky pressure read"), RunE: readPressure, } readPressuresCmd.Flags().BoolVar(&save, "save", true, "Save humidities") readPressuresCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures") readPressuresCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function") pressureCmd.AddCommand(listPressuresCmd) pressureCmd.AddCommand(readPressuresCmd) cmd.AddCommand(pressureCmd) 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") } 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.MeasuredValueTypePressure, measuredValues) cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) return nil } func readPressure(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.GetPressureSensors(config.ENABLED) } else { sensors = cnf.GetPressureSensorsByName(args) } if len(sensors) == 0 { return fmt.Errorf("No sensors matched") } measuredValues, err := sensor.Read(sensors, types.MeasuredValueTypePressure) if err != nil { return err } measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypePressure, 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 }