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-09-04 11:37:50 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
2019-10-11 10:44:04 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/storage"
|
2019-09-04 11:37:50 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/storage/db"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-10-11 10:36:00 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
2019-06-13 19:25:32 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
var (
|
|
|
|
postgresHost = "markus-pc.trier.cryptic.systems"
|
|
|
|
postgresPort = "5432"
|
|
|
|
postgresDatabase = "postgres"
|
|
|
|
postgresUser = "postgres"
|
|
|
|
postgresPassword = "postgres"
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
flogger = logger.NewSilentLogger()
|
2019-12-07 15:53:49 +00:00
|
|
|
)
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
func SetLogger(logger logger.Logger) {
|
|
|
|
flogger = logger
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// Start the daemon
|
2019-09-04 11:37:50 +00:00
|
|
|
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, version *semver.Version) {
|
2019-06-16 21:15:26 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
// Context
|
|
|
|
parentCtx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
|
|
|
|
|
|
|
// Ticker
|
|
|
|
// saveTicker := time.Tick(cleanCacheInterval)
|
|
|
|
|
|
|
|
// channels
|
|
|
|
debugChannel := make(chan string, 0)
|
|
|
|
infoChannel := make(chan string, 0)
|
|
|
|
warnChannel := make(chan string, 0)
|
|
|
|
errorChannel := make(chan error, 0)
|
|
|
|
fatalChannel := make(chan error, 1)
|
|
|
|
interruptChannel := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChannel, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
measuredValueChannel := make(chan *types.MeasuredValue, 0)
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
// Info
|
2019-09-04 11:37:50 +00:00
|
|
|
flogger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
|
|
|
flogger.Info("Use compression: %v", compression)
|
|
|
|
flogger.Info("Round: %v", round)
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
ticker := time.Tick(cleanCacheInterval)
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValuesCache := make([]*types.MeasuredValue, 0)
|
2019-12-07 15:53:49 +00:00
|
|
|
// measuredValuesLogfile := logfile.New(cnf.Logfile)
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
// Producer
|
|
|
|
go sensor.ReadContinuously(ctx, cnf.GetSensors(config.ENABLED), measuredValueChannel, errorChannel)
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
// Distributor
|
|
|
|
//measuredValueChannels := distribute.MeasuredValues(ctx, 5, measuredValueChannel)
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-12-07 15:53:49 +00:00
|
|
|
case debug, _ := <-debugChannel:
|
|
|
|
flogger.Debug("%v", debug)
|
|
|
|
case info, _ := <-infoChannel:
|
|
|
|
flogger.Info("%v", info)
|
|
|
|
case warn, _ := <-warnChannel:
|
|
|
|
flogger.Warn("%v", warn)
|
2019-06-18 21:02:11 +00:00
|
|
|
case err, _ := <-errorChannel:
|
2019-09-04 11:37:50 +00:00
|
|
|
flogger.Error("%v", err)
|
2019-12-07 15:53:49 +00:00
|
|
|
case fatal, _ := <-fatalChannel:
|
|
|
|
flogger.Fatal("Received a fatal error: %v", fatal)
|
|
|
|
case interrupt := <-interruptChannel:
|
|
|
|
flogger.Info("Received OS Signal: %v", interrupt)
|
|
|
|
flogger.Info("Close context")
|
|
|
|
cancel()
|
|
|
|
flogger.Info("Close channels")
|
|
|
|
close(debugChannel)
|
|
|
|
close(infoChannel)
|
|
|
|
close(warnChannel)
|
|
|
|
close(errorChannel)
|
|
|
|
close(interruptChannel)
|
|
|
|
return
|
2019-06-21 12:37:02 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
case <-ticker:
|
2019-06-16 18:51:28 +00:00
|
|
|
|
2019-10-11 10:44:04 +00:00
|
|
|
if round != 0 {
|
|
|
|
storage.Round(measuredValuesCache, round)
|
|
|
|
}
|
|
|
|
|
|
|
|
if compression {
|
|
|
|
measuredValuesCache = storage.Compression(measuredValuesCache)
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
// if err := logfile.Append(measuredValuesLogfile, measuredValuesCache); err != nil {
|
|
|
|
// flogger.Error("Can not save caches measured values in logfile: %v", err)
|
|
|
|
// }
|
2019-08-20 19:37:45 +00:00
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValuesCache = make([]*types.MeasuredValue, 0)
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2019-12-07 15:53:49 +00:00
|
|
|
case measuredValue, _ := <-measuredValueChannel:
|
|
|
|
measuredValuesCache = append(measuredValuesCache, measuredValue)
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-09-04 11:37:50 +00:00
|
|
|
|
|
|
|
func checkDeviceInDatabase(ctx context.Context, device *types.Device, database db.Database) {
|
|
|
|
_, err := database.SelectDeviceByID(ctx, device.DeviceID)
|
|
|
|
if err != nil {
|
|
|
|
flogger.Debug("It's seems the current device is not registered in the database. Register the device now")
|
|
|
|
err2 := database.InsertDevices(ctx, []*types.Device{device})
|
|
|
|
if err2 != nil {
|
|
|
|
flogger.Fatal("Can not register device into database: %v", err2)
|
|
|
|
}
|
|
|
|
flogger.Debug("Device successfully registered into the database")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
flogger.Debug("Device already registered into the database")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSensorsInDatabase(ctx context.Context, sensors []*types.Sensor, database db.Database) {
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
_, err := database.SelectSensorByID(ctx, sensor.SensorID)
|
|
|
|
if err != nil {
|
|
|
|
flogger.Debug("It's seems the sensor %v is not registered in the database. Register the sensor now", sensor.SensorName)
|
|
|
|
err2 := database.InsertSensors(ctx, []*types.Sensor{sensor})
|
|
|
|
if err2 != nil {
|
|
|
|
flogger.Fatal("Can not register sensor %v into database: %v", sensor.SensorName, err2)
|
|
|
|
}
|
|
|
|
flogger.Debug("Sensor %v successfully registered into the database", sensor.SensorName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
flogger.Debug("Sensor %v is already registered into the database", sensor.SensorName)
|
|
|
|
}
|
|
|
|
}
|