PKGBUILD/pkg/sensor/dht22.go
Markus Pesch fb8d4dd5eb
fix: new implementation
changes:
- Remove cli
  Some cli commands are not complete tested and are deprecated.

- Daemon
  - Old version has a very bad implementation of how to verify, if the
    device or the sensors are in the database insert. The current
    implementation can be improved but this one is betten then the old
    one.
  - Remove complete the cache store implementation. Use a normal array
    and query the length and capacity to determine how the array cache
    must be cleaned.

- Type
  Remove unused types and functions
2020-05-03 14:09:22 +02:00

62 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"
)
// DHT22 is a sensor to measure humidity and temperature.
type DHT22 struct {
*types.Sensor
mutex *sync.Mutex
}
// Read measured values
func (dht22 *DHT22) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
dht22.mutex.Lock()
defer dht22.mutex.Unlock()
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("Failed to initialize periph: %v", err)
}
dht, err := dht.NewDHT(dht22.GPIONumber, dht.Celsius, "")
if err != nil {
return nil, fmt.Errorf("Failed to initialize new DHT22 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",
FromDate: format.FormatedTime(),
TillDate: format.FormatedTime(),
SensorID: dht22.ID,
},
{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: "temperature",
FromDate: format.FormatedTime(),
TillDate: format.FormatedTime(),
SensorID: dht22.ID,
},
}
return measuredValues, nil
}