fix(pkg/logfile): support write and read air pressure values from json logfile

This commit is contained in:
Markus Pesch 2019-06-30 15:14:52 +02:00
parent 043fc395d9
commit 397f870435
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -58,6 +58,15 @@ func (jl *jsonLogfile) Read() ([]types.MeasuredValue, error) {
measuredValues = append(measuredValues, temperature) measuredValues = append(measuredValues, temperature)
} }
if _, ok := mappedMeasuredValue["pressure_id"]; ok {
pressure, err := jl.transformPressure(mappedMeasuredValue)
if err != nil {
log.Printf("Can not unmarshal pressure from map: %v", err)
continue
}
measuredValues = append(measuredValues, pressure)
}
if _, ok := mappedMeasuredValue["humidity_id"]; ok { if _, ok := mappedMeasuredValue["humidity_id"]; ok {
humidity, err := jl.transformHumidity(mappedMeasuredValue) humidity, err := jl.transformHumidity(mappedMeasuredValue)
if err != nil { if err != nil {
@ -118,6 +127,52 @@ func (jl *jsonLogfile) transformHumidity(mappedMeasuredValue map[string]interfac
return humidity, nil return humidity, nil
} }
func (jl *jsonLogfile) transformPressure(mappedMeasuredValue map[string]interface{}) (types.MeasuredValue, error) {
pressure := &types.Pressure{
PressureID: mappedMeasuredValue["pressure_id"].(string),
SensorID: mappedMeasuredValue["sensor_id"].(string),
CreationDate: nil,
UpdateDate: nil,
}
pressureValue, err := strconv.ParseFloat(mappedMeasuredValue["pressure_value"].(string), 64)
if err != nil {
return nil, fmt.Errorf("%v for pressure value. PressureID %v: %v", errorParseFloat, mappedMeasuredValue["pressure_id"].(string), err)
}
pressure.PressureValue = pressureValue
pressureFromDate, err := time.Parse(timeFormat, mappedMeasuredValue["pressure_from_date"].(string))
if err != nil {
return nil, fmt.Errorf("%v for pressure from date. PressureID %v: %v", errorParseTime, mappedMeasuredValue["pressure_id"].(string), err)
}
pressure.PressureFromDate = pressureFromDate
pressureTillDate, err := time.Parse(timeFormat, mappedMeasuredValue["pressure_till_date"].(string))
if err != nil {
return nil, fmt.Errorf("%v for pressure till date. PressureID %v: %v", errorParseTime, mappedMeasuredValue["pressure_id"].(string), err)
}
pressure.PressureTillDate = pressureTillDate
if mappedMeasuredValue["creation_date"] != nil {
creationDate, err := time.Parse(timeFormat, mappedMeasuredValue["creation_date"].(string))
if err != nil {
return nil, fmt.Errorf("%v for creation date. PressureID %v: %v", errorParseTime, mappedMeasuredValue["pressure_id"].(string), err)
}
pressure.CreationDate = &creationDate
}
if mappedMeasuredValue["update_date"] != nil {
updateDate, err := time.Parse(timeFormat, mappedMeasuredValue["update_date"].(string))
if err != nil {
return nil, fmt.Errorf("%v for update date. PressureID %v: %v", errorParseTime, mappedMeasuredValue["pressure_id"].(string), err)
}
pressure.UpdateDate = &updateDate
}
return pressure, nil
}
func (jl *jsonLogfile) transformTemperature(mappedMeasuredValue map[string]interface{}) (types.MeasuredValue, error) { func (jl *jsonLogfile) transformTemperature(mappedMeasuredValue map[string]interface{}) (types.MeasuredValue, error) {
temperature := &types.Temperature{ temperature := &types.Temperature{