152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
package sensor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
"github.com/go-flucky/go-dht"
|
|
uuid "github.com/satori/go.uuid"
|
|
)
|
|
|
|
// DHT11 is a sensor to measure humidity and temperature.
|
|
type DHT11 struct {
|
|
*types.Sensor
|
|
}
|
|
|
|
// GetSensorModel returns the sensor model
|
|
func (s *DHT11) GetSensorModel() types.SensorModel {
|
|
return s.Sensor.SensorModel
|
|
}
|
|
|
|
// ReadHumidity measure the humidity
|
|
func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
|
|
err := dht.HostInit()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("HostInit error: %v", err)
|
|
}
|
|
|
|
gpio, err := types.GPIOToString(*s.GPIONumber)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewDHT error: %v", err)
|
|
}
|
|
|
|
humidityValue, _, err := dht.Read()
|
|
if err != nil {
|
|
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,
|
|
HumidityFromDate: time.Now(),
|
|
HumidityTillDate: time.Now(),
|
|
SensorID: s.SensorID,
|
|
}
|
|
|
|
return humidity, nil
|
|
}
|
|
|
|
// ReadHumidityWriteIntoChannel and write values into a channel
|
|
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(round)
|
|
if err != nil {
|
|
errorChannel <- err
|
|
return
|
|
}
|
|
humidityChannel <- humidity
|
|
}
|
|
|
|
// ReadHumidityContinously into a channel until context closed
|
|
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(round, humidityChannel, errorChannel, nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ReadTemperature measure the temperature
|
|
func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
|
|
err := dht.HostInit()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("HostInit error: %v", err)
|
|
}
|
|
|
|
gpio, err := types.GPIOToString(*s.GPIONumber)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewDHT error: %v", err)
|
|
}
|
|
|
|
_, temperatureValue, err := dht.Read()
|
|
if err != nil {
|
|
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,
|
|
TemperatureFromDate: time.Now(),
|
|
TemperatureTillDate: time.Now(),
|
|
SensorID: s.SensorID,
|
|
}
|
|
|
|
return temperature, nil
|
|
}
|
|
|
|
// ReadTemperatureWriteIntoChannel and write values into a channel
|
|
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(round)
|
|
if err != nil {
|
|
errorChannel <- err
|
|
return
|
|
}
|
|
temperatureChannel <- temperature
|
|
}
|
|
|
|
// ReadTemperatureContinously into a channel until context closed
|
|
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(round, temperatureChannel, errorChannel, nil)
|
|
}
|
|
}
|
|
}
|