2019-06-13 19:25:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2019-06-15 13:45:35 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
|
|
"github.com/go-flucky/flucky/pkg/logfile"
|
2019-06-18 21:02:11 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/logger"
|
2019-06-16 18:51:28 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-13 19:25:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start the daemon
|
2019-06-18 21:02:11 +00:00
|
|
|
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) {
|
2019-06-16 21:15:26 +00:00
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
// Info
|
|
|
|
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
|
|
|
logger.Info("Use compression: %v", compression)
|
|
|
|
logger.Info("Round values: %v", round)
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile)
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
ticker := time.Tick(cleanCacheInterval)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
|
|
|
|
errorChannel := make(chan error, 0)
|
|
|
|
//humidityChannel := make(chan *types.Humidity, 0)
|
|
|
|
temperatureChannel := make(chan *types.Temperature, 0)
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
childContext, cancel := context.WithCancel(ctx)
|
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
|
2019-06-18 21:02:11 +00:00
|
|
|
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), round, temperatureChannel, errorChannel)
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
temperatureCache := make([]*types.Temperature, 0)
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
|
|
|
err := rgbled.Green(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Error("Can not turn on green info light: %v", err)
|
2019-06-16 18:51:28 +00:00
|
|
|
}
|
2019-06-15 13:45:35 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-06-18 21:02:11 +00:00
|
|
|
case err, _ := <-errorChannel:
|
2019-06-21 12:37:02 +00:00
|
|
|
err = rgbled.Red(rgbLEDs)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Can not turn on red info light: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Error("%v", err)
|
|
|
|
|
2019-06-21 12:37:02 +00:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case <-ticker:
|
2019-06-16 18:51:28 +00:00
|
|
|
err := rgbled.Blue(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Error("Can not turn on blue info light: %v", err)
|
2019-06-16 18:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
//temperaturesLogfile,_ := temperatureLogfile.ReadTemperatures()
|
|
|
|
|
|
|
|
err = logfile.AppendTemperatures(temperatureLogfile, compression, temperatureCache)
|
2019-06-15 13:45:35 +00:00
|
|
|
if err != nil {
|
2019-06-21 12:37:02 +00:00
|
|
|
|
|
|
|
err = rgbled.Red(rgbLEDs)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Can not turn on red info light: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
cancel()
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Fatal("Can not save temperatures: %v", err)
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
2019-06-19 17:18:01 +00:00
|
|
|
temperatureCache = make([]*types.Temperature, 0)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
err = rgbled.Green(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Error("Can not turn on green info light: %v", err)
|
2019-06-16 18:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
case temperature, _ := <-temperatureChannel:
|
2019-06-19 17:18:01 +00:00
|
|
|
temperatureCache = append(temperatureCache, temperature)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case killSignal := <-interrupt:
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
|
2019-06-17 21:37:48 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
err := rgbled.Red(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Error("Can not turn on red info light: %v", err)
|
2019-06-16 18:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
logger.Warn("Save remaining temperature data from the cache")
|
2019-06-19 17:18:01 +00:00
|
|
|
err = logfile.AppendTemperatures(temperatureLogfile, compression, temperatureCache)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
2019-06-19 17:18:01 +00:00
|
|
|
logger.Fatal("%v", err)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
return
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|