2019-06-19 17:18:01 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-06-23 11:33:09 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/sensor"
|
2019-06-19 17:18:01 +00:00
|
|
|
"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) {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
humidities := make([]*types.Humidity, 0)
|
|
|
|
|
|
|
|
f, err := os.Open(cl.logfile)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
jsonDecoder := json.NewDecoder(f)
|
|
|
|
err = jsonDecoder.Decode(&humidities)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return humidities, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
2019-06-22 11:13:15 +00:00
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
2019-06-22 11:13:15 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileNotFound, cl.logfile, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
|
|
|
f, err := os.Open(cl.logfile)
|
|
|
|
if err != nil {
|
2019-06-22 11:13:15 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
r := csv.NewReader(f)
|
|
|
|
records, err := r.ReadAll()
|
|
|
|
if err != nil {
|
2019-06-22 11:13:15 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
times := make([]time.Time, 0)
|
2019-06-23 11:33:09 +00:00
|
|
|
for _, j := range []int{3, 4} {
|
2019-06-19 17:18:01 +00:00
|
|
|
time, err := time.Parse(timeFormat, record[j])
|
|
|
|
if err != nil {
|
2019-06-22 11:13:15 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
times = append(times, time)
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
|
|
|
if err != nil {
|
2019-06-22 11:13:15 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 11:33:09 +00:00
|
|
|
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
|
|
|
|
}
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
temperature := &types.Temperature{
|
2019-06-23 11:33:09 +00:00
|
|
|
TemperatureID: record[0], // 0
|
|
|
|
TemperatureValue: temperatureValue, // 1
|
2019-06-23 12:17:57 +00:00
|
|
|
TemperatureUnit: measurementUnit, // 2
|
2019-06-23 11:33:09 +00:00
|
|
|
TemperatureFromDate: times[0], // 3
|
|
|
|
TemperatureTillDate: times[1], // 4
|
|
|
|
SensorID: record[5], // 5
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 12:25:58 +00:00
|
|
|
// Creation date
|
2019-06-23 11:33:09 +00:00
|
|
|
temperatureCreationDate, err := time.Parse(timeFormat, record[6])
|
2019-06-22 12:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
2019-06-22 12:25:58 +00:00
|
|
|
temperature.CreationDate = &temperatureCreationDate
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2019-06-23 11:33:09 +00:00
|
|
|
if record[7] != "" {
|
|
|
|
temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-23 11:33:09 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-06-21 09:48:44 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
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 {
|
2019-06-21 09:48:44 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
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),
|
2019-06-23 12:17:57 +00:00
|
|
|
fmt.Sprintf("%v", temperature.TemperatureUnit),
|
2019-06-19 17:18:01 +00:00
|
|
|
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),
|
2019-06-23 12:17:57 +00:00
|
|
|
fmt.Sprintf("%v", temperature.TemperatureUnit),
|
2019-06-19 17:18:01 +00:00
|
|
|
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)),
|
2019-06-22 11:13:15 +00:00
|
|
|
fmt.Sprintf(""),
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(record)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|