PKGBUILD/pkg/daemon/daemon.go
Markus Pesch fb8d4dd5eb
fix: new implementation
changes:
- Remove cli
  Some cli commands are not complete tested and are deprecated.

- Daemon
  - Old version has a very bad implementation of how to verify, if the
    device or the sensors are in the database insert. The current
    implementation can be improved but this one is betten then the old
    one.
  - Remove complete the cache store implementation. Use a normal array
    and query the length and capacity to determine how the array cache
    must be cleaned.

- Type
  Remove unused types and functions
2020-05-03 14:09:22 +02:00

116 lines
2.6 KiB
Go

package daemon
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/volker-raschek/flucky/pkg/config"
"github.com/volker-raschek/flucky/pkg/sensor"
"github.com/volker-raschek/flucky/pkg/storage"
"github.com/volker-raschek/flucky/pkg/types"
"github.com/volker-raschek/go-logger/pkg/logger"
)
func Start(cnf *config.Config, flogger logger.Logger) error {
sensors := make([]sensor.Sensor, 0)
for _, cnfSensor := range cnf.Sensors {
if !cnfSensor.Enabled {
continue
}
sensor, err := sensor.New(cnfSensor)
if err != nil {
return err
}
sensors = append(sensors, sensor)
}
measuredValueChannel := make(chan *types.MeasuredValue, 0)
// load storage endpoint
storageEndpoint, err := storage.New(cnf.StorageEndpoint, flogger)
if err != nil {
return err
}
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Kill, syscall.SIGTERM)
// Collection
parentCtx := context.Background()
// Insert device if not exist
device, _ := storageEndpoint.SelectDevice(parentCtx, cnf.Device.ID)
if device == nil {
if err := storageEndpoint.InsertDevice(parentCtx, cnf.Device); err != nil {
return err
}
}
// Insert sensors if not exist
for _, cnfSensor := range cnf.Sensors {
sensor, _ := storageEndpoint.SelectSensor(parentCtx, cnfSensor.ID)
if sensor == nil {
if err := storageEndpoint.InsertSensor(parentCtx, cnfSensor); err != nil {
return err
}
}
}
ctx, cancel := context.WithCancel(parentCtx)
for _, s := range sensors {
go func(sensor sensor.Sensor) {
for {
select {
case <-ctx.Done():
return
case <-sensor.GetTicker().C:
measuredValues, err := sensor.Read()
if err != nil {
flogger.Error("%v", err)
continue
}
for _, measuredValue := range measuredValues {
measuredValueChannel <- measuredValue
}
}
}
}(s)
}
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")
err := storageEndpoint.InsertMeasuredValues(ctx, measuredValues)
if err != nil {
flogger.Error("%v", err)
}
measuredValues = make([]*types.MeasuredValue, 0, 10)
}
case <-interruptChannel:
cancel()
close(measuredValueChannel)
err := storageEndpoint.InsertMeasuredValues(ctx, measuredValues)
if err != nil {
flogger.Error("%v", err)
}
break
}
}
return nil
}