2019-02-24 21:46:36 +00:00
|
|
|
package temperature
|
|
|
|
|
|
|
|
import (
|
2019-06-27 07:17:34 +00:00
|
|
|
"context"
|
2019-02-28 22:23:21 +00:00
|
|
|
"fmt"
|
2019-02-24 21:46:36 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2019-06-27 07:17:34 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types/typeswitch"
|
|
|
|
|
2019-06-24 21:08:23 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
|
|
|
|
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/logfile"
|
|
|
|
"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{
|
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
|
2019-06-16 11:00:50 +00:00
|
|
|
cnf, err := config.Read(configFile)
|
2019-02-24 21:46:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
// fetch all temperature sensors or sensors by args
|
2019-06-25 20:56:09 +00:00
|
|
|
sensors := make([]sensor.Sensor, 0)
|
2019-06-15 11:02:52 +00:00
|
|
|
if len(args) == 0 {
|
2019-06-25 20:56:09 +00:00
|
|
|
sensors = cnf.GetTemperatureSensors(config.ENABLED)
|
2019-06-15 11:02:52 +00:00
|
|
|
} else {
|
2019-06-25 20:56:09 +00:00
|
|
|
sensors = cnf.GetTemperatureSensorsByName(args)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-27 19:47:31 +00:00
|
|
|
if len(sensors) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
2019-06-30 12:34:13 +00:00
|
|
|
if err := rgbled.Run(rgbLEDs); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-06-27 19:47:31 +00:00
|
|
|
|
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 {
|
2019-06-24 21:08:23 +00:00
|
|
|
rgbled.Error(rgbLEDs)
|
2019-02-28 18:48:25 +00:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-02-24 21:46:36 +00:00
|
|
|
|
2019-06-27 18:31:52 +00:00
|
|
|
measuredValues = typeswitch.TemperatureValues(measuredValues)
|
2019-06-27 07:17:34 +00:00
|
|
|
|
2019-02-28 18:48:25 +00:00
|
|
|
// print temperatures on stdout
|
2019-06-27 18:31:52 +00:00
|
|
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
2019-06-13 21:21:41 +00:00
|
|
|
|
|
|
|
if logs {
|
2019-06-30 12:34:13 +00:00
|
|
|
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
|
|
|
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
2019-06-13 21:21:41 +00:00
|
|
|
if err != nil {
|
2019-06-24 21:08:23 +00:00
|
|
|
rgbled.Error(rgbLEDs)
|
2019-06-13 21:21:41 +00:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 21:08:23 +00:00
|
|
|
rgbled.Off(rgbLEDs)
|
2019-02-24 21:46:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
temperatureCmd.AddCommand(readTemperatureCmd)
|
2019-06-17 21:37:48 +00:00
|
|
|
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
|
|
|
}
|