fix(pkg/logfile): read from csv file styled by measured values
This commit is contained in:
parent
41653afebf
commit
4fe4abb0e9
@ -1,175 +1,182 @@
|
|||||||
package logfile
|
package logfile
|
||||||
|
|
||||||
// type csvLogfile struct {
|
import (
|
||||||
// logfile string
|
"encoding/csv"
|
||||||
// }
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
// func (cl *csvLogfile) GetLogfile() string {
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
// return cl.logfile
|
)
|
||||||
// }
|
|
||||||
|
|
||||||
// func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
type csvLogfile struct {
|
||||||
// if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
logfile string
|
||||||
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// humidities := make([]*types.Humidity, 0)
|
func (cl *csvLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
||||||
|
|
||||||
// f, err := os.Open(cl.logfile)
|
allMeasuredValues, err := cl.Read()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
return err
|
||||||
// }
|
}
|
||||||
// defer f.Close()
|
|
||||||
|
|
||||||
// jsonDecoder := json.NewDecoder(f)
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
||||||
// err = jsonDecoder.Decode(&humidities)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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)
|
func (cl *csvLogfile) Read() ([]types.MeasuredValue, error) {
|
||||||
// if err != nil {
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
||||||
// }
|
}
|
||||||
// defer f.Close()
|
|
||||||
|
|
||||||
// r := csv.NewReader(f)
|
f, err := os.Open(cl.logfile)
|
||||||
// records, err := r.ReadAll()
|
if err != nil {
|
||||||
// if err != nil {
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
}
|
||||||
// }
|
defer f.Close()
|
||||||
|
|
||||||
// for _, record := range records {
|
r := csv.NewReader(f)
|
||||||
// times := make([]time.Time, 0)
|
records, err := r.ReadAll()
|
||||||
// for _, j := range []int{3, 4} {
|
if err != nil {
|
||||||
// time, err := time.Parse(timeFormat, record[j])
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
||||||
// if err != nil {
|
}
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
|
|
||||||
// }
|
|
||||||
// times = append(times, time)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
measuredValues := make([]types.MeasuredValue, 0)
|
||||||
// if err != nil {
|
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
|
for _, record := range records {
|
||||||
// if err != nil {
|
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// temperature := &types.Temperature{
|
// Value
|
||||||
// TemperatureID: record[0], // 0
|
value, err := strconv.ParseFloat(record[2], 64)
|
||||||
// TemperatureValue: temperatureValue, // 1
|
if err != nil {
|
||||||
// TemperatureUnit: measurementUnit, // 2
|
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[2], err)
|
||||||
// TemperatureFromDate: times[0], // 3
|
}
|
||||||
// TemperatureTillDate: times[1], // 4
|
|
||||||
// SensorID: record[5], // 5
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Creation date
|
// Times
|
||||||
// temperatureCreationDate, err := time.Parse(timeFormat, record[6])
|
times := make([]time.Time, 0)
|
||||||
// if err != nil {
|
for _, i := range []int{3, 4} {
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
|
time, err := time.Parse(timeFormat, record[i])
|
||||||
// }
|
if err != nil {
|
||||||
// temperature.CreationDate = &temperatureCreationDate
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[i], err)
|
||||||
|
}
|
||||||
|
times = append(times, time)
|
||||||
|
}
|
||||||
|
|
||||||
// if record[7] != "" {
|
switch record[1] {
|
||||||
// temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
|
case fmt.Sprint(types.MeasuredValueTypeHumidity):
|
||||||
// if err != nil {
|
measuredValue := &types.Humidity{
|
||||||
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
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)
|
// CreationDate
|
||||||
// if err != nil {
|
creationDate, err := time.Parse(timeFormat, record[6])
|
||||||
// return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
if err != nil {
|
||||||
// }
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
|
||||||
// defer f.Close()
|
}
|
||||||
|
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 {
|
measuredValues = append(measuredValues, measuredValue)
|
||||||
// w.Write([]string{
|
default:
|
||||||
// fmt.Sprintf("%v", humidity.HumidityID),
|
continue
|
||||||
// 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)),
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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 {
|
w := csv.NewWriter(f)
|
||||||
// record := make([]string, 0)
|
|
||||||
|
|
||||||
// if temperature.UpdateDate != nil {
|
for _, measuredValue := range measuredValues {
|
||||||
// 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(""),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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
|
||||||
|
}
|
||||||
|
@ -7,7 +7,5 @@ import (
|
|||||||
type Logfile interface {
|
type Logfile interface {
|
||||||
Append(compression bool, measuredValues []types.MeasuredValue) error
|
Append(compression bool, measuredValues []types.MeasuredValue) error
|
||||||
Read() ([]types.MeasuredValue, error)
|
Read() ([]types.MeasuredValue, error)
|
||||||
// ReadCustom(r io.Reader) ([]types.MeasuredValue, error)
|
|
||||||
Write(measuredValues []types.MeasuredValue) error
|
Write(measuredValues []types.MeasuredValue) error
|
||||||
// WriteCustom(w io.Writer, measuredValues []types.MeasuredValue) error
|
|
||||||
}
|
}
|
||||||
|
@ -71,13 +71,13 @@ func Compression(measuredValues []types.MeasuredValue) []types.MeasuredValue {
|
|||||||
// into the correct format.
|
// into the correct format.
|
||||||
func New(logfile string) Logfile {
|
func New(logfile string) Logfile {
|
||||||
|
|
||||||
ext := filepath.Join(logfile)
|
ext := filepath.Ext(logfile)
|
||||||
|
|
||||||
switch ext {
|
switch ext {
|
||||||
// case ".csv":
|
case ".csv":
|
||||||
// return &csvLogfile{
|
return &csvLogfile{
|
||||||
// logfile: logfile,
|
logfile: logfile,
|
||||||
// }
|
}
|
||||||
case ".json":
|
case ".json":
|
||||||
return &jsonLogfile{
|
return &jsonLogfile{
|
||||||
logfile: logfile,
|
logfile: logfile,
|
||||||
|
@ -37,6 +37,10 @@ func (h *Humidity) GetCreationDate() *time.Time {
|
|||||||
return h.CreationDate
|
return h.CreationDate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) GetUpdateDate() *time.Time {
|
||||||
|
return h.UpdateDate
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Humidity) GetMeasuredValueType() MeasuredValueType {
|
func (h *Humidity) GetMeasuredValueType() MeasuredValueType {
|
||||||
return MeasuredValueTypeHumidity
|
return MeasuredValueTypeHumidity
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,16 @@ import "time"
|
|||||||
|
|
||||||
type MeasuredValue interface {
|
type MeasuredValue interface {
|
||||||
GetID() string
|
GetID() string
|
||||||
GetCreationDate() *time.Time
|
|
||||||
GetMeasuredValueType() MeasuredValueType
|
GetMeasuredValueType() MeasuredValueType
|
||||||
|
GetValue() float64
|
||||||
GetFromDate() time.Time
|
GetFromDate() time.Time
|
||||||
GetTillDate() time.Time
|
GetTillDate() time.Time
|
||||||
GetSensorID() string
|
GetSensorID() string
|
||||||
GetValue() float64
|
GetCreationDate() *time.Time
|
||||||
SetCreationDate(date *time.Time)
|
GetUpdateDate() *time.Time
|
||||||
|
|
||||||
SetTillDate(date time.Time)
|
SetTillDate(date time.Time)
|
||||||
|
SetCreationDate(date *time.Time)
|
||||||
SetUpdateDate(date *time.Time)
|
SetUpdateDate(date *time.Time)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// Temperature ...
|
// Temperature ...
|
||||||
type Temperature struct {
|
type Temperature struct {
|
||||||
@ -37,6 +39,10 @@ func (t *Temperature) GetCreationDate() *time.Time {
|
|||||||
return t.CreationDate
|
return t.CreationDate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) GetUpdateDate() *time.Time {
|
||||||
|
return t.UpdateDate
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Temperature) GetMeasuredValueType() MeasuredValueType {
|
func (t *Temperature) GetMeasuredValueType() MeasuredValueType {
|
||||||
return MeasuredValueTypeTemperature
|
return MeasuredValueTypeTemperature
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user