feat(daemon): add new subcommand to start a daemon
This commit is contained in:
		| @@ -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) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user