From 9feee62c35c7db9344fd09a96e03385a7806ee18 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Tue, 2 Jul 2019 22:43:05 +0200 Subject: [PATCH] feat(cmd/pressure): add new subcommand to operate with aire pressure --- cmd/cmd.go | 2 ++ cmd/pressure/list.go | 54 +++++++++++++++++++++++++++++ cmd/pressure/pressure.go | 22 ++++++++++++ cmd/pressure/read.go | 75 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 cmd/pressure/list.go create mode 100644 cmd/pressure/pressure.go create mode 100644 cmd/pressure/read.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 2bdb378..bddb35a 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -7,6 +7,7 @@ import ( "github.com/go-flucky/flucky/cmd/compression" "github.com/go-flucky/flucky/cmd/convert" + "github.com/go-flucky/flucky/cmd/pressure" "github.com/go-flucky/flucky/cmd/daemon" "github.com/go-flucky/flucky/cmd/humidity" @@ -63,6 +64,7 @@ func Execute(version string) { convert.InitCmd(rootCmd, configFile) daemon.InitCmd(rootCmd, configFile) humidity.InitCmd(rootCmd, configFile) + pressure.InitCmd(rootCmd, configFile) rgbled.InitCmd(rootCmd, configFile) sensor.InitCmd(rootCmd, configFile) temperature.InitCmd(rootCmd, configFile) diff --git a/cmd/pressure/list.go b/cmd/pressure/list.go new file mode 100644 index 0000000..4f8ed9c --- /dev/null +++ b/cmd/pressure/list.go @@ -0,0 +1,54 @@ +package pressure + +import ( + "fmt" + "log" + "os" + + "github.com/go-flucky/flucky/pkg/types" + + "github.com/go-flucky/flucky/pkg/cli" + "github.com/go-flucky/flucky/pkg/config" + "github.com/go-flucky/flucky/pkg/logfile" + "github.com/go-flucky/flucky/pkg/rgbled" + "github.com/spf13/cobra" +) + +var listTemperatureCmd = &cobra.Command{ + Use: "list", + Short: "print air pressures", + Example: fmt.Sprintf("flucky pressure logs"), + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + logfile := logfile.New(cnf.Device.Logfile) + + rgbLEDs := cnf.GetRGBLEDs(config.ENABLED) + if err := rgbled.Logfile(rgbLEDs); err != nil { + log.Fatalln(err) + } + + measuredValues, err := logfile.Read() + if err != nil { + log.Fatalln(err) + } + + if err := rgbled.Off(rgbLEDs); err != nil { + log.Fatalln(err) + } + + measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypePressure, measuredValues) + + cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) + + }, +} + +func init() { + pressureCmd.AddCommand(listTemperatureCmd) +} diff --git a/cmd/pressure/pressure.go b/cmd/pressure/pressure.go new file mode 100644 index 0000000..eb1b065 --- /dev/null +++ b/cmd/pressure/pressure.go @@ -0,0 +1,22 @@ +package pressure + +import ( + "github.com/spf13/cobra" +) + +var compression bool +var configFile string +var round float64 + +var pressureCmd = &cobra.Command{ + Use: "pressure", + Short: "Read pressure from sensor", +} + +// Execute a +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile + + cmd.AddCommand(pressureCmd) + +} diff --git a/cmd/pressure/read.go b/cmd/pressure/read.go new file mode 100644 index 0000000..a38e051 --- /dev/null +++ b/cmd/pressure/read.go @@ -0,0 +1,75 @@ +package pressure + +import ( + "context" + "log" + "os" + + "github.com/go-flucky/flucky/pkg/types" + + "github.com/go-flucky/flucky/pkg/cli" + "github.com/go-flucky/flucky/pkg/config" + "github.com/go-flucky/flucky/pkg/logfile" + "github.com/go-flucky/flucky/pkg/rgbled" + "github.com/go-flucky/flucky/pkg/sensor" + "github.com/spf13/cobra" +) + +var logs bool + +var readPressureCmd = &cobra.Command{ + Use: "read", + Short: "read pressure 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 + sensors := make([]sensor.Sensor, 0) + if len(args) == 0 { + sensors = cnf.GetPressureSensors(config.ENABLED) + } else { + sensors = cnf.GetPressureSensorsByName(args) + } + + if len(sensors) == 0 { + return + } + + rgbLEDs := cnf.GetRGBLEDs(config.ENABLED) + if err := rgbled.Run(rgbLEDs); err != nil { + log.Fatalln(err) + } + + ctx := context.Background() + measuredValues, err := sensor.Read(ctx, sensors) + if err != nil { + log.Fatalln(err) + } + + measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypePressure, measuredValues) + + cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) + + if logs { + measuredValuesLogfile := logfile.New(cnf.Device.Logfile) + err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues) + if err != nil { + log.Fatalln(err) + } + } + + rgbled.Off(rgbLEDs) + }, +} + +func init() { + pressureCmd.AddCommand(readPressureCmd) + readPressureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature") + readPressureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures") + readPressureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function") +}