feat(pkg): add logger and round direct over sensor interface

This commit is contained in:
2019-06-18 23:02:11 +02:00
parent 2941f7a527
commit 088ed3b5f7
12 changed files with 192 additions and 91 deletions

View File

@ -2,26 +2,28 @@ package daemon
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/internal/collect"
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
"github.com/go-flucky/flucky/pkg/logfile"
"github.com/go-flucky/flucky/pkg/logger"
"github.com/go-flucky/flucky/pkg/rgbled"
"github.com/go-flucky/flucky/pkg/sensor"
"github.com/go-flucky/flucky/pkg/types"
)
// Start the daemon
func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compression bool, round float64) error {
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) {
ticker := time.Tick(cleanCacheIntervall)
// Info
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
logger.Info("Use compression: %v", compression)
logger.Info("Round values: %v", round)
ticker := time.Tick(cleanCacheInterval)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
@ -34,66 +36,63 @@ func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compres
childContext, cancel := context.WithCancel(ctx)
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), round, temperatureChannel, errorChannel)
temperatures := make([]*types.Temperature, 0)
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
err := rgbled.Green(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on blue info light: %v", err)
logger.Error("Can not turn on green info light: %v", err)
}
for {
select {
case err, _ := <-errorChannel:
logger.Error("%v", err)
case <-ticker:
err := rgbled.Blue(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on yellow info light: %v", err)
logger.Error("Can not turn on blue info light: %v", err)
}
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
if err != nil {
cancel()
return fmt.Errorf("Can not save temperatures: %v", err)
logger.Fatal("Can not save temperatures: %v", err)
}
temperatures = make([]*types.Temperature, 0)
err = rgbled.Green(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on green info light: %v", err)
logger.Error("Can not turn on green info light: %v", err)
}
case temperature, more := <-temperatureChannel:
if more {
temperatures = append(temperatures, temperature)
continue
}
case temperature, _ := <-temperatureChannel:
temperatures = append(temperatures, temperature)
case killSignal := <-interrupt:
log.Printf("Daemon was interruped by system signal %v\n", killSignal)
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
cancel()
err := rgbled.Red(rgbLEDs)
if err != nil {
return fmt.Errorf("Can not turn on info light: %v", err)
logger.Error("Can not turn on red info light: %v", err)
}
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
log.Println(prittyprint.FormatErrors(errors))
logger.Warn("Save remaining temperature data from the cache")
if compression {
temperatures = logfile.CompressTemperature(temperatures)
}
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
if err != nil {
return fmt.Errorf("Can not save temperatures: %v", err)
logger.Fatal("Can not save temperatures: %v", err)
}
return nil
return
}
}
}