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
This commit is contained in:
@ -1,98 +1,91 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/d2r2/go-bsbmp"
|
||||
"github.com/d2r2/go-i2c"
|
||||
"github.com/d2r2/go-logger"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"github.com/volker-raschek/flucky/pkg/internal/format"
|
||||
"github.com/volker-raschek/flucky/pkg/types"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
// BME280 is a sensor to measure humidity and temperature.
|
||||
type BME280 struct {
|
||||
*types.Sensor
|
||||
}
|
||||
|
||||
// GetID returns the sensor id
|
||||
func (s *BME280) GetID() string {
|
||||
return s.ID
|
||||
}
|
||||
|
||||
// GetTicker returns a new ticker, which tick every when the sensor should be read
|
||||
func (s *BME280) GetTicker() *time.Ticker {
|
||||
duration, err := time.ParseDuration(s.TickDuration)
|
||||
if err != nil {
|
||||
duration = time.Minute
|
||||
}
|
||||
return time.NewTicker(duration)
|
||||
mutex *sync.Mutex
|
||||
}
|
||||
|
||||
// Read measured values
|
||||
func (s *BME280) Read() ([]*types.MeasuredValue, error) {
|
||||
func (bme280 *BME280) Read() ([]*types.MeasuredValue, error) {
|
||||
|
||||
// Lock multiple access
|
||||
bme280.mutex.Lock()
|
||||
defer bme280.mutex.Unlock()
|
||||
|
||||
// Create new connection to i2c-bus on 1 line with address 0x76.
|
||||
// Use i2cdetect utility to find device address over the i2c-bus
|
||||
i2c, err := i2c.NewI2C(*s.I2CAddress, *s.I2CBus)
|
||||
|
||||
i2c, err := i2c.NewI2C(*bme280.I2CAddress, *bme280.I2CBus)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
defer i2c.Close()
|
||||
|
||||
logger.ChangePackageLogLevel("i2c", logger.InfoLevel)
|
||||
// Reduce loglevel
|
||||
for _, pkg := range []string{"bsbmp", "i2c"} {
|
||||
err = logger.ChangePackageLogLevel(pkg, logger.InfoLevel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to change package log level: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
sensor, err := bsbmp.NewBMP(bsbmp.BME280, i2c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.ChangePackageLogLevel("bsbmp", logger.InfoLevel)
|
||||
|
||||
temperatureValue, err := sensor.ReadTemperatureC(bsbmp.ACCURACY_STANDARD)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pressureValue, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// pressureValueRound := math.Round(float64(pressureValue)/10*0.25) * 10 / 0.25
|
||||
|
||||
_, humidityValue, err := sensor.ReadHumidityRH(bsbmp.ACCURACY_STANDARD)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues := []*types.MeasuredValue{
|
||||
&types.MeasuredValue{
|
||||
{
|
||||
ID: uuid.NewV4().String(),
|
||||
Value: float64(humidityValue),
|
||||
ValueType: types.MeasuredValueTypeHumidity,
|
||||
ValueType: "humidity",
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.ID,
|
||||
SensorID: bme280.ID,
|
||||
},
|
||||
&types.MeasuredValue{
|
||||
{
|
||||
ID: uuid.NewV4().String(),
|
||||
Value: float64(pressureValue),
|
||||
ValueType: types.MeasuredValueTypePressure,
|
||||
ValueType: "pressure",
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.ID,
|
||||
SensorID: bme280.ID,
|
||||
},
|
||||
&types.MeasuredValue{
|
||||
{
|
||||
ID: uuid.NewV4().String(),
|
||||
Value: float64(temperatureValue),
|
||||
ValueType: types.MeasuredValueTypeTemperature,
|
||||
ValueType: "temperature",
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.ID,
|
||||
SensorID: bme280.ID,
|
||||
},
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user