fix(pkg/logfile): support xml logfiles
This commit is contained in:
parent
440d543a83
commit
3feee7764b
@ -111,10 +111,10 @@ func New(logfile string) Logfile {
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
// case ".xml":
|
||||
// return &xmlLogfile{
|
||||
// logfile: logfile,
|
||||
// }
|
||||
case ".xml":
|
||||
return &xmlLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
default:
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
|
@ -1,11 +1,17 @@
|
||||
package logfile
|
||||
|
||||
// type Temperatures struct {
|
||||
// XMLName xml.Name `xml:"temperatures"`
|
||||
// Temperatures []*Temperature `xml:"temperature"`
|
||||
// }
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
// type Temperature struct {
|
||||
// XMLName xml.Name `xml:"temperature"`
|
||||
// *types.Temperature
|
||||
// }
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -1,112 +1,74 @@
|
||||
package logfile
|
||||
|
||||
// type xmlLogfile struct {
|
||||
// logfile string
|
||||
// }
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
// func (xl *xmlLogfile) GetLogfile() string {
|
||||
// return xl.logfile
|
||||
// }
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// 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)
|
||||
// }
|
||||
type xmlLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
// humidities := make([]*types.Humidity, 0)
|
||||
func (xl *xmlLogfile) GetLogfile() string {
|
||||
return xl.logfile
|
||||
}
|
||||
|
||||
// f, err := os.Open(xl.logfile)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||
// }
|
||||
// defer f.Close()
|
||||
func (xl *xmlLogfile) Read() ([]*types.MeasuredValue, error) {
|
||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
||||
}
|
||||
|
||||
// xmlDecoder := xml.NewDecoder(f)
|
||||
// err = xmlDecoder.Decode(&humidities)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||
// }
|
||||
f, err := os.Open(xl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// return humidities, nil
|
||||
// }
|
||||
measuredValues := new(MeasuredValues)
|
||||
|
||||
// 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)
|
||||
// }
|
||||
if err := xml.NewDecoder(f).Decode(&measuredValues); err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||
}
|
||||
|
||||
// f, err := os.Open(xl.logfile)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||
// }
|
||||
// defer f.Close()
|
||||
cachedMeasuredValues := make([]*types.MeasuredValue, 0)
|
||||
for _, measuredValue := range measuredValues.MeasuredValues {
|
||||
cachedMeasuredValues = append(cachedMeasuredValues, measuredValue.MeasuredValue)
|
||||
}
|
||||
|
||||
// bytes, err := ioutil.ReadAll(f)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
|
||||
// }
|
||||
return cachedMeasuredValues, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
|
||||
// }
|
||||
writeCreationDate(measuredValues)
|
||||
|
||||
// temperatures := make([]*types.Temperature, 0)
|
||||
cachedMeasuredValues := new(MeasuredValues)
|
||||
|
||||
// for _, tmpTemperature := range tmpTemperatures.Temperatures {
|
||||
// temperatures = append(temperatures, tmpTemperature.Temperature)
|
||||
// }
|
||||
for _, measuredValue := range measuredValues {
|
||||
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)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
||||
// }
|
||||
// defer f.Close()
|
||||
_, err = f.Write(bytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorLogfileWrite, err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// 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
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user