From 5f859139a463588c587b89d6d741e7a259718212 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 15 Jun 2019 15:45:35 +0200 Subject: [PATCH] feat(daemon): add new subcommand to start a daemon --- cmd/cmd.go | 3 +- cmd/daemon/daemon.go | 17 +++++++++--- pkg/daemon/daemon.go | 63 +++++++++++++++++++++++++++++++++--------- pkg/logfile/logfile.go | 16 ++++++++++- 4 files changed, 80 insertions(+), 19 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 1220390..2d16aae 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -5,6 +5,7 @@ import ( "os" "time" + "github.com/volker-raschek/flucky/cmd/daemon" "github.com/volker-raschek/flucky/cmd/sensor" "github.com/volker-raschek/flucky/cmd/temperature" "github.com/volker-raschek/flucky/pkg/types" @@ -53,7 +54,7 @@ func Execute(version string) { rootCmd.Version = version rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file") - // humidity.InitCmd(rootCmd, configPath) + daemon.InitCmd(rootCmd, configPath) sensor.InitCmd(rootCmd, configPath) temperature.InitCmd(rootCmd, configPath) rootCmd.Execute() diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 550724f..0e8eb24 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -1,7 +1,11 @@ package daemon import ( + "log" + "github.com/spf13/cobra" + "github.com/volker-raschek/flucky/pkg/config" + "github.com/volker-raschek/flucky/pkg/daemon" ) var configPath string @@ -11,10 +15,15 @@ var daemonCmd = &cobra.Command{ Short: "Read continuously data from all enabled sensors", Run: func(cmd *cobra.Command, args []string) { // read configuration - // cnf, err := config.Read(configPath) - // if err != nil { - // log.Fatalln(err) - // } + cnf, err := config.Read(configPath) + if err != nil { + log.Fatalln(err) + } + + err = daemon.Start(cnf) + if err != nil { + panic(err) + } }, } diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 01f2503..5a755c2 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -1,26 +1,63 @@ package daemon import ( + "fmt" + "log" + "os" + "os/signal" + "syscall" + "time" + "github.com/volker-raschek/flucky/pkg/config" + "github.com/volker-raschek/flucky/pkg/logfile" + "github.com/volker-raschek/flucky/pkg/sensor" + "github.com/volker-raschek/flucky/pkg/types" ) // Start the daemon func Start(cnf *config.Configuration) error { - // interrupt := make(chan os.Signal, 1) - // signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM) + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM) - // humidityChannel := make(chan *types.Humidity) - // temperatureChannel := make(chan *types.Temperature) + errorChannel := make(chan error, 0) + //humidityChannel := make(chan *types.Humidity, 0) + temperatureChannel := make(chan *types.Temperature, 0) - // go sensor.ReadHumiditiesContinuously(humiditySensors, humidityChannel) - // go sensor.ReadTemperaturesContinuously(temperatureSensors, temperatureChannel) + // go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel) + go sensor.ReadTemperaturesContinuously(cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel) - // for { - // select { - // case killSignal := <-interrupt: - // return fmt.Errorf("Daemon was interruped by system signal %v", killSignal) - // } - // } - return nil + temperatures := make([]*types.Temperature, 0) + + ticker := time.Tick(time.Second * 60) + + for { + select { + case <-ticker: + log.Printf("Write measured values into logfile") + + log.Printf("%v new measured temperature values", len(temperatures)) + err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, true) + if err != nil { + return fmt.Errorf("Can not save temperatures: %v", err) + } + temperatures = make([]*types.Temperature, 0) + + case temperature, more := <-temperatureChannel: + if more { + temperatures = append(temperatures, temperature) + } else { + log.Printf("Temperature Channel closed. Write remaining values into the logfile") + err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, true) + if err != nil { + return fmt.Errorf("Can not save temperatures: %v", err) + } + } + + case killSignal := <-interrupt: + //close(humidityChannel) + close(temperatureChannel) + return fmt.Errorf("Daemon was interruped by system signal %v", killSignal) + } + } } diff --git a/pkg/logfile/logfile.go b/pkg/logfile/logfile.go index 0def712..246c1d0 100644 --- a/pkg/logfile/logfile.go +++ b/pkg/logfile/logfile.go @@ -74,6 +74,20 @@ func ReadTemperatures(temperatureLogfile string) ([]*types.Temperature, error) { return temperatures, nil } +func ReadTemperaturesChannel(temperatureChannel <-chan *types.Temperature) []*types.Temperature { + temperatures := make([]*types.Temperature, 0) + for { + select { + case temperature, more := <-temperatureChannel: + if more { + temperatures = append(temperatures, temperature) + } + default: + return temperatures + } + } +} + // ReadTemperaturesCustom from a custom reader and returns an array with // temperatures func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) { @@ -172,7 +186,7 @@ func WriteTemperaturesCustom(temperatures []*types.Temperature, w io.Writer, com writeCreationDate(temperatures) jsonEncoder := json.NewEncoder(w) - jsonEncoder.SetIndent("", " ") + jsonEncoder.SetIndent("", " ") err := jsonEncoder.Encode(temperatures) if err != nil { return fmt.Errorf("Can not encode temperatures: %v", err)