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
|
|
|
"log"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
|
|
"github.com/go-flucky/flucky/pkg/daemon"
|
2019-06-13 19:25:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-10-11 10:36:00 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Must set logger for daemon package
|
|
|
|
// logLevel, err := cmd.Flags().GetString("loglevel")
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf("No loglevel defined: %v", err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
//flogger := initializeLogger(logLevel)
|
|
|
|
// daemon.SetLogLevel(flogger)
|
|
|
|
|
|
|
|
cnf, err := config.Read(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeLogger(logLevel string) logger.Logger {
|
|
|
|
log.Println(logLevel)
|
|
|
|
switch logLevel {
|
|
|
|
case "debug", "DEBUG":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelDebug)
|
|
|
|
case "info", "INFO":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelInfo)
|
|
|
|
case "warn", "WARN":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelWarn)
|
|
|
|
case "error", "ERROR":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelError)
|
|
|
|
case "fatal", "FATAL":
|
|
|
|
return logger.NewDefaultLogger(logger.LogLevelFatal)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|