PKGBUILD/cmd/temperature/read.go

56 lines
1.4 KiB
Go

package temperature
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/volker-raschek/flucky/pkg/cli"
"github.com/volker-raschek/flucky/pkg/config"
"github.com/volker-raschek/flucky/pkg/logfile"
"github.com/volker-raschek/flucky/pkg/sensor"
)
var compression bool
var logs bool
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(cnfPath)
if err != nil {
log.Fatalln(err)
}
// fetch all temperature sensors
temperatureSensors := cnf.GetTemperatureSensors(config.ENABLED)
// read temperature from sensors
temperatures, err := sensor.ReadTemperatures(temperatureSensors)
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().BoolVarP(&logs, "logs", "l", true, "Log temperature")
readTemperatureCmd.Flags().BoolVarP(&compression, "compression", "c", true, "Compress measured with logged temperatures")
}