2019-06-19 17:18:01 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2019-06-21 11:37:29 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2019-06-19 17:18:01 +00:00
|
|
|
"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) {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
humidities := make([]*types.Humidity, 0)
|
|
|
|
|
|
|
|
f, err := os.Open(xl.logfile)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
xmlDecoder := xml.NewDecoder(f)
|
|
|
|
err = xmlDecoder.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 (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
|
|
|
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(xl.logfile)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-06-21 11:37:29 +00:00
|
|
|
bytes, err := ioutil.ReadAll(f)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-21 11:37:29 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpTemperatures := new(Temperatures)
|
|
|
|
|
|
|
|
err = xml.Unmarshal(bytes, tmpTemperatures)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(len(tmpTemperatures.Temperatures))
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
|
|
|
for _, tmpTemperature := range tmpTemperatures.Temperatures {
|
|
|
|
temperatures = append(temperatures, tmpTemperature.Temperature)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return temperatures, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
|
|
|
|
|
|
|
f, err := os.Create(xl.logfile)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
xmlEncoder := xml.NewEncoder(f)
|
|
|
|
xmlEncoder.Indent("", " ")
|
|
|
|
err = xmlEncoder.Encode(humidities)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
|
|
|
f, err := os.Create(xl.logfile)
|
|
|
|
if err != nil {
|
2019-06-21 09:48:44 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
writeCreationDate(temperatures)
|
|
|
|
|
2019-06-21 11:37:29 +00:00
|
|
|
tmpTemperatures := new(Temperatures)
|
|
|
|
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
tmpTemperature := &Temperature{
|
|
|
|
Temperature: temperature,
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpTemperatures.Temperatures = append(tmpTemperatures.Temperatures, tmpTemperature)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := xml.MarshalIndent(tmpTemperatures, "", " ")
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-21 11:37:29 +00:00
|
|
|
return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", errorLogfileWrite, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
2019-06-21 11:37:29 +00:00
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
return nil
|
|
|
|
}
|