2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2019-03-04 10:05:02 +00:00
|
|
|
"fmt"
|
2019-06-18 21:02:11 +00:00
|
|
|
"math"
|
2019-06-17 21:37:48 +00:00
|
|
|
"sync"
|
2019-03-04 10:05:02 +00:00
|
|
|
"time"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-15 15:07:50 +00:00
|
|
|
"github.com/go-flucky/go-dht"
|
2019-03-04 10:05:02 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-02-22 12:08:58 +00:00
|
|
|
)
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// DHT22 is a sensor to measure humidity and temperature.
|
|
|
|
type DHT22 struct {
|
2019-02-22 12:08:58 +00:00
|
|
|
*types.Sensor
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// GetSensorModel returns the sensor model
|
|
|
|
func (s *DHT22) GetSensorModel() types.SensorModel {
|
|
|
|
return s.Sensor.SensorModel
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadHumidity measure the humidity
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
|
2019-03-04 10:05:02 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
if round != 0 {
|
|
|
|
humidityValue = math.Round(humidityValue/round) * round
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:05:02 +00:00
|
|
|
humidity := &types.Humidity{
|
2019-06-13 20:22:12 +00:00
|
|
|
HumidityID: uuid.NewV4().String(),
|
|
|
|
HumidityValue: humidityValue,
|
|
|
|
HumidityFromDate: time.Now(),
|
|
|
|
HumidityTillDate: time.Now(),
|
|
|
|
SensorID: s.SensorID,
|
2019-03-04 10:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humidity, nil
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
// ReadHumidityWriteIntoChannel and write values into a channel
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-06-17 21:37:48 +00:00
|
|
|
if wg != nil {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
humidity, err := s.ReadHumidity(round)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
humidityChannel <- humidity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadHumidityContinously into a channel until context closed
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
2019-06-17 21:37:48 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
2019-06-18 21:02:11 +00:00
|
|
|
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// ReadTemperature measure the temperature
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
|
2019-03-04 10:05:02 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
// round
|
|
|
|
if round != 0 {
|
|
|
|
temperatureValue = math.Round(temperatureValue/round) * round
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:05:02 +00:00
|
|
|
temperature := &types.Temperature{
|
2019-06-13 20:22:12 +00:00
|
|
|
TemperatureID: uuid.NewV4().String(),
|
|
|
|
TemperatureValue: temperatureValue,
|
|
|
|
TemperatureFromDate: time.Now(),
|
|
|
|
TemperatureTillDate: time.Now(),
|
|
|
|
SensorID: s.SensorID,
|
2019-03-04 10:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return temperature, nil
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
2019-06-17 21:37:48 +00:00
|
|
|
|
|
|
|
// ReadTemperatureWriteIntoChannel and write values into a channel
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-06-17 21:37:48 +00:00
|
|
|
if wg != nil {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
temperature, err := s.ReadTemperature(round)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
temperatureChannel <- temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTemperatureContinously into a channel until context closed
|
2019-06-18 21:02:11 +00:00
|
|
|
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
2019-06-17 21:37:48 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
2019-06-18 21:02:11 +00:00
|
|
|
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|