2019-06-13 19:25:32 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2020-05-21 15:40:24 +00:00
|
|
|
"net/url"
|
2019-06-15 13:45:35 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
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-05-03 12:04:08 +00:00
|
|
|
func Start(cnf *config.Config, flogger logger.Logger) error {
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
measuredValueChannel := make(chan *types.MeasuredValue, 0)
|
|
|
|
|
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-05-21 18:34:38 +00:00
|
|
|
repoSensors, err := repo.GetSensors()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
if !repoSensor.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 15:53:49 +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)
|
|
|
|
signal.Notify(interruptChannel, os.Kill, syscall.SIGTERM)
|
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-05-03 12:04:08 +00:00
|
|
|
for _, s := range sensors {
|
2020-01-10 18:42:19 +00:00
|
|
|
go func(sensor sensor.Sensor) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2020-05-03 12:04:08 +00:00
|
|
|
case <-sensor.GetTicker().C:
|
2020-01-10 18:42:19 +00:00
|
|
|
measuredValues, err := sensor.Read()
|
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
flogger.Error("%v", err)
|
|
|
|
continue
|
2020-01-10 18:42:19 +00:00
|
|
|
}
|
2020-01-11 16:24:18 +00:00
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValueChannel <- measuredValue
|
2020-01-10 18:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
2019-06-15 13:45:35 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
measuredValues := make([]*types.MeasuredValue, 0, 10)
|
|
|
|
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) {
|
|
|
|
flogger.Debug("Flush cache")
|
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-05-03 12:04:08 +00:00
|
|
|
measuredValues = make([]*types.MeasuredValue, 0, 10)
|
2020-01-11 16:24:18 +00:00
|
|
|
}
|
2019-09-04 11:37:50 +00:00
|
|
|
|
2020-01-11 16:24:18 +00:00
|
|
|
case <-interruptChannel:
|
2020-05-03 12:04:08 +00:00
|
|
|
cancel()
|
|
|
|
close(measuredValueChannel)
|
|
|
|
|
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
|
|
|
|
|
|
|
break
|
2019-09-04 11:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 12:04:08 +00:00
|
|
|
}
|