2019-06-23 19:33:45 +00:00
|
|
|
package humidity
|
|
|
|
|
|
|
|
import (
|
2019-06-27 16:49:04 +00:00
|
|
|
"context"
|
2019-06-23 19:33:45 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/storage"
|
2019-07-02 20:33:01 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-27 16:49:04 +00:00
|
|
|
|
2019-06-23 19:33:45 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/cli"
|
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
2019-07-02 20:33:01 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
2019-06-23 19:33:45 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logs bool
|
|
|
|
|
|
|
|
var readHumidityCmd = &cobra.Command{
|
|
|
|
Use: "read",
|
2019-07-02 20:54:58 +00:00
|
|
|
Short: "Reading air pressure values from different or specified sensors by arguments",
|
2019-06-23 19:33:45 +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-06-23 19:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all temperature sensors or sensors by args
|
2019-06-27 16:49:04 +00:00
|
|
|
sensors := make([]sensor.Sensor, 0)
|
2019-06-23 19:33:45 +00:00
|
|
|
if len(args) == 0 {
|
2019-06-27 16:49:04 +00:00
|
|
|
sensors = cnf.GetHumiditySensors(config.ENABLED)
|
2019-06-23 19:33:45 +00:00
|
|
|
} else {
|
2019-06-27 16:49:04 +00:00
|
|
|
sensors = cnf.GetHumiditySensorsByName(args)
|
2019-06-23 19:33:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 19:47:31 +00:00
|
|
|
if len(sensors) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
2019-06-30 12:34:13 +00:00
|
|
|
if err := rgbled.Run(rgbLEDs); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-06-27 16:49:04 +00:00
|
|
|
|
2019-06-27 19:47:31 +00:00
|
|
|
ctx := context.Background()
|
2019-06-27 16:49:04 +00:00
|
|
|
measuredValues, err := sensor.Read(ctx, sensors)
|
2019-06-23 19:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeHumidity, measuredValues)
|
2019-06-27 16:49:04 +00:00
|
|
|
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
2019-06-23 19:33:45 +00:00
|
|
|
|
2019-06-27 18:31:52 +00:00
|
|
|
if logs {
|
2019-12-07 15:53:49 +00:00
|
|
|
storageEndpoint, err := cnf.GetStorageEndpointURL()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = storage.Write(ctx, measuredValues, storageEndpoint)
|
2019-06-27 18:31:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 19:47:31 +00:00
|
|
|
|
|
|
|
rgbled.Off(rgbLEDs)
|
2019-06-23 19:33:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|