feat(daemon): add new subcommand to start a daemon
This commit is contained in:
		@@ -5,6 +5,7 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/volker-raschek/flucky/cmd/daemon"
 | 
				
			||||||
	"github.com/volker-raschek/flucky/cmd/sensor"
 | 
						"github.com/volker-raschek/flucky/cmd/sensor"
 | 
				
			||||||
	"github.com/volker-raschek/flucky/cmd/temperature"
 | 
						"github.com/volker-raschek/flucky/cmd/temperature"
 | 
				
			||||||
	"github.com/volker-raschek/flucky/pkg/types"
 | 
						"github.com/volker-raschek/flucky/pkg/types"
 | 
				
			||||||
@@ -53,7 +54,7 @@ func Execute(version string) {
 | 
				
			|||||||
	rootCmd.Version = version
 | 
						rootCmd.Version = version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file")
 | 
						rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file")
 | 
				
			||||||
	// humidity.InitCmd(rootCmd, configPath)
 | 
						daemon.InitCmd(rootCmd, configPath)
 | 
				
			||||||
	sensor.InitCmd(rootCmd, configPath)
 | 
						sensor.InitCmd(rootCmd, configPath)
 | 
				
			||||||
	temperature.InitCmd(rootCmd, configPath)
 | 
						temperature.InitCmd(rootCmd, configPath)
 | 
				
			||||||
	rootCmd.Execute()
 | 
						rootCmd.Execute()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,11 @@
 | 
				
			|||||||
package daemon
 | 
					package daemon
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
						"github.com/volker-raschek/flucky/pkg/config"
 | 
				
			||||||
 | 
						"github.com/volker-raschek/flucky/pkg/daemon"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var configPath string
 | 
					var configPath string
 | 
				
			||||||
@@ -11,10 +15,15 @@ var daemonCmd = &cobra.Command{
 | 
				
			|||||||
	Short: "Read continuously data from all enabled sensors",
 | 
						Short: "Read continuously data from all enabled sensors",
 | 
				
			||||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
						Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
		// read configuration
 | 
							// read configuration
 | 
				
			||||||
		// cnf, err := config.Read(configPath)
 | 
							cnf, err := config.Read(configPath)
 | 
				
			||||||
		// if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
		// 	log.Fatalln(err)
 | 
								log.Fatalln(err)
 | 
				
			||||||
		// }
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = daemon.Start(cnf)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								panic(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,26 +1,63 @@
 | 
				
			|||||||
package daemon
 | 
					package daemon
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"os/signal"
 | 
				
			||||||
 | 
						"syscall"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/volker-raschek/flucky/pkg/config"
 | 
						"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
 | 
					// Start the daemon
 | 
				
			||||||
func Start(cnf *config.Configuration) error {
 | 
					func Start(cnf *config.Configuration) error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// interrupt := make(chan os.Signal, 1)
 | 
						interrupt := make(chan os.Signal, 1)
 | 
				
			||||||
	// signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
 | 
						signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// humidityChannel := make(chan *types.Humidity)
 | 
						errorChannel := make(chan error, 0)
 | 
				
			||||||
	// temperatureChannel := make(chan *types.Temperature)
 | 
						//humidityChannel := make(chan *types.Humidity, 0)
 | 
				
			||||||
 | 
						temperatureChannel := make(chan *types.Temperature, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// go sensor.ReadHumiditiesContinuously(humiditySensors, humidityChannel)
 | 
						// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
 | 
				
			||||||
	// go sensor.ReadTemperaturesContinuously(temperatureSensors, temperatureChannel)
 | 
						go sensor.ReadTemperaturesContinuously(cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// for {
 | 
						temperatures := make([]*types.Temperature, 0)
 | 
				
			||||||
	// 	select {
 | 
					
 | 
				
			||||||
	// 	case killSignal := <-interrupt:
 | 
						ticker := time.Tick(time.Second * 60)
 | 
				
			||||||
	// 		return fmt.Errorf("Daemon was interruped by system signal %v", killSignal)
 | 
					
 | 
				
			||||||
	// 	}
 | 
						for {
 | 
				
			||||||
	// }
 | 
							select {
 | 
				
			||||||
	return nil
 | 
							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)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,6 +74,20 @@ func ReadTemperatures(temperatureLogfile string) ([]*types.Temperature, error) {
 | 
				
			|||||||
	return temperatures, nil
 | 
						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
 | 
					// ReadTemperaturesCustom from a custom reader and returns an array with
 | 
				
			||||||
// temperatures
 | 
					// temperatures
 | 
				
			||||||
func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) {
 | 
					func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user