fix(pkg/logfile/csv): read from csv file

This commit is contained in:
Markus Pesch 2019-06-22 13:13:15 +02:00
parent 3791723230
commit bbcccd7479
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 11 additions and 7 deletions

View File

@ -42,22 +42,23 @@ func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
}
func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
return nil, fmt.Errorf("%v %v: %v", errorLogfileNotFound, cl.logfile, err)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(cl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
}
defer f.Close()
r := csv.NewReader(f)
records, err := r.ReadAll()
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, cl.logfile)
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
}
for _, record := range records {
@ -65,14 +66,14 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
for _, j := range []int{2, 3} {
time, err := time.Parse(timeFormat, record[j])
if err != nil {
return nil, fmt.Errorf("%v: %v", errorParseTime, record[j])
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
}
times = append(times, time)
}
temperatureValue, err := strconv.ParseFloat(record[1], 64)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorParseFloat, record[1])
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
}
temperature := &types.Temperature{
@ -86,7 +87,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if len(record) == 6 && record[5] != "" {
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
if err != nil {
return nil, fmt.Errorf("%v: %v", errorParseTime, record[5])
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
}
temperature.CreationDate = &temperatureCreationDate
@ -95,7 +96,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if len(record) == 7 && record[6] != "" {
temperatureUpdateDate, err := time.Parse(timeFormat, record[6])
if err != nil {
return nil, fmt.Errorf("%v: %v", errorParseTime, record[6])
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
}
temperature.UpdateDate = &temperatureUpdateDate
@ -166,6 +167,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.SensorID),
fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
fmt.Sprintf(""),
}
}

View File

@ -0,0 +1,2 @@
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:18:07.463893776 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587701 +0200
1 a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 2019-06-14 21:15:28.504051541 +0200 2019-06-14 21:18:07.384104493 +0200 84eac248-6927-4db6-b6f9-7891ce2d301e 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587076 +0200
2 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 2019-06-14 21:15:28.583856443 +0200 2019-06-14 21:18:07.463893776 +0200 efcd755e-82d1-4789-a50b-355b8735b8d8 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587701 +0200