2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2019-03-03 18:13:37 +00:00
|
|
|
"fmt"
|
2019-06-17 21:37:48 +00:00
|
|
|
"sync"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/internal/format"
|
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-03 18:13:37 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-02-22 12:08:58 +00:00
|
|
|
)
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// DHT11 is a sensor to measure humidity and temperature.
|
|
|
|
type DHT11 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 *DHT11) GetSensorModel() types.SensorModel {
|
|
|
|
return s.Sensor.SensorModel
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
// Read measured values
|
2019-07-02 20:33:01 +00:00
|
|
|
func (s *DHT11) Read() ([]*types.MeasuredValue, error) {
|
2019-06-25 20:22:34 +00:00
|
|
|
|
2019-03-03 18:13:37 +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)
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
humidityValue, temperatureValue, err := dht.Read()
|
2019-03-03 18:13:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Read error: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := []*types.MeasuredValue{
|
|
|
|
&types.MeasuredValue{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(humidityValue),
|
|
|
|
ValueType: types.MeasuredValueTypeHumidity,
|
2019-08-20 19:37:45 +00:00
|
|
|
FromDate: format.FormatedTime(),
|
|
|
|
TillDate: format.FormatedTime(),
|
2019-07-02 20:33:01 +00:00
|
|
|
SensorID: s.SensorID,
|
2019-06-25 20:22:34 +00:00
|
|
|
},
|
2019-07-02 20:33:01 +00:00
|
|
|
&types.MeasuredValue{
|
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(temperatureValue),
|
|
|
|
ValueType: types.MeasuredValueTypeTemperature,
|
2019-08-20 19:37:45 +00:00
|
|
|
FromDate: format.FormatedTime(),
|
|
|
|
TillDate: format.FormatedTime(),
|
2019-07-02 20:33:01 +00:00
|
|
|
SensorID: s.SensorID,
|
2019-06-25 20:22:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
2019-02-24 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
// ReadChannel reads the measured values from the sensor and writes them to a
|
|
|
|
// channel.
|
2019-07-02 20:33:01 +00:00
|
|
|
func (s *DHT11) ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-06-17 21:37:48 +00:00
|
|
|
if wg != nil {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
measuredValues, err := s.Read()
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:31:40 +00:00
|
|
|
measuredValuesChannel <- measuredValues
|
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
// ReadContinously reads the measured values continously from the sensor and
|
|
|
|
// writes them to a channel.
|
2019-07-02 20:33:01 +00:00
|
|
|
func (s *DHT11) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, 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-27 07:31:40 +00:00
|
|
|
s.ReadChannel(measuredValuesChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|