PKGBUILD/pkg/sensor/dht22.go
Markus Pesch 3a090d190e
fix: cli temperature read
changes:
- fix: read temperature values without daemon
  Add subcommand to read temperature values without starting the daemon

- fix: implement measured value types
  Replace measured value types with constants

- fix: add sensor pipelines
  Add functions which returns a channel with measured values

- fix: filter measured values from a channel
  Add functions to filter measured values by sensor id or measured
  value types.
2020-09-21 20:05:37 +02:00

60 lines
1.3 KiB
Go

package sensor
import (
"fmt"
"sync"
"git.cryptic.systems/volker.raschek/flucky/pkg/internal/format"
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
"git.cryptic.systems/volker.raschek/go-dht"
uuid "github.com/satori/go.uuid"
)
// 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: types.Humidity,
Date: format.FormatedTime(),
SensorID: dht22.ID,
},
{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: types.Temperature,
Date: format.FormatedTime(),
SensorID: dht22.ID,
},
}
return measuredValues, nil
}