PKGBUILD/cmd/temperature/read.go
Markus Pesch dbef4f8241
fix(pkg/config): use storage endpoints
changes:
- Only one storage endpoint can be defined. This consists of a URL which
  can be used to specify whether the data is to be stored in a file or
  in a database.
2019-12-08 12:49:21 +01:00

75 lines
1.9 KiB
Go

package temperature
import (
"context"
"fmt"
"log"
"os"
"github.com/go-flucky/flucky/pkg/storage"
"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/sensor"
"github.com/spf13/cobra"
)
var logs bool
var readTemperatureCmd = &cobra.Command{
Use: "read",
Short: "Reading temperature values from different or specified sensors by arguments",
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
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.GetTemperatureSensors(config.ENABLED)
} else {
sensors = cnf.GetTemperatureSensorsByName(args)
}
if len(sensors) == 0 {
return
}
ctx := context.Background()
measuredValues, err := sensor.Read(ctx, sensors)
if err != nil {
log.Fatalln(err)
}
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
// print temperatures on stdout
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
if logs {
storageEndpoint, err := cnf.GetStorageEndpointURL()
if err != nil {
log.Fatalln(err)
}
err = storage.Write(ctx, measuredValues, storageEndpoint)
if err != nil {
log.Fatalln(err)
}
}
},
}
func init() {
temperatureCmd.AddCommand(readTemperatureCmd)
readTemperatureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
readTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
readTemperatureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
}