fix(pkg/types): use measured values as struct instead interface

This commit is contained in:
2019-07-02 22:33:01 +02:00
parent d703d33e08
commit 825511c3b5
23 changed files with 218 additions and 659 deletions

View File

@ -25,7 +25,7 @@ func (s *BME280) GetSensorModel() types.SensorModel {
}
// Read measured values
func (s *BME280) Read() ([]types.MeasuredValue, error) {
func (s *BME280) Read() ([]*types.MeasuredValue, error) {
// Create new connection to i2c-bus on 1 line with address 0x76.
// Use i2cdetect utility to find device address over the i2c-bus
@ -62,27 +62,30 @@ func (s *BME280) Read() ([]types.MeasuredValue, error) {
log.Fatal(err)
}
measuredValues := []types.MeasuredValue{
&types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: float64(humidityValue),
HumidityFromDate: time.Now(),
HumidityTillDate: time.Now(),
SensorID: s.SensorID,
measuredValues := []*types.MeasuredValue{
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(humidityValue),
ValueType: types.MeasuredValueTypeHumidity,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Pressure{
PressureID: uuid.NewV4().String(),
PressureValue: float64(pressureValue),
PressureFromDate: time.Now(),
PressureTillDate: time.Now(),
SensorID: s.SensorID,
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(pressureValue),
ValueType: types.MeasuredValueTypePressure,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: float64(temperatureValue),
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: types.MeasuredValueTypeTemperature,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
}
@ -91,7 +94,7 @@ func (s *BME280) Read() ([]types.MeasuredValue, error) {
// ReadChannel reads the measured values from the sensor and writes them to a
// channel.
func (s *BME280) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *BME280) ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
@ -108,7 +111,7 @@ func (s *BME280) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue,
// ReadContinously reads the measured values continously from the sensor and
// writes them to a channel.
func (s *BME280) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func (s *BME280) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():

View File

@ -22,7 +22,7 @@ func (s *DHT11) GetSensorModel() types.SensorModel {
}
// Read measured values
func (s *DHT11) Read() ([]types.MeasuredValue, error) {
func (s *DHT11) Read() ([]*types.MeasuredValue, error) {
err := dht.HostInit()
if err != nil {
@ -44,20 +44,22 @@ func (s *DHT11) Read() ([]types.MeasuredValue, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
measuredValues := []types.MeasuredValue{
&types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: humidityValue,
HumidityFromDate: time.Now(),
HumidityTillDate: time.Now(),
SensorID: s.SensorID,
measuredValues := []*types.MeasuredValue{
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(humidityValue),
ValueType: types.MeasuredValueTypeHumidity,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: types.MeasuredValueTypeTemperature,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
}
@ -66,7 +68,7 @@ func (s *DHT11) Read() ([]types.MeasuredValue, error) {
// ReadChannel reads the measured values from the sensor and writes them to a
// channel.
func (s *DHT11) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT11) ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
@ -83,7 +85,7 @@ func (s *DHT11) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue,
// ReadContinously reads the measured values continously from the sensor and
// writes them to a channel.
func (s *DHT11) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func (s *DHT11) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():

View File

@ -22,7 +22,7 @@ func (s *DHT22) GetSensorModel() types.SensorModel {
}
// Read measured values
func (s *DHT22) Read() ([]types.MeasuredValue, error) {
func (s *DHT22) Read() ([]*types.MeasuredValue, error) {
err := dht.HostInit()
if err != nil {
@ -44,20 +44,22 @@ func (s *DHT22) Read() ([]types.MeasuredValue, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
measuredValues := []types.MeasuredValue{
&types.Humidity{
HumidityID: uuid.NewV4().String(),
HumidityValue: humidityValue,
HumidityFromDate: time.Now(),
HumidityTillDate: time.Now(),
SensorID: s.SensorID,
measuredValues := []*types.MeasuredValue{
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(humidityValue),
ValueType: types.MeasuredValueTypeHumidity,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: types.MeasuredValueTypeTemperature,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
}
@ -66,7 +68,7 @@ func (s *DHT22) Read() ([]types.MeasuredValue, error) {
// ReadChannel reads the measured values from the sensor and writes them to a
// channel.
func (s *DHT22) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT22) ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
@ -83,7 +85,7 @@ func (s *DHT22) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue,
// ReadContinously reads the measured values continously from the sensor and
// writes them to a channel.
func (s *DHT22) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func (s *DHT22) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():

View File

@ -25,7 +25,7 @@ func (s *DS18B20) GetSensorModel() types.SensorModel {
}
// Read measured values
func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
func (s *DS18B20) Read() ([]*types.MeasuredValue, error) {
if s.WireID == nil {
return nil, fmt.Errorf("WireID is not specified for sensor %v", s.Name())
@ -50,13 +50,14 @@ func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
temperatureValue := c / 1000
measuredValues := []types.MeasuredValue{
&types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
measuredValues := []*types.MeasuredValue{
&types.MeasuredValue{
ID: uuid.NewV4().String(),
Value: float64(temperatureValue),
ValueType: types.MeasuredValueTypeTemperature,
FromDate: time.Now(),
TillDate: time.Now(),
SensorID: s.SensorID,
},
}
@ -66,7 +67,7 @@ func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
// ReadChannel reads the measured values from the sensor and writes them to a
// channel.
func (s *DS18B20) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DS18B20) ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
@ -83,7 +84,7 @@ func (s *DS18B20) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue
// ReadContinously reads the measured values continously from the sensor and
// writes them to a channel.
func (s *DS18B20) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func (s *DS18B20) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():

View File

@ -9,7 +9,7 @@ import (
type Sensor interface {
GetSensorModel() types.SensorModel
Read() ([]types.MeasuredValue, error)
ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup)
ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error)
Read() ([]*types.MeasuredValue, error)
ReadChannel(measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup)
ReadContinously(ctx context.Context, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error)
}

View File

@ -12,9 +12,9 @@ import (
)
// Read measured values from sensors
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
func Read(ctx context.Context, sensors []Sensor) ([]*types.MeasuredValue, error) {
measuredValuesChannel := make(chan []types.MeasuredValue, len(sensors))
measuredValuesChannel := make(chan []*types.MeasuredValue, len(sensors))
errorChannel := make(chan error, len(sensors))
ReadChannel(ctx, sensors, measuredValuesChannel, errorChannel)
@ -31,7 +31,7 @@ func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error)
// ReadChannel reads the measured values from sensors and writes them to a
// channel.
func ReadChannel(ctx context.Context, sensors []Sensor, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func ReadChannel(ctx context.Context, sensors []Sensor, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
wg := new(sync.WaitGroup)
wg.Add(len(sensors))
@ -45,7 +45,7 @@ func ReadChannel(ctx context.Context, sensors []Sensor, measuredValuesChannel ch
// ReadContinuously reads the measured values continously from sensors and writes
// them to a channel.
func ReadContinuously(ctx context.Context, sensors []Sensor, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
func ReadContinuously(ctx context.Context, sensors []Sensor, measuredValuesChannel chan<- []*types.MeasuredValue, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():