179 lines
4.5 KiB
Go
179 lines
4.5 KiB
Go
|
package logfile
|
||
|
|
||
|
import (
|
||
|
"encoding/csv"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-flucky/flucky/pkg/types"
|
||
|
)
|
||
|
|
||
|
type csvLogfile struct {
|
||
|
logfile string
|
||
|
}
|
||
|
|
||
|
func (cl *csvLogfile) GetLogfile() string {
|
||
|
return cl.logfile
|
||
|
}
|
||
|
|
||
|
func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
||
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, cl.logfile)
|
||
|
}
|
||
|
|
||
|
humidities := make([]*types.Humidity, 0)
|
||
|
|
||
|
f, err := os.Open(cl.logfile)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, cl.logfile)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
jsonDecoder := json.NewDecoder(f)
|
||
|
err = jsonDecoder.Decode(&humidities)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
|
||
|
}
|
||
|
|
||
|
return humidities, nil
|
||
|
}
|
||
|
|
||
|
func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
||
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, cl.logfile)
|
||
|
}
|
||
|
|
||
|
temperatures := make([]*types.Temperature, 0)
|
||
|
|
||
|
f, err := os.Open(cl.logfile)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, cl.logfile)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
r := csv.NewReader(f)
|
||
|
records, err := r.ReadAll()
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, cl.logfile)
|
||
|
}
|
||
|
|
||
|
for _, record := range records {
|
||
|
times := make([]time.Time, 0)
|
||
|
for _, j := range []int{2, 3} {
|
||
|
time, err := time.Parse(timeFormat, record[j])
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[j])
|
||
|
}
|
||
|
times = append(times, time)
|
||
|
}
|
||
|
|
||
|
temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrParseFloat, record[1])
|
||
|
}
|
||
|
|
||
|
temperature := &types.Temperature{
|
||
|
TemperatureID: record[0],
|
||
|
TemperatureValue: temperatureValue,
|
||
|
TemperatureFromDate: times[0],
|
||
|
TemperatureTillDate: times[1],
|
||
|
SensorID: record[4],
|
||
|
}
|
||
|
|
||
|
if len(record) == 6 && record[5] != "" {
|
||
|
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[5])
|
||
|
}
|
||
|
|
||
|
temperature.CreationDate = &temperatureCreationDate
|
||
|
}
|
||
|
|
||
|
if len(record) == 7 && record[6] != "" {
|
||
|
temperatureUpdateDate, err := time.Parse(timeFormat, record[6])
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[6])
|
||
|
}
|
||
|
|
||
|
temperature.UpdateDate = &temperatureUpdateDate
|
||
|
}
|
||
|
|
||
|
temperatures = append(temperatures, temperature)
|
||
|
}
|
||
|
|
||
|
return temperatures, nil
|
||
|
}
|
||
|
|
||
|
func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
||
|
|
||
|
f, err := os.Create(cl.logfile)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("%v: %v", ErrLogileCreate, cl.logfile)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
w := csv.NewWriter(f)
|
||
|
|
||
|
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)),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
w.Flush()
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
||
|
f, err := os.Create(cl.logfile)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("%v: %v", ErrLogileCreate, cl.logfile)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
writeCreationDate(temperatures)
|
||
|
|
||
|
w := csv.NewWriter(f)
|
||
|
|
||
|
for _, temperature := range temperatures {
|
||
|
record := make([]string, 0)
|
||
|
|
||
|
if temperature.UpdateDate != nil {
|
||
|
record = []string{
|
||
|
fmt.Sprintf("%v", temperature.TemperatureID),
|
||
|
fmt.Sprintf("%v", temperature.TemperatureValue),
|
||
|
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.TemperatureFromDate.Format(timeFormat)),
|
||
|
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
||
|
fmt.Sprintf("%v", temperature.SensorID),
|
||
|
fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
w.Write(record)
|
||
|
}
|
||
|
|
||
|
w.Flush()
|
||
|
|
||
|
return nil
|
||
|
}
|