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-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-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-06-27 19:12:00 +00:00
|
|
|
flogger.Debug("Found sensor %v", repoSensor.GetName())
|
|
|
|
|
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-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) {
|
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-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-06-27 19:12:00 +00:00
|
|
|
case signal := <-interruptChannel:
|
2020-05-03 12:04:08 +00:00
|
|
|
cancel()
|
|
|
|
close(measuredValueChannel)
|
|
|
|
|
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
|
|
|
}
|