2019-07-02 20:43:05 +00:00
|
|
|
package pressure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2019-09-04 11:37:50 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/storage/logfile"
|
2019-07-02 20:43:05 +00:00
|
|
|
"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/rgbled"
|
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logs bool
|
|
|
|
|
|
|
|
var readPressureCmd = &cobra.Command{
|
|
|
|
Use: "read",
|
2019-07-02 20:54:58 +00:00
|
|
|
Short: "Operates with air pressure values",
|
2019-07-02 20:43:05 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
// read configuration
|
2019-08-19 10:24:08 +00:00
|
|
|
cnf, err := config.Read(*configFile)
|
2019-07-02 20:43:05 +00:00
|
|
|
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 {
|
2019-09-04 11:37:50 +00:00
|
|
|
measuredValuesLogfile := logfile.New(cnf.Logfile)
|
2019-07-02 20:43:05 +00:00
|
|
|
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")
|
|
|
|
}
|