fix: breaking changes

changes:
- remove remote operations
- add function to write measured values into a channel
- add get humidity sensors from config
- add get temperature sensors from config
- remove FileLogger
- exclude some functions from pkf into internal
This commit is contained in:
2019-06-13 21:25:32 +02:00
parent 98e5f3a536
commit 5220eac16b
39 changed files with 481 additions and 1376 deletions

View File

@ -3,106 +3,87 @@ package sensor
import (
"sync"
"github.com/volker-raschek/flucky/pkg/internal/errutils"
"github.com/volker-raschek/flucky/pkg/internal/collect"
"github.com/volker-raschek/flucky/pkg/internal/prittyprint"
"github.com/volker-raschek/flucky/pkg/types"
)
type HumiditySensor interface {
ReadHumidity() (*types.Humidity, error)
}
type TemperatureSensor interface {
ReadTemperature() (*types.Temperature, error)
}
// ReadHumidities returns a list of measured humidities by humidity sensors
func ReadHumidities(humiditySensors []HumiditySensor) ([]*types.Humidity, error) {
errorChannel := make(chan error, len(humiditySensors))
humidityChannel := make(chan *types.Humidity, len(humiditySensors))
ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel)
errorList := collect.Errors(errorChannel)
if len(errorList) != 0 {
return nil, prittyprint.FormatErrors(errorList)
}
humidityList := collect.Humidities(humidityChannel)
return humidityList, nil
}
// ReadHumiditiesIntoChannel reads the humidity values of humidity sensors and writes them into a channel
func ReadHumiditiesIntoChannel(humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
wg := new(sync.WaitGroup)
wg.Add(len(humiditySensors))
errChannel := make(chan error, len(humiditySensors))
humidityChannel := make(chan *types.Humidity, len(humiditySensors))
for _, humiditySensor := range humiditySensors {
go func(hs HumiditySensor) {
defer wg.Done()
humidity, err := hs.ReadHumidity()
if err != nil {
errChannel <- err
humidityChannel <- nil
errorChannel <- err
return
}
errChannel <- nil
humidityChannel <- humidity
}(humiditySensor)
}
wg.Wait()
errorList := errutils.CollectErrors(errChannel)
if err := errutils.FormatErrors(errorList); err != nil {
return nil, err
}
humidityList := collectHumidities(humidityChannel)
return humidityList, nil
}
// ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel
func ReadHumiditiesContinuously(humiditySensors []HumiditySensor, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
for {
ReadHumiditiesIntoChannel(humiditySensors, humidityChannel, errorChannel)
}
}
// ReadTemperatures returns a list of measured temperatures by temperature sensors
func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) {
errorChannel := make(chan error, len(temperatureSensors))
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel)
errorList := collect.Errors(errorChannel)
if len(errorList) != 0 {
return nil, prittyprint.FormatErrors(errorList)
}
temperatureList := collect.Temperatures(temperatureChannel)
return temperatureList, nil
}
// ReadTemperaturesIntoChannel reads the temperature values of temperature sensors and writes them into a channel
func ReadTemperaturesIntoChannel(temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
wg := new(sync.WaitGroup)
wg.Add(len(temperatureSensors))
errChannel := make(chan error, len(temperatureSensors))
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors {
go func(ts TemperatureSensor) {
defer wg.Done()
temperature, err := ts.ReadTemperature()
if err != nil {
errChannel <- err
errorChannel <- err
return
}
temperatureChannel <- temperature
}(temperatureSensor)
}
wg.Wait()
errorList := errutils.CollectErrors(errChannel)
if err := errutils.FormatErrors(errorList); err != nil {
return nil, err
}
temperatureList := collectTemperatures(temperatureChannel)
return temperatureList, nil
}
func collectHumidities(humChan <-chan *types.Humidity) []*types.Humidity {
humidityList := make([]*types.Humidity, 0)
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
func ReadTemperaturesContinuously(temperatureSensors []TemperatureSensor, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case hum, more := <-humChan:
if more {
humidityList = append(humidityList, hum)
continue
}
return nil
default:
return humidityList
}
}
}
func collectTemperatures(tempChan <-chan *types.Temperature) []*types.Temperature {
temperatureList := make([]*types.Temperature, 0)
for {
select {
case temp, more := <-tempChan:
if more {
temperatureList = append(temperatureList, temp)
continue
}
return nil
default:
return temperatureList
}
ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel)
}
}