189 lines
4.6 KiB
Go
189 lines
4.6 KiB
Go
package logfile
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
)
|
|
|
|
type csvLogfile struct {
|
|
logfile string
|
|
}
|
|
|
|
func (cl *csvLogfile) Read() ([]types.MeasuredValue, error) {
|
|
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)
|
|
}
|
|
|
|
measuredValues := make([]types.MeasuredValue, 0)
|
|
|
|
for _, record := range records {
|
|
|
|
// 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)
|
|
}
|
|
|
|
switch record[1] {
|
|
case fmt.Sprint(types.MeasuredValueTypeHumidity):
|
|
measuredValue := &types.Humidity{
|
|
HumidityID: record[0], // 0
|
|
// 1 - MeasuredValue
|
|
HumidityValue: value, // 2
|
|
HumidityFromDate: times[0], // 3
|
|
HumidityTillDate: 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.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
|
|
// 1 - MeasuredValue
|
|
TemperatureValue: value, // 2
|
|
TemperatureFromDate: times[0], // 3
|
|
TemperatureTillDate: 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)
|
|
default:
|
|
continue
|
|
}
|
|
|
|
}
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|
|
|
|
func (cl *csvLogfile) Write(measuredValues []types.MeasuredValue) error {
|
|
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{
|
|
measuredValue.GetID(),
|
|
fmt.Sprintf("%v", measuredValue.GetMeasuredValueType()),
|
|
fmt.Sprintf("%v", measuredValue.GetValue()),
|
|
measuredValue.GetFromDate().Format(timeFormat),
|
|
measuredValue.GetTillDate().Format(timeFormat),
|
|
measuredValue.GetSensorID(),
|
|
}
|
|
|
|
if measuredValue.GetCreationDate() != nil {
|
|
record = append(record, measuredValue.GetCreationDate().Format(timeFormat))
|
|
} else {
|
|
record = append(record, "null")
|
|
}
|
|
|
|
if measuredValue.GetUpdateDate() != nil {
|
|
record = append(record, measuredValue.GetUpdateDate().Format(timeFormat))
|
|
} else {
|
|
record = append(record, "null")
|
|
}
|
|
|
|
w.Write(record)
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
}
|