PKGBUILD/pkg/sensor/dht11.go
Markus Pesch 43e9d00dcb
fix: implement repository test
changes:
- Implement repository test for the sqlite backend
- Add testutils package to start container images
- Remove deprecated till_date in measured values
- Renamed columns of the table humidities, pressures and temperatures
2020-06-01 00:56:04 +02:00

60 lines
1.3 KiB
Go

package sensor
import (
"fmt"
"sync"
"github.com/go-flucky/go-dht"
uuid "github.com/satori/go.uuid"
"github.com/volker-raschek/flucky/pkg/internal/format"
"github.com/volker-raschek/flucky/pkg/types"
)
// DHT11 is a sensor to measure humidity and temperature.
type DHT11 struct {
*types.Sensor
mutex *sync.Mutex
}
// Read measured values
func (dht11 *DHT11) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
dht11.mutex.Lock()
defer dht11.mutex.Unlock()
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("Failed to initialize periph: %v", err)
}
dht, err := dht.NewDHT(dht11.GPIONumber, dht.Celsius, "")
if err != nil {
return nil, fmt.Errorf("Failed to initialize new DHT11 sensor: %v", err)
}
humidityValue, temperatureValue, err := dht.Read()
if err != nil {
return nil, fmt.Errorf("Read error: %v", err)
}
measuredValues := []*types.MeasuredValue{
{
ID: uuid.NewV4().String(),
Value: float64(humidityValue),
ValueType: "humidity",
Date: format.FormatedTime(),
SensorID: dht11.ID,
},
{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: "temperature",
Date: format.FormatedTime(),
SensorID: dht11.ID,
},
}
return measuredValues, nil
}