PKGBUILD/pkg/sensor/dht11.go

76 lines
1.7 KiB
Go
Raw Normal View History

2019-02-22 12:08:58 +00:00
package sensor
import (
2019-03-03 18:13:37 +00:00
"fmt"
"time"
2019-02-22 12:08:58 +00:00
"github.com/volker-raschek/flucky/pkg/internal/format"
"github.com/volker-raschek/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
)
// DHT11 is a sensor to measure humidity and temperature.
type DHT11 struct {
2019-02-22 12:08:58 +00:00
*types.Sensor
}
// GetID returns the sensor id
func (s *DHT11) GetID() string {
return s.ID
}
// GetTicker returns a new ticker, which tick every when the sensor should be read
func (s *DHT11) GetTicker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}
// Read measured values
func (s *DHT11) Read() ([]*types.MeasuredValue, error) {
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)
}
humidityValue, temperatureValue, err := dht.Read()
2019-03-03 18:13:37 +00:00
if err != nil {
return nil, fmt.Errorf("Read error: %v", err)
}
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(),
SensorID: s.ID,
},
&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(),
SensorID: s.ID,
},
}
return measuredValues, nil
2019-02-24 21:46:36 +00:00
}