PKGBUILD/cmd/temperature/read.go

62 lines
1.6 KiB
Go

package temperature
import (
"fmt"
"log"
"os"
"github.com/go-flucky/flucky/pkg/cli"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/logfile"
"github.com/go-flucky/flucky/pkg/sensor"
"github.com/spf13/cobra"
)
var compression bool
var logs bool
var round float64
var readTemperatureCmd = &cobra.Command{
Use: "read",
Short: "read temperature from sensor",
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
temperatureSensors := make([]sensor.TemperatureSensor, 0)
if len(args) == 0 {
temperatureSensors = cnf.GetTemperatureSensors(config.ENABLED)
} else {
temperatureSensors = cnf.GetTemperatureSensorsByName(args)
}
temperatures, err := sensor.ReadTemperatures(temperatureSensors, round)
if err != nil {
log.Fatalln(err)
}
// print temperatures on stdout
cli.PrintTemperatures(temperatures, cnf, os.Stdout)
if logs {
err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
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")
}