fix(pkg/logfile,sensor): implement pressure as measured value

This commit is contained in:
Markus Pesch 2019-06-30 15:06:23 +02:00
parent 289aaf2093
commit 043fc395d9
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
4 changed files with 108 additions and 4 deletions

View File

@ -80,6 +80,34 @@ func (cl *csvLogfile) Read() ([]types.MeasuredValue, error) {
measuredValues = append(measuredValues, measuredValue)
case fmt.Sprint(types.MeasuredValueTypePressure):
measuredValue := &types.Pressure{
PressureID: record[0], // 0
// 1 - MeasuredValue
PressureValue: value, // 2
PressureFromDate: times[0], // 3
PressureTillDate: times[1], // 4
SensorID: record[5], // 5
}
// CreationDate
creationDate, err := time.Parse(timeFormat, record[6])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
}
measuredValue.CreationDate = &creationDate
// UpdateDate
if record[7] != "null" {
updateDate, err := time.Parse(timeFormat, record[7])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
}
measuredValue.UpdateDate = &updateDate
}
measuredValues = append(measuredValues, measuredValue)
case fmt.Sprint(types.MeasuredValueTypeTemperature):
measuredValue := &types.Temperature{
TemperatureID: record[0], // 0

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"math"
"sync"
"time"
@ -50,10 +51,12 @@ func (s *BME280) Read() ([]types.MeasuredValue, error) {
log.Fatal(err)
}
// p, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
// if err != nil {
// log.Fatal(err)
// }
pressureValue, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
if err != nil {
log.Fatal(err)
}
pressureValueRound := math.Round(float64(pressureValue)/10*0.25) * 10 / 0.25
_, humidityValue, err := sensor.ReadHumidityRH(bsbmp.ACCURACY_STANDARD)
if err != nil {
@ -68,6 +71,13 @@ func (s *BME280) Read() ([]types.MeasuredValue, error) {
HumidityTillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Pressure{
PressureID: uuid.NewV4().String(),
PressureValue: pressureValueRound,
PressureFromDate: time.Now(),
PressureTillDate: time.Now(),
SensorID: s.SensorID,
},
&types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: float64(temperatureValue),

View File

@ -22,10 +22,12 @@ type MeasuredValueType string
const (
MeasuredValueTypeHumidity MeasuredValueType = "humidity"
MeasuredValueTypePressure MeasuredValueType = "pressure"
MeasuredValueTypeTemperature MeasuredValueType = "temperature"
)
var MeasuredValueTypes = []MeasuredValueType{
MeasuredValueTypeHumidity,
MeasuredValueTypePressure,
MeasuredValueTypeTemperature,
}

64
pkg/types/pressure.go Normal file
View File

@ -0,0 +1,64 @@
package types
import (
"time"
)
// Pressure ...
type Pressure struct {
PressureID string `json:"pressure_id" xml:"pressure_id"`
PressureValue float64 `json:"pressure_value,string" xml:"pressure_value,string"`
PressureFromDate time.Time `json:"pressure_from_date" xml:"pressure_from_date"`
PressureTillDate time.Time `json:"pressure_till_date" xml:"pressure_till_date"`
SensorID string `json:"sensor_id" xml:"sensor_id"`
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
}
func (t *Pressure) GetID() string {
return t.PressureID
}
func (t *Pressure) GetValue() float64 {
return t.PressureValue
}
func (t *Pressure) GetFromDate() time.Time {
return t.PressureFromDate
}
func (t *Pressure) GetTillDate() time.Time {
return t.PressureTillDate
}
func (t *Pressure) GetSensorID() string {
return t.SensorID
}
func (t *Pressure) GetCreationDate() *time.Time {
return t.CreationDate
}
func (t *Pressure) GetUpdateDate() *time.Time {
return t.UpdateDate
}
func (t *Pressure) GetMeasuredValueType() MeasuredValueType {
return MeasuredValueTypePressure
}
func (t *Pressure) SetValue(value float64) {
t.PressureValue = value
}
func (t *Pressure) SetTillDate(date time.Time) {
t.PressureTillDate = date
}
func (t *Pressure) SetCreationDate(date *time.Time) {
t.CreationDate = date
}
func (t *Pressure) SetUpdateDate(date *time.Time) {
t.UpdateDate = date
}