74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package humidity
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/go-flucky/flucky/pkg/logfile"
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
|
"github.com/go-flucky/flucky/pkg/types/typeswitch"
|
|
|
|
"github.com/go-flucky/flucky/pkg/cli"
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var logs bool
|
|
|
|
var readHumidityCmd = &cobra.Command{
|
|
Use: "read",
|
|
Short: "read humidity 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.GetHumiditySensors(config.ENABLED)
|
|
} else {
|
|
sensors = cnf.GetHumiditySensorsByName(args)
|
|
}
|
|
|
|
if len(sensors) == 0 {
|
|
return
|
|
}
|
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
|
rgbled.Run(rgbLEDs)
|
|
|
|
ctx := context.Background()
|
|
measuredValues, err := sensor.Read(ctx, sensors)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
measuredValues = typeswitch.HumidityValues(measuredValues)
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
|
|
|
if logs {
|
|
logfile := logfile.New(cnf.Device.Logfile)
|
|
err := logfile.Append(compression, measuredValues)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|
|
|
|
rgbled.Off(rgbLEDs)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
humidityCmd.AddCommand(readHumidityCmd)
|
|
readHumidityCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
|
|
readHumidityCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
|
readHumidityCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
|
}
|