feat(cmd/pressure): add new subcommand to operate with aire pressure

This commit is contained in:
Markus Pesch 2019-07-02 22:43:05 +02:00
parent 93c08d8147
commit 9feee62c35
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
4 changed files with 153 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/go-flucky/flucky/cmd/compression" "github.com/go-flucky/flucky/cmd/compression"
"github.com/go-flucky/flucky/cmd/convert" "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/daemon"
"github.com/go-flucky/flucky/cmd/humidity" "github.com/go-flucky/flucky/cmd/humidity"
@ -63,6 +64,7 @@ func Execute(version string) {
convert.InitCmd(rootCmd, configFile) convert.InitCmd(rootCmd, configFile)
daemon.InitCmd(rootCmd, configFile) daemon.InitCmd(rootCmd, configFile)
humidity.InitCmd(rootCmd, configFile) humidity.InitCmd(rootCmd, configFile)
pressure.InitCmd(rootCmd, configFile)
rgbled.InitCmd(rootCmd, configFile) rgbled.InitCmd(rootCmd, configFile)
sensor.InitCmd(rootCmd, configFile) sensor.InitCmd(rootCmd, configFile)
temperature.InitCmd(rootCmd, configFile) temperature.InitCmd(rootCmd, configFile)

54
cmd/pressure/list.go Normal file
View File

@ -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)
}

22
cmd/pressure/pressure.go Normal file
View File

@ -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)
}

75
cmd/pressure/read.go Normal file
View File

@ -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")
}