|
|
|
@ -1,175 +1,182 @@
|
|
|
|
|
package logfile
|
|
|
|
|
|
|
|
|
|
// type csvLogfile struct {
|
|
|
|
|
// logfile string
|
|
|
|
|
// }
|
|
|
|
|
import (
|
|
|
|
|
"encoding/csv"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
// func (cl *csvLogfile) GetLogfile() string {
|
|
|
|
|
// return cl.logfile
|
|
|
|
|
// }
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
|
|
|
|
// if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
|
|
|
|
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
|
|
|
|
// }
|
|
|
|
|
type csvLogfile struct {
|
|
|
|
|
logfile string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// humidities := make([]*types.Humidity, 0)
|
|
|
|
|
func (cl *csvLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
|
|
|
|
|
|
|
|
|
// f, err := os.Open(cl.logfile)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
|
|
|
|
// }
|
|
|
|
|
// defer f.Close()
|
|
|
|
|
allMeasuredValues, err := cl.Read()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// jsonDecoder := json.NewDecoder(f)
|
|
|
|
|
// err = jsonDecoder.Decode(&humidities)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
|
|
|
|
// }
|
|
|
|
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
|
|
|
|
|
|
|
|
|
// return humidities, nil
|
|
|
|
|
// }
|
|
|
|
|
if compression {
|
|
|
|
|
allMeasuredValues = Compression(allMeasuredValues)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
|
|
|
|
err = cl.Write(allMeasuredValues)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
|
|
// if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileNotFound, cl.logfile, err)
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// f, err := os.Open(cl.logfile)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
|
|
|
|
|
// }
|
|
|
|
|
// defer f.Close()
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// r := csv.NewReader(f)
|
|
|
|
|
// records, err := r.ReadAll()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
|
|
|
|
// }
|
|
|
|
|
f, err := os.Open(cl.logfile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
// for _, record := range records {
|
|
|
|
|
// times := make([]time.Time, 0)
|
|
|
|
|
// for _, j := range []int{3, 4} {
|
|
|
|
|
// time, err := time.Parse(timeFormat, record[j])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
|
|
|
|
|
// }
|
|
|
|
|
// times = append(times, time)
|
|
|
|
|
// }
|
|
|
|
|
r := csv.NewReader(f)
|
|
|
|
|
records, err := r.ReadAll()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
|
|
|
|
// }
|
|
|
|
|
measuredValues := make([]types.MeasuredValue, 0)
|
|
|
|
|
|
|
|
|
|
// measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
|
|
|
|
|
// }
|
|
|
|
|
for _, record := range records {
|
|
|
|
|
|
|
|
|
|
// temperature := &types.Temperature{
|
|
|
|
|
// TemperatureID: record[0], // 0
|
|
|
|
|
// TemperatureValue: temperatureValue, // 1
|
|
|
|
|
// TemperatureUnit: measurementUnit, // 2
|
|
|
|
|
// TemperatureFromDate: times[0], // 3
|
|
|
|
|
// TemperatureTillDate: times[1], // 4
|
|
|
|
|
// SensorID: record[5], // 5
|
|
|
|
|
// }
|
|
|
|
|
// Value
|
|
|
|
|
value, err := strconv.ParseFloat(record[2], 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[2], err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// // Creation date
|
|
|
|
|
// temperatureCreationDate, err := time.Parse(timeFormat, record[6])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
|
|
|
|
|
// }
|
|
|
|
|
// temperature.CreationDate = &temperatureCreationDate
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if record[7] != "" {
|
|
|
|
|
// temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
|
|
|
|
// }
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// temperature.UpdateDate = &temperatureUpdateDate
|
|
|
|
|
// }
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
// temperatures = append(temperatures, temperature)
|
|
|
|
|
// }
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return temperatures, nil
|
|
|
|
|
// }
|
|
|
|
|
measuredValues = append(measuredValues, measuredValue)
|
|
|
|
|
|
|
|
|
|
// func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// f, err := os.Create(cl.logfile)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
|
|
|
|
// }
|
|
|
|
|
// defer f.Close()
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
// w := csv.NewWriter(f)
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for _, humidity := range humidities {
|
|
|
|
|
// w.Write([]string{
|
|
|
|
|
// fmt.Sprintf("%v", humidity.HumidityID),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.HumidityValue),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.HumidityFromDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.HumidityTillDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.SensorID),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.CreationDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", humidity.UpdateDate.Format(timeFormat)),
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
measuredValues = append(measuredValues, measuredValue)
|
|
|
|
|
default:
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// w.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
|
|
|
|
return measuredValues, nil
|
|
|
|
|
|
|
|
|
|
// func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
|
|
|
|
// f, err := os.Create(cl.logfile)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
|
|
|
|
// }
|
|
|
|
|
// defer f.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writeCreationDate(temperatures)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
// w := csv.NewWriter(f)
|
|
|
|
|
writeCreationDate(measuredValues)
|
|
|
|
|
|
|
|
|
|
// for _, temperature := range temperatures {
|
|
|
|
|
// record := make([]string, 0)
|
|
|
|
|
w := csv.NewWriter(f)
|
|
|
|
|
|
|
|
|
|
// if temperature.UpdateDate != nil {
|
|
|
|
|
// record = []string{
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureID),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureValue),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureUnit),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.SensorID),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.UpdateDate.Format(timeFormat)),
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// record = []string{
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureID),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureValue),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureUnit),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.SensorID),
|
|
|
|
|
// fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
|
|
|
|
// fmt.Sprintf(""),
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
|
|
|
|
|
|
// w.Write(record)
|
|
|
|
|
// }
|
|
|
|
|
record := []string{
|
|
|
|
|
measuredValue.GetID(),
|
|
|
|
|
fmt.Sprintf("%v", measuredValue.GetMeasuredValueType()),
|
|
|
|
|
fmt.Sprintf("%v", measuredValue.GetValue()),
|
|
|
|
|
measuredValue.GetFromDate().Format(timeFormat),
|
|
|
|
|
measuredValue.GetTillDate().Format(timeFormat),
|
|
|
|
|
measuredValue.GetSensorID(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// w.Flush()
|
|
|
|
|
if measuredValue.GetCreationDate() != nil {
|
|
|
|
|
record = append(record, measuredValue.GetCreationDate().Format(timeFormat))
|
|
|
|
|
} else {
|
|
|
|
|
record = append(record, "null")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
|
|
|
|
if measuredValue.GetUpdateDate() != nil {
|
|
|
|
|
record = append(record, measuredValue.GetUpdateDate().Format(timeFormat))
|
|
|
|
|
} else {
|
|
|
|
|
record = append(record, "null")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(record)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|