feat(pkg/logfile): support csv and xml logfile
This commit is contained in:
178
pkg/logfile/csv.go
Normal file
178
pkg/logfile/csv.go
Normal file
@ -0,0 +1,178 @@
|
||||
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
|
||||
}
|
11
pkg/logfile/errors.go
Normal file
11
pkg/logfile/errors.go
Normal file
@ -0,0 +1,11 @@
|
||||
package logfile
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrLogfileNotFound = errors.New("Can not find logfile")
|
||||
var ErrLogileCreate = errors.New("Can not create logfile")
|
||||
var ErrLogfileDecode = errors.New("Can not decode from reader")
|
||||
var ErrLogfileEncode = errors.New("Cano not encode from writer")
|
||||
var ErrLogfileOpen = errors.New("Can not open logfile")
|
||||
var ErrParseFloat = errors.New("Can not parse float")
|
||||
var ErrParseTime = errors.New("Can not parse time")
|
13
pkg/logfile/interface.go
Normal file
13
pkg/logfile/interface.go
Normal file
@ -0,0 +1,13 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type Logfile interface {
|
||||
GetLogfile() string
|
||||
ReadHumidities() ([]*types.Humidity, error)
|
||||
ReadTemperatures() ([]*types.Temperature, error)
|
||||
WriteHumidities(humidities []*types.Humidity) error
|
||||
WriteTemperatures(temperatures []*types.Temperature) error
|
||||
}
|
96
pkg/logfile/json.go
Normal file
96
pkg/logfile/json.go
Normal file
@ -0,0 +1,96 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type jsonLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) GetLogfile() string {
|
||||
return jl.logfile
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, jl.logfile)
|
||||
}
|
||||
|
||||
humidities := make([]*types.Humidity, 0)
|
||||
|
||||
f, err := os.Open(jl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, jl.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 (jl *jsonLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, jl.logfile)
|
||||
}
|
||||
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
|
||||
f, err := os.Open(jl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, jl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
jsonDecoder := json.NewDecoder(f)
|
||||
err = jsonDecoder.Decode(&temperatures)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
|
||||
}
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
||||
|
||||
f, err := os.Create(jl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogileCreate, jl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
jsonEncoder := json.NewEncoder(f)
|
||||
jsonEncoder.SetIndent("", " ")
|
||||
err = jsonEncoder.Encode(humidities)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
||||
f, err := os.Create(jl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogileCreate, jl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
writeCreationDate(temperatures)
|
||||
|
||||
jsonEncoder := json.NewEncoder(f)
|
||||
jsonEncoder.SetIndent("", " ")
|
||||
err = jsonEncoder.Encode(temperatures)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@ -12,6 +9,33 @@ import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// AppendTemperatures with temperature values from a logfile. As additional option it's possible to compress the temperature data.
|
||||
func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types.Temperature) error {
|
||||
|
||||
allTemperatures := make([]*types.Temperature, 0)
|
||||
|
||||
if _, err := os.Stat(logfile.GetLogfile()); err == nil {
|
||||
temperaturesFromLogfile, err := logfile.ReadTemperatures()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
allTemperatures = append(allTemperatures, temperaturesFromLogfile...)
|
||||
}
|
||||
|
||||
allTemperatures = append(allTemperatures, temperatures...)
|
||||
|
||||
if compression {
|
||||
allTemperatures = CompressTemperature(allTemperatures)
|
||||
}
|
||||
|
||||
err := logfile.WriteTemperatures(allTemperatures)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompressTemperature compresses the temperatures from an array. It is checked
|
||||
// whether the measured temperature of a value corresponds to that of the
|
||||
// predecessor. If this is the case, the current value is discarded and the
|
||||
@ -51,60 +75,33 @@ func CompressTemperature(temperatures []*types.Temperature) []*types.Temperature
|
||||
return compressedTemperatures
|
||||
}
|
||||
|
||||
// ReadTemperatures from a file and returns an array with temperatures
|
||||
func ReadTemperatures(temperatureLogfile string) ([]*types.Temperature, error) {
|
||||
// New returns a log file with basic functions for reading and writing data.
|
||||
// The file extension of the logfile is taken into account to format the logfile
|
||||
// into the correct format.
|
||||
func New(logfile string) Logfile {
|
||||
|
||||
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("Can not find temperature logfile %v", temperatureLogfile)
|
||||
}
|
||||
ext := filepath.Ext(logfile)
|
||||
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
|
||||
f, err := os.Open(temperatureLogfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not open temperature logfile %v", temperatureLogfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
temperatures, err = ReadTemperaturesCustom(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not read temperatures from logfile %v", temperatureLogfile)
|
||||
}
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
// ReadTemperaturesChannel reads temperatures from a channel until it is closed
|
||||
// and returns the temperature.
|
||||
func ReadTemperaturesChannel(temperatureChannel <-chan *types.Temperature) []*types.Temperature {
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
for {
|
||||
select {
|
||||
case temperature, more := <-temperatureChannel:
|
||||
if more {
|
||||
temperatures = append(temperatures, temperature)
|
||||
}
|
||||
default:
|
||||
return temperatures
|
||||
switch ext {
|
||||
case ".csv":
|
||||
return &csvLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
case ".json":
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
case ".xml":
|
||||
return &xmlLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
default:
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadTemperaturesCustom from a custom reader and returns an array with
|
||||
// temperatures
|
||||
func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) {
|
||||
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
|
||||
decoder := json.NewDecoder(r)
|
||||
err := decoder.Decode(&temperatures)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not decode temperatures from reader: %v", err)
|
||||
}
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
// SplittTemperatures into multiple arrays. The Size can be defined by
|
||||
// temperatureSplitBy parameter.
|
||||
func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
|
||||
@ -127,80 +124,3 @@ func SortTemperatures(temperatures []*types.Temperature) {
|
||||
return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
|
||||
})
|
||||
}
|
||||
|
||||
// WriteTemperatures encode temperatures into json and write it into a file.
|
||||
// Compression can be enabled over a bolean parameter
|
||||
func WriteTemperatures(temperatures []*types.Temperature, temperatureLogfile string, compression bool) error {
|
||||
|
||||
allTemperatures := make([]*types.Temperature, 0)
|
||||
|
||||
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(filepath.Dir(temperatureLogfile), 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create directory %v to write temperatures into the logfile", filepath.Dir(temperatureLogfile))
|
||||
}
|
||||
|
||||
f, err := os.Create(temperatureLogfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
} else {
|
||||
f, err := os.Open(temperatureLogfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not open file %v: %v", temperatureLogfile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
savedTemperatures, err := ReadTemperaturesCustom(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not read temperatures from logfile %v: %v", temperatureLogfile, err)
|
||||
}
|
||||
|
||||
allTemperatures = append(allTemperatures, savedTemperatures...)
|
||||
}
|
||||
|
||||
f, err := os.Create(temperatureLogfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
allTemperatures = append(allTemperatures, temperatures...)
|
||||
|
||||
err = WriteTemperaturesCustom(allTemperatures, f, compression)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not write temperatures to logfile %v: %v", temperatureLogfile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteTemperaturesCustom encode temperatures into json and write it into
|
||||
// custom writer. Compression can be enabled over a bolean parameter
|
||||
func WriteTemperaturesCustom(temperatures []*types.Temperature, w io.Writer, compression bool) error {
|
||||
|
||||
writeCreationDate(temperatures)
|
||||
|
||||
if compression {
|
||||
temperatures = CompressTemperature(temperatures)
|
||||
}
|
||||
|
||||
jsonEncoder := json.NewEncoder(w)
|
||||
jsonEncoder.SetIndent("", " ")
|
||||
err := jsonEncoder.Encode(temperatures)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not encode temperatures: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeCreationDate(temperatures []*types.Temperature) {
|
||||
now := time.Now()
|
||||
for _, temperature := range temperatures {
|
||||
if temperature.CreationDate == nil {
|
||||
temperature.CreationDate = &now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package logfile_test
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLogfile(t *testing.T) {
|
||||
|
||||
}
|
19
pkg/logfile/utils.go
Normal file
19
pkg/logfile/utils.go
Normal file
@ -0,0 +1,19 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
//.999999999 -0700 MST
|
||||
var timeFormat = "2006-01-02 15:04:05.999999999 -0700"
|
||||
|
||||
func writeCreationDate(temperatures []*types.Temperature) {
|
||||
now := time.Now()
|
||||
for _, temperature := range temperatures {
|
||||
if temperature.CreationDate == nil {
|
||||
temperature.CreationDate = &now
|
||||
}
|
||||
}
|
||||
}
|
96
pkg/logfile/xml.go
Normal file
96
pkg/logfile/xml.go
Normal file
@ -0,0 +1,96 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type xmlLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) GetLogfile() string {
|
||||
return xl.logfile
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, xl.logfile)
|
||||
}
|
||||
|
||||
humidities := make([]*types.Humidity, 0)
|
||||
|
||||
f, err := os.Open(xl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
xmlDecoder := xml.NewDecoder(f)
|
||||
err = xmlDecoder.Decode(&humidities)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
|
||||
}
|
||||
|
||||
return humidities, nil
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, xl.logfile)
|
||||
}
|
||||
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
|
||||
f, err := os.Open(xl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
xmlDecoder := xml.NewDecoder(f)
|
||||
err = xmlDecoder.Decode(&temperatures)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
|
||||
}
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
||||
|
||||
f, err := os.Create(xl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogileCreate, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
xmlEncoder := xml.NewEncoder(f)
|
||||
xmlEncoder.Indent("", " ")
|
||||
err = xmlEncoder.Encode(humidities)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
||||
f, err := os.Create(xl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogileCreate, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
writeCreationDate(temperatures)
|
||||
|
||||
xmlEncoder := xml.NewEncoder(f)
|
||||
xmlEncoder.Indent("", " ")
|
||||
err = xmlEncoder.Encode(temperatures)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user