2019-06-13 19:25:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2020-06-27 19:12:00 +00:00
|
|
|
"fmt"
|
2020-05-21 15:40:24 +00:00
|
|
|
"net/url"
|
2019-06-15 13:45:35 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2020-06-10 19:13:05 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/repository"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/sensor"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
|
|
|
"git.cryptic.systems/volker.raschek/go-logger"
|
2019-06-13 19:25:32 +00:00
|
|
|
)
|
|
|
|
|
2020-10-07 21:59:27 +00:00
|
|
|
func Start(cnf *config.Config, cachedEntries uint, flogger logger.Logger) error {
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2020-06-01 10:41:48 +00:00
|
|
|
// load data source name (dsn)
|
|
|
|
dsnURL, err := url.Parse(cnf.DSN)
|
2020-05-21 15:40:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:41:48 +00:00
|
|
|
repo, err := repository.New(dsnURL, flogger)
|
2020-01-11 16:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:12:00 +00:00
|
|
|
// Add
|
2020-09-07 17:12:30 +00:00
|
|
|
repoDevice, err := repo.GetDevice(cnf.DeviceID)
|
2020-06-27 19:12:00 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case repoDevice == nil:
|
2020-09-07 17:12:30 +00:00
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.AddDevices(&types.Device{
|
|
|
|
ID: cnf.DeviceID,
|
|
|
|
Name: hostname,
|
|
|
|
})
|
2020-06-27 19:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-07 17:12:30 +00:00
|
|
|
repoDevice, err = repo.GetDevice(cnf.DeviceID)
|
2020-06-27 19:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoSensors, err := repo.GetSensorsByDeviceID(repoDevice.ID)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
2020-05-21 18:34:38 +00:00
|
|
|
return err
|
2020-06-27 19:12:00 +00:00
|
|
|
case repoSensors == nil, len(repoSensors) <= 0:
|
|
|
|
return fmt.Errorf("No sensors found")
|
2020-05-21 18:34:38 +00:00
|
|
|
}
|
2019-06-16 21:15:26 +00:00
|
|
|
|
2020-05-21 18:34:38 +00:00
|
|
|
sensors := make([]sensor.Sensor, 0)
|
|
|
|
for _, repoSensor := range repoSensors {
|
2020-06-27 19:12:00 +00:00
|
|
|
if !repoSensor.Enabled || repoSensor.DeviceID != repoDevice.ID {
|
2020-05-21 18:34:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 15:53:49 +00:00
|
|
|
|
2020-09-21 17:36:42 +00:00
|
|
|
flogger.Debug("Found sensor %v", repoSensor.Name)
|
2020-06-27 19:12:00 +00:00
|
|
|
|
2020-05-21 18:34:38 +00:00
|
|
|
sensor, err := sensor.New(repoSensor)
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-21 18:34:38 +00:00
|
|
|
sensors = append(sensors, sensor)
|
2020-05-03 12:04:08 +00:00
|
|
|
}
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2020-05-21 18:34:38 +00:00
|
|
|
interruptChannel := make(chan os.Signal, 1)
|
2020-06-27 19:12:00 +00:00
|
|
|
signal.Notify(interruptChannel, os.Interrupt, os.Kill)
|
2020-01-10 18:42:19 +00:00
|
|
|
|
2020-05-21 18:34:38 +00:00
|
|
|
// Collection
|
|
|
|
parentCtx := context.Background()
|
2020-05-03 12:04:08 +00:00
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
2020-01-10 18:42:19 +00:00
|
|
|
|
2020-09-21 17:36:42 +00:00
|
|
|
measuredValueChannel, errorChannel := sensor.ReadTickingPipeline(ctx, sensors...)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case err, open := <-errorChannel:
|
|
|
|
if !open {
|
2020-01-10 18:42:19 +00:00
|
|
|
return
|
2020-09-21 17:36:42 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
flogger.Error("%v", err)
|
2020-01-10 18:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 17:36:42 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2020-10-07 21:59:27 +00:00
|
|
|
measuredValues := make([]*types.MeasuredValue, 0, cachedEntries)
|
2020-05-03 12:04:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case measuredValue := <-measuredValueChannel:
|
|
|
|
flogger.Debug("%v\t%v\t%v", measuredValue.ID, measuredValue.ValueType, measuredValue.Value)
|
|
|
|
measuredValues = append(measuredValues, measuredValue)
|
|
|
|
|
|
|
|
if cap(measuredValues) == len(measuredValues) {
|
2020-06-27 19:12:00 +00:00
|
|
|
flogger.Debug("Flush cache with %v values", len(measuredValues))
|
2020-05-21 18:07:32 +00:00
|
|
|
err := repo.AddMeasuredValues(measuredValues...)
|
2020-05-03 12:04:08 +00:00
|
|
|
if err != nil {
|
|
|
|
flogger.Error("%v", err)
|
2020-01-11 16:24:18 +00:00
|
|
|
}
|
2020-10-07 21:59:27 +00:00
|
|
|
measuredValues = make([]*types.MeasuredValue, 0, cachedEntries)
|
2020-01-11 16:24:18 +00:00
|
|
|
}
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2020-06-27 19:12:00 +00:00
|
|
|
case signal := <-interruptChannel:
|
2020-05-03 12:04:08 +00:00
|
|
|
cancel()
|
|
|
|
|
2020-06-27 19:12:00 +00:00
|
|
|
flogger.Info("Stopping daemon: Received process signal %v", signal.String())
|
|
|
|
|
|
|
|
flogger.Debug("Flush cache with %v remaining values", len(measuredValues))
|
|
|
|
|
2020-05-21 18:07:32 +00:00
|
|
|
err := repo.AddMeasuredValues(measuredValues...)
|
2020-01-18 13:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
flogger.Error("%v", err)
|
|
|
|
}
|
2020-05-03 12:04:08 +00:00
|
|
|
|
2020-06-27 19:12:00 +00:00
|
|
|
flogger.Debug("Close repository")
|
|
|
|
err = repo.Close()
|
|
|
|
if err != nil {
|
|
|
|
flogger.Error("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-09-04 11:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 12:04:08 +00:00
|
|
|
}
|