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", errorLogfileNotFound, xl.logfile) } humidities := make([]*types.Humidity, 0) f, err := os.Open(xl.logfile) if err != nil { return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile) } defer f.Close() xmlDecoder := xml.NewDecoder(f) err = xmlDecoder.Decode(&humidities) if err != nil { return nil, fmt.Errorf("%v: %v", errorLogfileDecode, 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", errorLogfileNotFound, xl.logfile) } temperatures := make([]*types.Temperature, 0) f, err := os.Open(xl.logfile) if err != nil { return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile) } defer f.Close() xmlDecoder := xml.NewDecoder(f) err = xmlDecoder.Decode(&temperatures) if err != nil { return nil, fmt.Errorf("%v: %v", errorLogfileDecode, 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", errorLogfileCreate, xl.logfile) } defer f.Close() xmlEncoder := xml.NewEncoder(f) xmlEncoder.Indent("", " ") err = xmlEncoder.Encode(humidities) if err != nil { return fmt.Errorf("%v: %v", errorLogfileEncode, 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", errorLogfileCreate, 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", errorLogfileEncode, err) } return nil }