97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
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
|
|
}
|