2019-06-13 19:25:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"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-15 14:42:54 +00:00
|
|
|
func Start(cnf *config.Configuration, compression bool) error {
|
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)
|
|
|
|
|
|
|
|
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
|
|
|
|
go sensor.ReadTemperaturesContinuously(cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
2019-06-16 18:51:28 +00:00
|
|
|
ticker := time.Tick(time.Second * 300)
|
|
|
|
|
|
|
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
|
|
|
err := rgbled.Green(rgbLEDs)
|
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Can not turn on yellow info light: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
log.Printf("Write measured values into logfile")
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
log.Printf("%v new measured temperature values", len(temperatures))
|
2019-06-16 18:51:28 +00:00
|
|
|
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
2019-06-15 13:45:35 +00:00
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
log.Printf("Temperature Channel closed. Write remaining values into the logfile")
|
2019-06-15 14:42:54 +00:00
|
|
|
err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
2019-06-15 13:45:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not save temperatures: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case killSignal := <-interrupt:
|
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-15 13:45:35 +00:00
|
|
|
//close(humidityChannel)
|
|
|
|
close(temperatureChannel)
|
|
|
|
return fmt.Errorf("Daemon was interruped by system signal %v", killSignal)
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|