fix(pkg/logfile): support xml logfiles

This commit is contained in:
Markus Pesch 2019-07-03 18:46:19 +02:00
parent 440d543a83
commit 3feee7764b
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 73 additions and 105 deletions

View File

@ -111,10 +111,10 @@ func New(logfile string) Logfile {
return &jsonLogfile{ return &jsonLogfile{
logfile: logfile, logfile: logfile,
} }
// case ".xml": case ".xml":
// return &xmlLogfile{ return &xmlLogfile{
// logfile: logfile, logfile: logfile,
// } }
default: default:
return &jsonLogfile{ return &jsonLogfile{
logfile: logfile, logfile: logfile,

View File

@ -1,11 +1,17 @@
package logfile package logfile
// type Temperatures struct { import (
// XMLName xml.Name `xml:"temperatures"` "encoding/xml"
// Temperatures []*Temperature `xml:"temperature"`
// }
// type Temperature struct { "github.com/go-flucky/flucky/pkg/types"
// XMLName xml.Name `xml:"temperature"` )
// *types.Temperature
// } type MeasuredValues struct {
XMLName xml.Name `xml:"measured_values"`
MeasuredValues []*MeasuredValue `xml:"measured_value"`
}
type MeasuredValue struct {
XMLName xml.Name `xml:"measured_value"`
*types.MeasuredValue
}

View File

@ -1,112 +1,74 @@
package logfile package logfile
// type xmlLogfile struct { import (
// logfile string "encoding/xml"
// } "fmt"
"os"
// func (xl *xmlLogfile) GetLogfile() string { "github.com/go-flucky/flucky/pkg/types"
// return xl.logfile )
// }
// func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) { type xmlLogfile struct {
// if _, err := os.Stat(xl.logfile); os.IsNotExist(err) { logfile string
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile) }
// }
// humidities := make([]*types.Humidity, 0) func (xl *xmlLogfile) GetLogfile() string {
return xl.logfile
}
// f, err := os.Open(xl.logfile) func (xl *xmlLogfile) Read() ([]*types.MeasuredValue, error) {
// if err != nil { if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile) return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
// } }
// defer f.Close()
// xmlDecoder := xml.NewDecoder(f) f, err := os.Open(xl.logfile)
// err = xmlDecoder.Decode(&humidities) if err != nil {
// if err != nil { return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err) }
// } defer f.Close()
// return humidities, nil measuredValues := new(MeasuredValues)
// }
// func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) { if err := xml.NewDecoder(f).Decode(&measuredValues); err != nil {
// if _, err := os.Stat(xl.logfile); os.IsNotExist(err) { return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile) }
// }
// f, err := os.Open(xl.logfile) cachedMeasuredValues := make([]*types.MeasuredValue, 0)
// if err != nil { for _, measuredValue := range measuredValues.MeasuredValues {
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile) cachedMeasuredValues = append(cachedMeasuredValues, measuredValue.MeasuredValue)
// } }
// defer f.Close()
// bytes, err := ioutil.ReadAll(f) return cachedMeasuredValues, nil
// if err != nil { }
// return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
// }
// tmpTemperatures := new(Temperatures) func (xl *xmlLogfile) Write(measuredValues []*types.MeasuredValue) error {
f, err := os.Create(xl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
}
defer f.Close()
// err = xml.Unmarshal(bytes, tmpTemperatures) writeCreationDate(measuredValues)
// if err != nil {
// return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
// }
// temperatures := make([]*types.Temperature, 0) cachedMeasuredValues := new(MeasuredValues)
// for _, tmpTemperature := range tmpTemperatures.Temperatures { for _, measuredValue := range measuredValues {
// temperatures = append(temperatures, tmpTemperature.Temperature) cachedMeasuredValue := &MeasuredValue{
// } MeasuredValue: measuredValue,
}
// return temperatures, nil cachedMeasuredValues.MeasuredValues = append(cachedMeasuredValues.MeasuredValues, cachedMeasuredValue)
// } }
// func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error { bytes, err := xml.MarshalIndent(cachedMeasuredValues, "", " ")
if err != nil {
return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
}
// f, err := os.Create(xl.logfile) _, err = f.Write(bytes)
// if err != nil { if err != nil {
// return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile) return fmt.Errorf("%v: %v", errorLogfileWrite, err)
// } }
// defer f.Close()
// xmlEncoder := xml.NewEncoder(f) return nil
// 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)
// tmpTemperatures := new(Temperatures)
// for _, temperature := range temperatures {
// tmpTemperature := &Temperature{
// Temperature: temperature,
// }
// tmpTemperatures.Temperatures = append(tmpTemperatures.Temperatures, tmpTemperature)
// }
// bytes, err := xml.MarshalIndent(tmpTemperatures, "", " ")
// if err != nil {
// return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
// }
// _, err = f.Write(bytes)
// if err != nil {
// return fmt.Errorf("%v: %v", errorLogfileWrite, err)
// }
// return nil
// }