fix(pkg/sensor): reduce interface functions for better error handling
This commit is contained in:
@ -2,6 +2,7 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
@ -59,8 +60,49 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
||||
measuredValuesCache := make([]*types.MeasuredValue, 0)
|
||||
// measuredValuesLogfile := logfile.New(cnf.Logfile)
|
||||
|
||||
// Producer
|
||||
go sensor.ReadContinuously(ctx, cnf.GetSensors(config.ENABLED), measuredValueChannel, errorChannel)
|
||||
// Init semaphoreChannel
|
||||
semaphoreChannels := make(map[string]chan struct{})
|
||||
for _, sensor := range cnf.GetSensors(config.ENABLED) {
|
||||
semaphoreChannels[sensor.ID()] = make(chan struct{}, 1)
|
||||
}
|
||||
|
||||
// Start producers
|
||||
for _, s := range cnf.GetSensors(config.ENABLED) {
|
||||
|
||||
// start go routine for each sensor
|
||||
go func(sensor sensor.Sensor) {
|
||||
// run forever
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("Closed context: %v", ctx.Err().Error())
|
||||
return
|
||||
case <-semaphoreChannels[sensor.ID()]:
|
||||
measuredValues, err := sensor.Read()
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
for _, measmeasuredValue := range measuredValues {
|
||||
measuredValueChannel <- measmeasuredValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}(s)
|
||||
|
||||
// start ticker for each sensor
|
||||
go func(sensor sensor.Sensor) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("Closed context: %v", ctx.Err().Error())
|
||||
return
|
||||
case <-sensor.Ticker().C:
|
||||
semaphoreChannels[sensor.ID()] <- struct{}{}
|
||||
}
|
||||
}
|
||||
}(s)
|
||||
}
|
||||
|
||||
// Distributor
|
||||
//measuredValueChannels := distribute.MeasuredValues(ctx, 5, measuredValueChannel)
|
||||
@ -105,7 +147,11 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
||||
|
||||
measuredValuesCache = make([]*types.MeasuredValue, 0)
|
||||
|
||||
case measuredValue, _ := <-measuredValueChannel:
|
||||
case measuredValue, open := <-measuredValueChannel:
|
||||
if !open {
|
||||
errorChannel <- fmt.Errorf("MeasuredValue channel closed")
|
||||
cancel()
|
||||
}
|
||||
measuredValuesCache = append(measuredValuesCache, measuredValue)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user