feat(daemon): add new subcommand to start a daemon
This commit is contained in:
parent
82faa1d536
commit
5f859139a4
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user