2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2019-03-04 10:05:02 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-03-04 10:05:02 +00:00
|
|
|
"github.com/MichaelS11/go-dht"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-05-12 09:57:53 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
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-15 09:49:46 +00:00
|
|
|
// GetEnabled returns a boolen if the sensor is enabled or disabled
|
|
|
|
func (s *DHT22) GetEnabled() bool {
|
|
|
|
return s.SensorEnabled
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
func (s *DHT22) ReadHumidity() (*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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-13 19:25:32 +00:00
|
|
|
// ReadTemperature measure the temperature
|
|
|
|
func (s *DHT22) ReadTemperature() (*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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|