feat(pkg): add logger and round direct over sensor interface

This commit is contained in:
Markus Pesch 2019-06-18 23:02:11 +02:00
parent 2941f7a527
commit 088ed3b5f7
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
12 changed files with 192 additions and 91 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/daemon"
"github.com/go-flucky/flucky/pkg/logger"
"github.com/spf13/cobra"
)
@ -29,10 +30,9 @@ var daemonCmd = &cobra.Command{
log.Fatalf("Can not parse clean cache interval into duration time: %v", err)
}
err = daemon.Start(cnf, duration, compression, round)
if err != nil {
log.Fatalln(err)
}
logger := logger.NewDefaultLogger(logger.LogLevelDebug, false)
daemon.Start(cnf, duration, compression, round, logger)
},
}

View File

@ -2,26 +2,28 @@ package daemon
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/internal/collect"
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
"github.com/go-flucky/flucky/pkg/logfile"
"github.com/go-flucky/flucky/pkg/logger"
"github.com/go-flucky/flucky/pkg/rgbled"
"github.com/go-flucky/flucky/pkg/sensor"
"github.com/go-flucky/flucky/pkg/types"
)
// Start the daemon
func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compression bool, round float64) error {
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) {
ticker := time.Tick(cleanCacheIntervall)
// Info
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
logger.Info("Use compression: %v", compression)
logger.Info("Round values: %v", round)
ticker := time.Tick(cleanCacheInterval)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
@ -34,66 +36,63 @@ func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compres
childContext, cancel := context.WithCancel(ctx)
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), round, temperatureChannel, errorChannel)
temperatures := make([]*types.Temperature, 0)
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
err := rgbled.Green(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on blue info light: %v", err)
logger.Error("Can not turn on green info light: %v", err)
}
for {
select {
case err, _ := <-errorChannel:
logger.Error("%v", err)
case <-ticker:
err := rgbled.Blue(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on yellow info light: %v", err)
logger.Error("Can not turn on blue info light: %v", err)
}
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
if err != nil {
cancel()
return fmt.Errorf("Can not save temperatures: %v", err)
logger.Fatal("Can not save temperatures: %v", err)
}
temperatures = make([]*types.Temperature, 0)
err = rgbled.Green(rgbLEDs)
if err != nil {
cancel()
return fmt.Errorf("Can not turn on green info light: %v", err)
logger.Error("Can not turn on green info light: %v", err)
}
case temperature, more := <-temperatureChannel:
if more {
case temperature, _ := <-temperatureChannel:
temperatures = append(temperatures, temperature)
continue
}
case killSignal := <-interrupt:
log.Printf("Daemon was interruped by system signal %v\n", killSignal)
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
cancel()
err := rgbled.Red(rgbLEDs)
if err != nil {
return fmt.Errorf("Can not turn on info light: %v", err)
logger.Error("Can not turn on red info light: %v", err)
}
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
log.Println(prittyprint.FormatErrors(errors))
logger.Warn("Save remaining temperature data from the cache")
if compression {
temperatures = logfile.CompressTemperature(temperatures)
}
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
if err != nil {
return fmt.Errorf("Can not save temperatures: %v", err)
logger.Fatal("Can not save temperatures: %v", err)
}
return nil
return
}
}
}

View File

