PKGBUILD/cmd/temperature/read.go

61 lines
1.5 KiB
Go
Raw Normal View History

2019-02-24 21:46:36 +00:00
package temperature
import (
"fmt"
2019-02-24 21:46:36 +00:00
"log"
"os"
"github.com/spf13/cobra"
2019-05-12 09:57:53 +00:00
"github.com/volker-raschek/flucky/pkg/cli"
"github.com/volker-raschek/flucky/pkg/config"
"github.com/volker-raschek/flucky/pkg/logfile"
2019-05-12 09:57:53 +00:00
"github.com/volker-raschek/flucky/pkg/sensor"
2019-02-24 21:46:36 +00:00
)
2019-06-14 19:17:06 +00:00
var compression bool
2019-03-03 18:13:37 +00:00
var logs bool
2019-02-24 21:46:36 +00:00
var readTemperatureCmd = &cobra.Command{
Use: "read",
Short: "read temperature from sensor",
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
2019-02-24 21:46:36 +00:00
Run: func(cmd *cobra.Command, args []string) {
// read configuration
2019-06-15 12:25:45 +00:00
cnf, err := config.Read(configPath)
2019-02-24 21:46:36 +00:00
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)
}
2019-02-24 21:46:36 +00:00
2019-02-28 18:48:25 +00:00
// read temperature from sensors
temperatures, err := sensor.ReadTemperatures(temperatureSensors)
if err != nil {
log.Fatalln(err)
}
2019-02-24 21:46:36 +00:00
2019-02-28 18:48:25 +00:00
// print temperatures on stdout
cli.PrintTemperatures(temperatures, cnf, os.Stdout)
if logs {
2019-06-14 19:17:06 +00:00
err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
if err != nil {
log.Fatalln(err)
}
}
2019-02-24 21:46:36 +00:00
},
}
func init() {
temperatureCmd.AddCommand(readTemperatureCmd)
2019-03-03 18:13:37 +00:00
readTemperatureCmd.Flags().BoolVarP(&logs, "logs", "l", true, "Log temperature")
2019-06-14 19:17:06 +00:00
readTemperatureCmd.Flags().BoolVarP(&compression, "compression", "c", true, "Compress measured with logged temperatures")
2019-02-24 21:46:36 +00:00
}