2019-06-13 19:25:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2020-01-13 21:26:41 +00:00
|
|
|
"fmt"
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/cmd/internal"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/daemon"
|
2019-06-13 19:25:32 +00:00
|
|
|
)
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
var (
|
2020-01-11 16:24:18 +00:00
|
|
|
cachedMeasuredValues uint
|
|
|
|
compression bool
|
|
|
|
round float64
|
|
|
|
temperatureUnit string
|
2019-09-01 10:27:06 +00:00
|
|
|
)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2020-01-13 21:26:41 +00:00
|
|
|
// InitCmd initialize all daemon subcommands
|
|
|
|
func InitCmd(cmd *cobra.Command) error {
|
|
|
|
daemonCmd := &cobra.Command{
|
|
|
|
Use: "daemon",
|
|
|
|
Short: "Read continuously data from all enabled sensors",
|
|
|
|
Example: "flucky daemon",
|
|
|
|
RunE: run,
|
|
|
|
}
|
2019-09-01 10:27:06 +00:00
|
|
|
|
2019-06-16 21:15:26 +00:00
|
|
|
daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
2020-01-11 16:24:18 +00:00
|
|
|
daemonCmd.Flags().UintVar(&cachedMeasuredValues, "cached-values", 500, "Number of cached values before saveing into the storage endpoint")
|
2019-12-07 15:53:49 +00:00
|
|
|
daemonCmd.Flags().Float64Var(&round, "round", 0.5, "Round values. The value 0 deactivates the function")
|
2020-01-13 21:26:41 +00:00
|
|
|
cmd.AddCommand(daemonCmd)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cmd *cobra.Command, args []string) error {
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No config file defined: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:55:43 +00:00
|
|
|
logLevel, err := cmd.Flags().GetString("loglevel")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No loglevel defined: %v", err)
|
|
|
|
}
|
2020-01-13 21:26:41 +00:00
|
|
|
|
2020-01-18 13:55:43 +00:00
|
|
|
flogger := internal.InitializeLogger(logLevel)
|
|
|
|
daemon.SetLogger(flogger)
|
2020-01-13 21:26:41 +00:00
|
|
|
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
|
|
|
}
|