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

@ -9,11 +9,18 @@ import (
"github.com/volker-raschek/flucky/pkg/types"
)
type DHT11Sensor struct {
// DHT11 is a sensor to measure humidity and temperature.
type DHT11 struct {
*types.Sensor
}
func (s *DHT11Sensor) ReadHumidity() (*types.Humidity, error) {
// GetSensorModel returns the sensor model
func (s *DHT11) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel
}
// ReadHumidity measure the humidity
func (s *DHT11) ReadHumidity() (*types.Humidity, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -44,7 +51,8 @@ func (s *DHT11Sensor) ReadHumidity() (*types.Humidity, error) {
return humidity, nil
}
func (s *DHT11Sensor) ReadTemperature() (*types.Temperature, error) {
// ReadTemperature measure the temperature
func (s *DHT11) ReadTemperature() (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)

View File

@ -9,11 +9,18 @@ import (
"github.com/volker-raschek/flucky/pkg/types"
)
type DHT22Sensor struct {
// DHT22 is a sensor to measure humidity and temperature.
type DHT22 struct {
*types.Sensor
}
func (s *DHT22Sensor) ReadHumidity() (*types.Humidity, error) {
// GetSensorModel returns the sensor model
func (s *DHT22) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel
}
// ReadHumidity measure the humidity
func (s *DHT22) ReadHumidity() (*types.Humidity, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -44,7 +51,8 @@ func (s *DHT22Sensor) ReadHumidity() (*types.Humidity, error) {
return humidity, nil
}
func (s *DHT22Sensor) ReadTemperature() (*types.Temperature, error) {
// ReadTemperature measure the temperature
func (s *DHT22) ReadTemperature() (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)

View File

@ -9,10 +9,22 @@ import (
"github.com/yryz/ds18b20"
)
// DS18B20 is a sensor to measure humidity and temperature.
type DS18B20 struct {
*types.Sensor
}
// GetSensorModel returns the sensor model
func (s *DS18B20) GetSensorModel() types.SensorModel {
return s.Sensor.SensorModel
}
// GetSensor return the sensor struct
func (s *DS18B20) GetSensor() *types.Sensor {
return s.Sensor
}
// ReadTemperature measure the temperature
func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
t, err := ds18b20.Temperature(*s.WireID)

15
pkg/sensor/interfaces.go Normal file
View File

@ -0,0 +1,15 @@
package sensor
import "github.com/volker-raschek/flucky/pkg/types"
// HumiditySensor is a interface to describe required functions to measure humidities
type HumiditySensor interface {
GetSensorModel() types.SensorModel
ReadHumidity() (*types.Humidity, error)
}
// TemperatureSensor is a interface to describe required functions to measure temperatures
type TemperatureSensor interface {
GetSensorModel() types.SensorModel
ReadTemperature() (*types.Temperature, error)
}

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