2018-11-20 21:55:06 +00:00
|
|
|
package temperature
|
2018-11-07 19:07:15 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-13 21:26:41 +00:00
|
|
|
"context"
|
2019-02-24 21:46:36 +00:00
|
|
|
"fmt"
|
2020-01-13 21:26:41 +00:00
|
|
|
"os"
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/cli"
|
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
|
|
"github.com/go-flucky/flucky/pkg/storage"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2018-11-07 19:07:15 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
var (
|
|
|
|
compression bool
|
2020-01-13 21:26:41 +00:00
|
|
|
save bool
|
2019-09-01 10:27:06 +00:00
|
|
|
round float64
|
|
|
|
)
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
// InitCmd initialize all temperature subcommands
|
|
|
|
func InitCmd(cmd *cobra.Command) error {
|
|
|
|
|
|
|
|
temperatureCmd := &cobra.Command{
|
|
|
|
Use: "temperature",
|
|
|
|
Short: "Operates with temperature values",
|
|
|
|
}
|
|
|
|
|
|
|
|
listTemperaturesCmd := &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List temperature values from specified or all sensors",
|
|
|
|
Example: fmt.Sprintf("flucky temperature list"),
|
|
|
|
RunE: listTemperatures,
|
|
|
|
}
|
2018-11-07 19:07:15 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
readTemperatures := &cobra.Command{
|
|
|
|
Use: "read",
|
|
|
|
Short: "Read temperature values from specified or all sensors",
|
|
|
|
Example: fmt.Sprintf("flucky temperature read"),
|
|
|
|
RunE: readTemperature,
|
|
|
|
}
|
|
|
|
readTemperatures.Flags().BoolVar(&save, "save", true, "Save humidities")
|
|
|
|
readTemperatures.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
|
|
|
readTemperatures.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
2019-09-01 10:27:06 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
temperatureCmd.AddCommand(listTemperaturesCmd)
|
|
|
|
temperatureCmd.AddCommand(readTemperatures)
|
2018-11-07 19:07:15 +00:00
|
|
|
cmd.AddCommand(temperatureCmd)
|
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listTemperatures(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No config file defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
storageEndpoint, err := cnf.GetStorageEndpointURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues, err := storage.Read(ctx, storageEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
|
|
|
|
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTemperature(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No config file defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sensors := make([]sensor.Sensor, 0)
|
|
|
|
if len(args) == 0 {
|
|
|
|
sensors = cnf.GetTemperatureSensors(config.ENABLED)
|
|
|
|
} else {
|
|
|
|
sensors = cnf.GetTemperatureSensorsByName(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sensors) == 0 {
|
|
|
|
return fmt.Errorf("No sensors matched")
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues, err := sensor.Read(sensors, types.MeasuredValueTypeTemperature)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
|
|
|
|
|
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
|
|
|
|
|
|
|
if save {
|
|
|
|
storageEndpoint, err := cnf.GetStorageEndpointURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = storage.Write(ctx, measuredValues, storageEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|