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-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/config"
|
2020-05-21 15:40:24 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/repository/db"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/sensor"
|
|
|
|
"github.com/volker-raschek/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
|
|
|
)
|
|
|
|
|
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
|
|
|
sensors := make([]sensor.Sensor, 0)
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
|
|
|
if !cnfSensor.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-18 13:55:43 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
sensor, err := sensor.New(cnfSensor)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sensors = append(sensors, sensor)
|
|
|
|
}
|
2020-01-11 16:24:18 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
measuredValueChannel := make(chan *types.MeasuredValue, 0)
|
|
|
|
|
|
|
|
// load storage endpoint
|
2020-05-21 15:40:24 +00:00
|
|
|
storageEndpointURL, err := url.Parse(cnf.StorageEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := db.New(storageEndpointURL, flogger)
|
2020-01-11 16:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
interruptChannel := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChannel, os.Kill, syscall.SIGTERM)
|
2019-06-16 21:15:26 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Collection
|
2019-12-07 15:53:49 +00:00
|
|
|
parentCtx := context.Background()
|
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Insert device if not exist
|
2020-05-21 15:40:24 +00:00
|
|
|
device, _ := backend.SelectDevice(parentCtx, cnf.Device.ID)
|
2020-05-03 12:04:08 +00:00
|
|
|
if device == nil {
|
2020-05-21 15:40:24 +00:00
|
|
|
if err := backend.InsertDevices(parentCtx, cnf.Device); err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Insert sensors if not exist
|
|
|
|
for _, cnfSensor := range cnf.Sensors {
|
2020-05-21 15:40:24 +00:00
|
|
|
sensor, _ := backend.SelectSensor(parentCtx, cnfSensor.ID)
|
2020-05-03 12:04:08 +00:00
|
|
|
if sensor == nil {
|
2020-05-21 15:40:24 +00:00
|
|
|
if err := backend.InsertSensors(parentCtx, cnfSensor); err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 18:42:19 +00:00
|
|
|
}
|
|
|
|
|
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 15:40:24 +00:00
|
|
|
err := backend.InsertMeasuredValues(ctx, 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 15:40:24 +00:00
|
|
|
err := backend.InsertMeasuredValues(ctx, 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
|
|
|
}
|