refac(pkg/types): remove deprecated prefix name of struct attributes
This commit is contained in:
@ -1,10 +1,7 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/d2r2/go-bsbmp"
|
||||
@ -20,8 +17,18 @@ type BME280 struct {
|
||||
*types.Sensor
|
||||
}
|
||||
|
||||
func (s *BME280) ID() string {
|
||||
return s.SensorID
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Read measured values
|
||||
@ -69,7 +76,7 @@ func (s *BME280) Read() ([]*types.MeasuredValue, error) {
|
||||
ValueType: types.MeasuredValueTypeHumidity,
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.SensorID,
|
||||
SensorID: s.ID,
|
||||
},
|
||||
&types.MeasuredValue{
|
||||
ID: uuid.NewV4().String(),
|
||||
@ -77,7 +84,7 @@ func (s *BME280) Read() ([]*types.MeasuredValue, error) {
|
||||
ValueType: types.MeasuredValueTypePressure,
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.SensorID,
|
||||
SensorID: s.ID,
|
||||
},
|
||||
&types.MeasuredValue{
|
||||
ID: uuid.NewV4().String(),
|
||||
@ -85,51 +92,9 @@ func (s *BME280) Read() ([]*types.MeasuredValue, error) {
|
||||
ValueType: types.MeasuredValueTypeTemperature,
|
||||
FromDate: format.FormatedTime(),
|
||||
TillDate: format.FormatedTime(),
|
||||
SensorID: s.SensorID,
|
||||
SensorID: s.ID,
|
||||
},
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||
// channel.
|
||||
func (s *BME280) ReadChannel(measuredValueChannel chan<- *types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
||||
measuredValues, err := s.Read()
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
return
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValueChannel <- measuredValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ReadContinously reads the measured values continously from the sensor and
|
||||
// writes them to a channel.
|
||||
func (s *BME280) ReadContinously(ctx context.Context, measuredValueChannel chan<- *types.MeasuredValue, errorChannel chan<- error) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||
return
|
||||
default:
|
||||
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ticker returns a new ticker, which tick every when the sensor should be read
|
||||
func (s *BME280) Ticker() *time.Ticker {
|
||||
duration, err := time.ParseDuration(s.TickDuration)
|
||||
if err != nil {
|
||||
duration = time.Minute
|
||||
}
|
||||
return time.NewTicker(duration)
|
||||
}
|
||||
|
Reference in New Issue
Block a user