fix: cli temperature read

changes:
- fix: read temperature values without daemon
  Add subcommand to read temperature values without starting the daemon

- fix: implement measured value types
  Replace measured value types with constants

- fix: add sensor pipelines
  Add functions which returns a channel with measured values

- fix: filter measured values from a channel
  Add functions to filter measured values by sensor id or measured
  value types.
This commit is contained in:
2020-09-21 19:36:42 +02:00
parent 7cbd80c726
commit 3a090d190e
17 changed files with 481 additions and 77 deletions

View File

@ -16,8 +16,6 @@ import (
func Start(cnf *config.Config, flogger logger.Logger) error {
measuredValueChannel := make(chan *types.MeasuredValue, 0)
// load data source name (dsn)
dsnURL, err := url.Parse(cnf.DSN)
if err != nil {
@ -69,7 +67,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
continue
}
flogger.Debug("Found sensor %v", repoSensor.GetName())
flogger.Debug("Found sensor %v", repoSensor.Name)
sensor, err := sensor.New(repoSensor)
if err != nil {
@ -85,25 +83,23 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
parentCtx := context.Background()
ctx, cancel := context.WithCancel(parentCtx)
for _, s := range sensors {
go func(sensor sensor.Sensor) {
for {
select {
case <-ctx.Done():
measuredValueChannel, errorChannel := sensor.ReadTickingPipeline(ctx, sensors...)
go func() {
for {
select {
case <-ctx.Done():
return
case err, open := <-errorChannel:
if !open {
return
case <-sensor.GetTicker().C:
measuredValues, err := sensor.Read()
if err != nil {
flogger.Error("%v", err)
continue
}
for _, measuredValue := range measuredValues {
measuredValueChannel <- measuredValue
}
}
if err != nil {
flogger.Error("%v", err)
}
}
}(s)
}
}
}()
measuredValues := make([]*types.MeasuredValue, 0, 10)
for {
@ -123,7 +119,6 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
case signal := <-interruptChannel:
cancel()
close(measuredValueChannel)
flogger.Info("Stopping daemon: Received process signal %v", signal.String())