2019-06-19 17:18:01 +00:00
|
|
|
package logfile
|
|
|
|
|
2019-06-28 11:04:04 +00:00
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type csvLogfile struct {
|
|
|
|
logfile string
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
func (cl *csvLogfile) Read() ([]*types.MeasuredValue, error) {
|
2019-06-28 11:04:04 +00:00
|
|
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(cl.logfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
r := csv.NewReader(f)
|
|
|
|
records, err := r.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := make([]*types.MeasuredValue, 0)
|
2019-06-28 11:04:04 +00:00
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
// ValueType
|
|
|
|
valueType, err := types.SelectMeasuredValueType(record[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:04:04 +00:00
|
|
|
// Value
|
|
|
|
value, err := strconv.ParseFloat(record[2], 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[2], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Times
|
|
|
|
times := make([]time.Time, 0)
|
|
|
|
for _, i := range []int{3, 4} {
|
|
|
|
time, err := time.Parse(timeFormat, record[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[i], err)
|
|
|
|
}
|
|
|
|
times = append(times, time)
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValue := &types.MeasuredValue{
|
|
|
|
ID: record[0],
|
|
|
|
ValueType: *valueType,
|
|
|
|
Value: value,
|
|
|
|
FromDate: times[0],
|
|
|
|
TillDate: times[1],
|
|
|
|
SensorID: record[5],
|
|
|
|
}
|
2019-06-28 11:04:04 +00:00
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
// Creation date
|
|
|
|
creationDate, err := time.Parse(timeFormat, record[6])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
|
|
|
|
}
|
|
|
|
measuredValue.CreationDate = &creationDate
|
2019-06-28 11:04:04 +00:00
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
if record[7] != "null" {
|
|
|
|
updateDate, err := time.Parse(timeFormat, record[7])
|
2019-06-28 11:04:04 +00:00
|
|
|
if err != nil {
|
2019-07-02 20:33:01 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
2019-06-28 11:04:04 +00:00
|
|
|
}
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValue.UpdateDate = &updateDate
|
2019-06-28 11:04:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues = append(measuredValues, measuredValue)
|
|
|
|
|
2019-06-28 11:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
func (cl *csvLogfile) Write(measuredValues []*types.MeasuredValue) error {
|
2019-06-28 11:04:04 +00:00
|
|
|
f, err := os.Create(cl.logfile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
writeCreationDate(measuredValues)
|
|
|
|
|
|
|
|
w := csv.NewWriter(f)
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
|
|
|
|
record := []string{
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValue.ID,
|
|
|
|
fmt.Sprintf("%v", measuredValue.ValueType),
|
|
|
|
fmt.Sprintf("%v", measuredValue.Value),
|
|
|
|
measuredValue.FromDate.Format(timeFormat),
|
|
|
|
measuredValue.TillDate.Format(timeFormat),
|
|
|
|
measuredValue.SensorID,
|
2019-06-28 11:04:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
if measuredValue.CreationDate != nil {
|
|
|
|
record = append(record, measuredValue.CreationDate.Format(timeFormat))
|
2019-06-28 11:04:04 +00:00
|
|
|
} else {
|
|
|
|
record = append(record, "null")
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
if measuredValue.UpdateDate != nil {
|
|
|
|
record = append(record, measuredValue.UpdateDate.Format(timeFormat))
|
2019-06-28 11:04:04 +00:00
|
|
|
} else {
|
|
|
|
record = append(record, "null")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(record)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|