@ -181,13 +181,12 @@ func WriteTemperatures(temperatures []*types.Temperature, temperatureLogfile str
// custom writer. Compression can be enabled over a bolean parameter
func WriteTemperaturesCustom(temperatures []*types.Temperature, w io.Writer, compression bool) error {
// CompressTemperature
writeCreationDate(temperatures)
if compression {
temperatures = CompressTemperature(temperatures)
}
writeCreationDate(temperatures)
jsonEncoder := json.NewEncoder(w)
jsonEncoder.SetIndent("", " ")
err := jsonEncoder.Encode(temperatures)

70
pkg/logger/default.go Normal file
View File

@ -0,0 +1,70 @@
package logger
import (
"fmt"
"io"
"io/ioutil"
"os"
"sync"
"time"
)
type defaultLogger struct {
logLevel LogLevel
mutex *sync.Mutex
stdout io.Writer
stderr io.Writer
}
func (dl *defaultLogger) Debug(f string, v ...interface{}) {
dl.log(LogLevelDebug, f, v...)
}
func (dl *defaultLogger) Info(f string, v ...interface{}) {
dl.log(LogLevelInfo, f, v...)
}
func (dl *defaultLogger) Warn(f string, v ...interface{}) {
dl.log(LogLevelWarn, f, v...)
}
func (dl *defaultLogger) Error(f string, v ...interface{}) {
dl.log(LogLevelError, f, v...)
}
func (dl *defaultLogger) Fatal(f string, v ...interface{}) {
dl.log(LogLevelFatal, f, v...)
os.Exit(1)
}
func (dl *defaultLogger) log(ll LogLevel, f string, v ...interface{}) {
layout := "2006/01/02 15:04:05"
dw := dl.stdout
switch dl.logLevel {
case LogLevelError:
dw = dl.stderr
case LogLevelFatal:
dw = dl.stderr
}
dl.mutex.Lock()
fmt.Fprintf(dw, "%v %v: %v \n", time.Now().Format(layout), ll, fmt.Sprintf(f, v...))
dl.mutex.Unlock()
}
func NewDefaultLogger(logLevel LogLevel, time bool) *defaultLogger {
return &defaultLogger{
logLevel: logLevel,
mutex: new(sync.Mutex),
stdout: os.Stdout,
stderr: os.Stderr,
}
}
func NewSilentLogger() *defaultLogger {
return &defaultLogger{
logLevel: LogLevelDebug,
mutex: new(sync.Mutex),
stdout: ioutil.Discard,
stderr: ioutil.Discard,
}
}

9
pkg/logger/interface.go Normal file
View File

@ -0,0 +1,9 @@
package logger
type Logger interface {
Debug(string, ...interface{})
Info(string, ...interface{})
Warn(string, ...interface{})
Error(string, ...interface{})
Fatal(string, ...interface{})
}

11
pkg/logger/loglevel.go Normal file
View File

@ -0,0 +1,11 @@
package logger
type LogLevel string
const (
LogLevelDebug LogLevel = "DEBUG"
LogLevelInfo LogLevel = "INFO"
LogLevelWarn LogLevel = "WARN"
LogLevelError LogLevel = "ERROR"
LogLevelFatal LogLevel = "FATAL"
)

View File

@ -3,6 +3,7 @@ package sensor
import (
"context"
"fmt"
"math"
"sync"
"time"
@ -22,7 +23,7 @@ func (s *DHT11) GetSensorModel() types.SensorModel {
}
// ReadHumidity measure the humidity
func (s *DHT11) ReadHumidity() (*types.Humidity, error) {
func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -43,6 +44,10 @@ func (s *DHT11) ReadHumidity() (*types.Humidity, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
if round != 0 {
humidityValue = math.Round(humidityValue/round) * round
}
humidity := &types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: humidityValue,
@ -55,12 +60,12 @@ func (s *DHT11) ReadHumidity() (*types.Humidity, error) {
}
// ReadHumidityWriteIntoChannel and write values into a channel
func (s *DHT11) ReadHumidityWriteIntoChannel(humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT11) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
humidity, err := s.ReadHumidity()
humidity, err := s.ReadHumidity(round)
if err != nil {
errorChannel <- err
return
@ -69,20 +74,20 @@ func (s *DHT11) ReadHumidityWriteIntoChannel(humidityChannel chan<- *types.Humid
}
// ReadHumidityContinously into a channel until context closed
func (s *DHT11) ReadHumidityContinously(ctx context.Context, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, nil)
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
}
}
}
// ReadTemperature measure the temperature
func (s *DHT11) ReadTemperature() (*types.Temperature, error) {
func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -103,6 +108,10 @@ func (s *DHT11) ReadTemperature() (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
}
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
@ -115,12 +124,12 @@ func (s *DHT11) ReadTemperature() (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT11) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT11) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature()
temperature, err := s.ReadTemperature(round)
if err != nil {
errorChannel <- err
return
@ -129,14 +138,14 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -3,6 +3,7 @@ package sensor
import (
"context"
"fmt"
"math"
"sync"
"time"
@ -22,7 +23,7 @@ func (s *DHT22) GetSensorModel() types.SensorModel {
}
// ReadHumidity measure the humidity
func (s *DHT22) ReadHumidity() (*types.Humidity, error) {
func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -43,6 +44,10 @@ func (s *DHT22) ReadHumidity() (*types.Humidity, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
if round != 0 {
humidityValue = math.Round(humidityValue/round) * round
}
humidity := &types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: humidityValue,
@ -55,12 +60,12 @@ func (s *DHT22) ReadHumidity() (*types.Humidity, error) {
}
// ReadHumidityWriteIntoChannel and write values into a channel
func (s *DHT22) ReadHumidityWriteIntoChannel(humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT22) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
humidity, err := s.ReadHumidity()
humidity, err := s.ReadHumidity(round)
if err != nil {
errorChannel <- err
return
@ -69,20 +74,20 @@ func (s *DHT22) ReadHumidityWriteIntoChannel(humidityChannel chan<- *types.Humid
}
// ReadHumidityContinously into a channel until context closed
func (s *DHT22) ReadHumidityContinously(ctx context.Context, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, nil)
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
}
}
}
// ReadTemperature measure the temperature
func (s *DHT22) ReadTemperature() (*types.Temperature, error) {
func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -103,6 +108,11 @@ func (s *DHT22) ReadTemperature() (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
}
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
@ -115,12 +125,12 @@ func (s *DHT22) ReadTemperature() (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT22) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature()
temperature, err := s.ReadTemperature(round)
if err != nil {
errorChannel <- err
return
@ -129,14 +139,14 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"math"
"path/filepath"
"strconv"
"strings"
@ -30,7 +31,7 @@ func (s *DS18B20) GetSensor() *types.Sensor {
}
// ReadTemperature measure the temperature
func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
if err != nil {
@ -44,14 +45,21 @@ func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
return nil, ErrReadSensor
}
celsius, err := strconv.ParseFloat(raw[i+2:len(raw)-1], 64)
c, err := strconv.ParseFloat(raw[i+2:len(raw)-1], 64)
if err != nil {
return nil, ErrParseData
}
temperatureValue := c / 1000
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
}
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: celsius / 1000,
TemperatureValue: temperatureValue,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -62,12 +70,12 @@ func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DS18B20) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DS18B20) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature()
temperature, err := s.ReadTemperature(round)
if err != nil {
errorChannel <- err
return
@ -76,14 +84,14 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *typ
}
// ReadTemperatureContinously into a channel until context closed
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -10,15 +10,15 @@ import (
// HumiditySensor is a interface to describe required functions to measure humidities
type HumiditySensor interface {
GetSensorModel() types.SensorModel
ReadHumidity() (*types.Humidity, error)
ReadHumidityWriteIntoChannel(humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup)
ReadHumidityContinously(ctx context.Context, humidityChannel chan<- *types.Humidity, errorChannel chan<- error)
ReadHumidity(round float64) (*types.Humidity, error)
ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup)
ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error)
}
// TemperatureSensor is a interface to describe required functions to measure temperatures
type TemperatureSensor interface {
GetSensorModel() types.SensorModel
ReadTemperature() (*types.Temperature, error)
ReadTemperatureWriteIntoChannel(temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
ReadTemperatureContinously(ctx context.Context, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
ReadTemperature(round float64) (*types.Temperature, error)
ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
}

View File

@ -3,7 +3,6 @@ package sensor
import (
"context"
"fmt"
"math"
"sync"
"github.com/go-flucky/flucky/pkg/internal/collect"
@ -20,7 +19,7 @@ func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.H
wg.Add(len(humiditySensors))
for _, humiditySensor := range humiditySensors {
go humiditySensor.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, wg)
go humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
}
wg.Wait()
@ -32,31 +31,25 @@ func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.H
humidities := collect.Humidities(humidityChannel)
if round != 0 {
for _, humidity := range humidities {
humidity.HumidityValue = math.Round(humidity.HumidityValue/round) * round
}
}
return humidities, nil
}
// ReadHumiditiesWriteIntoChannel reads the humidity values of humidity sensors and writes them into a channel
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
for _, humiditySensor := range humiditySensors {
humiditySensor.ReadHumidityWriteIntoChannel(humidityChannel, errorChannel, wg)
humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
}
}
// ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
return
default:
ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, humidityChannel, errorChannel, nil)
ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, round, humidityChannel, errorChannel, nil)
}
}
}
@ -70,7 +63,7 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, round float64) ([]
wg.Add(len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors {
go temperatureSensor.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, wg)
go temperatureSensor.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, wg)
}
wg.Wait()
@ -82,32 +75,25 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, round float64) ([]
temperatures := collect.Temperatures(temperatureChannel)
if round != 0 {
for _, temperature := range temperatures {
temperature.TemperatureValue = math.Round(temperature.TemperatureValue/round) * round
}
}
return temperatures, nil
}
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
for _, temperatureSensor := range temperatureSensors {
temperatureSensor.ReadTemperatureWriteIntoChannel(temperatureChannel, errorChannel, wg)
temperatureSensor.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, wg)
}
}
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
return
default:
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, temperatureChannel, errorChannel, nil)
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, round, temperatureChannel, errorChannel, nil)
}
}
}