PKGBUILD/cmd/temperature/read.go

80 lines
2.1 KiB
Go
Raw Normal View History

2019-02-24 21:46:36 +00:00
package temperature
import (
2019-06-27 07:17:34 +00:00
"context"
"fmt"
2019-02-24 21:46:36 +00:00
"log"
"os"
"github.com/go-flucky/flucky/pkg/rgbled"
"github.com/go-flucky/flucky/pkg/storage/logfile"
"github.com/go-flucky/flucky/pkg/types"
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/cli"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/sensor"
2019-02-24 21:46:36 +00:00
"github.com/spf13/cobra"
)
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: "Reading temperature values from different or specified sensors by arguments",
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
cnf, err := config.Read(*configFile)
2019-02-24 21:46:36 +00:00
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)
}
2019-02-24 21:46:36 +00:00
if len(sensors) == 0 {
return
}
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
if err := rgbled.Run(rgbLEDs); err != nil {
log.Fatalln(err)
}
2019-06-27 07:17:34 +00:00
ctx := context.Background()
measuredValues, err := sensor.Read(ctx, sensors)
2019-02-28 18:48:25 +00:00
if err != nil {
rgbled.Error(rgbLEDs)
2019-02-28 18:48:25 +00:00
log.Fatalln(err)
}
2019-02-24 21:46:36 +00:00
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
2019-06-27 07:17:34 +00:00
2019-02-28 18:48:25 +00:00
// print temperatures on stdout
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
if logs {
measuredValuesLogfile := logfile.New(cnf.Logfile)
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
if err != nil {
rgbled.Error(rgbLEDs)
log.Fatalln(err)
}
}
rgbled.Off(rgbLEDs)
2019-02-24 21:46:36 +00:00
},
}
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")
2019-02-24 21:46:36 +00:00
}