refac(pkg/types): remove deprecated prefix name of struct attributes

This commit is contained in:
2020-01-10 22:03:49 +01:00
parent 95fb1f6745
commit 2cd2188dcb
16 changed files with 176 additions and 314 deletions

View File

@ -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)
}

View File

@ -1,9 +1,7 @@
package sensor
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-flucky/flucky/pkg/internal/format"
@ -17,8 +15,18 @@ type DHT11 struct {
*types.Sensor
}
func (s *DHT11) ID() string {
return s.SensorID
// GetID returns the sensor id
func (s *DHT11) GetID() string {
return s.ID
}
// GetTicker returns a new ticker, which tick every when the sensor should be read
func (s *DHT11) GetTicker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}
// Read measured values
@ -51,7 +59,7 @@ func (s *DHT11) 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(),
@ -59,51 +67,9 @@ func (s *DHT11) 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 *DHT11) 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 *DHT11) 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 *DHT11) Ticker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}

View File

@ -1,9 +1,7 @@
package sensor
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-flucky/flucky/pkg/internal/format"
@ -17,8 +15,18 @@ type DHT22 struct {
*types.Sensor
}
func (s *DHT22) ID() string {
return s.SensorID
// GetID returns the sensor id
func (s *DHT22) GetID() string {
return s.ID
}
// GetTicker returns a new ticker, which tick every when the sensor should be read
func (s *DHT22) GetTicker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}
// Read measured values
@ -51,7 +59,7 @@ func (s *DHT22) 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(),
@ -59,51 +67,9 @@ func (s *DHT22) 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 *DHT22) 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 *DHT22) 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 *DHT22) Ticker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}

View File

@ -1,13 +1,11 @@
package sensor
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/go-flucky/flucky/pkg/internal/format"
@ -20,20 +18,30 @@ type DS18B20 struct {
*types.Sensor
}
func (s *DS18B20) ID() string {
return s.SensorID
// GetID returns the sensor id
func (s *DS18B20) GetID() string {
return s.ID
}
// GetTicker returns a new ticker, which tick every when the sensor should be read
func (s *DS18B20) GetTicker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}
// Read measured values
func (s *DS18B20) Read() ([]*types.MeasuredValue, error) {
if s.WireID == nil {
return nil, fmt.Errorf("WireID is not specified for sensor %v", s.Name())
return nil, fmt.Errorf("WireID is not specified for sensor %v", s.Name)
}
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
if err != nil {
return nil, fmt.Errorf("Can not read data from sensor %v", s.SensorName)
return nil, fmt.Errorf("Can not read data from sensor %v", s.Name)
}
raw := string(data)
@ -57,52 +65,9 @@ func (s *DS18B20) 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 *DS18B20) 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 *DS18B20) 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 *DS18B20) Ticker() *time.Ticker {
duration, err := time.ParseDuration(s.TickDuration)
if err != nil {
duration = time.Minute
}
return time.NewTicker(duration)
}

View File

@ -6,7 +6,7 @@ import (
)
type Sensor interface {
ID() string
GetID() string
GetTicker() *time.Ticker
Read() ([]*types.MeasuredValue, error)
Ticker() *time.Ticker
}