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

This commit is contained in:
2019-06-18 23:02:11 +02:00
parent 2941f7a527
commit 088ed3b5f7
12 changed files with 192 additions and 91 deletions

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)
}
}
}