PKGBUILD/pkg/sensor/sensor.go

90 lines
3.2 KiB
Go
Raw Normal View History

package sensor
2019-02-24 21:46:36 +00:00
import (
"sync"
"github.com/volker-raschek/flucky/pkg/internal/collect"
"github.com/volker-raschek/flucky/pkg/internal/prittyprint"
2019-05-12 09:57:53 +00:00
"github.com/volker-raschek/flucky/pkg/types"
2019-02-24 21:46:36 +00:00
)
// 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)
2019-02-24 21:46:36 +00:00
errorList := collect.Errors(errorChannel)
if len(errorList) != 0 {
return nil, prittyprint.FormatErrors(errorList)
}
2019-02-24 21:46:36 +00:00
humidityList := collect.Humidities(humidityChannel)
return humidityList, nil
}
2019-02-24 21:46:36 +00:00
// 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) {
2019-02-24 21:46:36 +00:00
wg := new(sync.WaitGroup)
wg.Add(len(humiditySensors))
for _, humiditySensor := range humiditySensors {
go func(hs HumiditySensor) {
defer wg.Done()
humidity, err := hs.ReadHumidity()
if err != nil {
errorChannel <- err
return
2019-02-24 21:46:36 +00:00
}
humidityChannel <- humidity
}(humiditySensor)
}
wg.Wait()
}
2019-02-24 21:46:36 +00:00
// 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)
2019-02-24 21:46:36 +00:00
}
}
2019-02-24 21:46:36 +00:00
// 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)
2019-02-24 21:46:36 +00:00
errorList := collect.Errors(errorChannel)
if len(errorList) != 0 {
return nil, prittyprint.FormatErrors(errorList)
}
temperatureList := collect.Temperatures(temperatureChannel)
return temperatureList, nil
2018-11-28 17:07:20 +00:00
}
// 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) {
2019-02-24 21:46:36 +00:00
wg := new(sync.WaitGroup)
wg.Add(len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors {
go func(ts TemperatureSensor) {
defer wg.Done()
temperature, err := ts.ReadTemperature()
if err != nil {
errorChannel <- err
return
2019-02-24 21:46:36 +00:00
}
temperatureChannel <- temperature
}(temperatureSensor)
}
wg.Wait()
}
// 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) {
2019-02-24 21:46:36 +00:00
for {
ReadTemperaturesIntoChannel(temperatureSensors, temperatureChannel, errorChannel)
2019-02-24 21:46:36 +00:00
}
}