PKGBUILD/cmd/temperature/temperature.go

132 lines
3.0 KiB
Go
Raw Normal View History

package temperature
2018-11-07 19:07:15 +00:00
import (
"context"
2019-02-24 21:46:36 +00:00
"fmt"
"os"
2019-02-24 21:46:36 +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"
)
var (
compression bool
save bool
round float64
)
// 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
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")
temperatureCmd.AddCommand(listTemperaturesCmd)
temperatureCmd.AddCommand(readTemperatures)
2018-11-07 19:07:15 +00:00
cmd.AddCommand(temperatureCmd)
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
}