feat(pkg): add logger and round direct over sensor interface
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user