2019-02-24 21:46:36 +00:00
|
|
|
package temperature
|
|
|
|
|
|
|
|
import (
|
2019-02-28 22:23:21 +00:00
|
|
|
"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/sensor"
|
2019-02-24 21:46:36 +00:00
|
|
|
)
|
|
|
|
|
2019-03-03 18:13:37 +00:00
|
|
|
var logs bool
|
2019-02-24 21:46:36 +00:00
|
|
|
|
|
|
|
var readTemperatureCmd = &cobra.Command{
|
2019-02-28 22:23:21 +00:00
|
|
|
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
|
|
|
|
fc, err := config.Read(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2019-02-28 18:48:25 +00:00
|
|
|
// fetch all temperature sensors
|
2019-02-28 22:23:21 +00:00
|
|
|
temperatureSensors, err := fc.GetTemperatureSensors(args)
|
2019-02-24 21:46:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
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, fc, os.Stdout)
|
2019-03-03 18:13:37 +00:00
|
|
|
|
|
|
|
if logs {
|
|
|
|
err = fc.FileLogger.LogTemperatures(temperatures)
|
|
|
|
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(&follow, "follow", "f", false, "Follow output")
|
|
|
|
readTemperatureCmd.Flags().BoolVarP(&logs, "logs", "l", true, "Log temperature")
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|