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
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
2019-06-17 21:37:48 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/internal/collect"
|
|
|
|
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/logfile"
|
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-17 21:37:48 +00:00
|
|
|
func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compression bool, round float64) error {
|
2019-06-16 21:15:26 +00:00
|
|
|
|
|
|
|
ticker := time.Tick(cleanCacheIntervall)
|
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-17 21:37:48 +00:00
|
|
|
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
|
2019-06-15 13:45:35 +00:00
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
|
|
|
err := rgbled.Green(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-17 21:37:48 +00:00
|
|
|
cancel()
|
2019-06-16 18:51:28 +00:00
|
|
|
return fmt.Errorf("Can not turn on blue info light: %v", err)
|
|
|
|
}
|
2019-06-15 13:45:35 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
2019-06-16 18:51:28 +00:00
|
|
|
err := rgbled.Blue(rgbLEDs)
|
|
|
|
if err != nil {
|
2019-06-17 21:37:48 +00:00
|
|
|
cancel()
|
2019-06-16 18:51:28 +00:00
|
|
|
return fmt.Errorf("Can not turn on yellow info light: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
2019-06-15 13:45:35 +00:00
|
|
|
if err != nil {
|
2019-06-17 21:37:48 +00:00
|
|
|
cancel()
|
2019-06-15 13:45:35 +00:00
|
|
|
return fmt.Errorf("Can not save temperatures: %v", err)
|
|
|
|
}
|
|
|
|
temperatures = 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-17 21:37:48 +00:00
|
|
|
cancel()
|
2019-06-16 18:51:28 +00:00
|
|
|
return fmt.Errorf("Can not turn on green info light: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case temperature, more := <-temperatureChannel:
|
|
|
|
if more {
|
|
|
|
temperatures = append(temperatures, temperature)
|
2019-06-17 21:37:48 +00:00
|
|
|
continue
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case killSignal := <-interrupt:
|
2019-06-17 21:37:48 +00:00
|
|
|
log.Printf("Daemon was interruped by system signal %v\n", killSignal)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
err := rgbled.Red(rgbLEDs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not turn on info light: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
errors := collect.Errors(errorChannel)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
log.Println(prittyprint.FormatErrors(errors))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not save temperatures: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|