PKGBUILD/pkg/logfile/json.go

194 lines
4.5 KiB
Go

package logfile
import (
"encoding/json"
"fmt"
"log"
"os"
"sync"
"github.com/go-flucky/flucky/pkg/internal/collect"
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
)
type jsonLogfile struct {
cnf *config.Configuration
}
func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
allMeasuredValues, err := jl.Read()
if err != nil {
return err
}
log.Println(len(allMeasuredValues))
allMeasuredValues = append(allMeasuredValues, measuredValues...)
log.Println(len(allMeasuredValues))
err = jl.Write(allMeasuredValues)
if err != nil {
return err
}
return nil
}
func (jl *jsonLogfile) Read() ([]types.MeasuredValue, error) {
measuredValues := make([]types.MeasuredValue, 0)
humidityValues, err := jl.readHumidities()
if err != nil {
return nil, err
}
measuredValues = append(measuredValues, humidityValues...)
temperatureValues, err := jl.readTemperatures()
if err != nil {
return nil, err
}
measuredValues = append(measuredValues, temperatureValues...)
return measuredValues, nil
}
func (jl *jsonLogfile) readHumidities() ([]types.MeasuredValue, error) {
if _, err := os.Stat(jl.cnf.Device.HumidityLogfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.cnf.Device.HumidityLogfile)
}
humidities := make([]*types.Humidity, 0)
f, err := os.Open(jl.cnf.Device.HumidityLogfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.HumidityLogfile)
}
defer f.Close()
if err = json.NewDecoder(f).Decode(&humidities); err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
measuredValues := make([]types.MeasuredValue, 0)
for _, humidity := range humidities {
measuredValues = append(measuredValues, humidity)
}
return measuredValues, nil
}
func (jl *jsonLogfile) readTemperatures() ([]types.MeasuredValue, error) {
if _, err := os.Stat(jl.cnf.Device.TemperatureLogfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.cnf.Device.TemperatureLogfile)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(jl.cnf.Device.TemperatureLogfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.TemperatureLogfile)
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&temperatures); err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
measuredValues := make([]types.MeasuredValue, 0)
for _, temperature := range temperatures {
measuredValues = append(measuredValues, temperature)
}
return measuredValues, nil
}
func (jl *jsonLogfile) Write(measuredValues []types.MeasuredValue) error {
humidities := make([]*types.Humidity, 0)
temperatures := make([]*types.Temperature, 0)
for _, measuredValue := range measuredValues {
switch v := measuredValue.(type) {
case *types.Humidity:
humidities = append(humidities, v)
case *types.Temperature:
temperatures = append(temperatures, v)
default:
return fmt.Errorf("%v", errorTypeSwitch)
}
}
errorChannel := make(chan error, 0)
wg := new(sync.WaitGroup)
wg.Add(2)
go jl.writeHumidities(humidities, errorChannel, wg)
go jl.writeTemperatures(temperatures, errorChannel, wg)
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func (jl *jsonLogfile) writeHumidities(humidities []*types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
f, err := os.Create(jl.cnf.Device.HumidityLogfile)
if err != nil {
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.HumidityLogfile)
return
}
defer f.Close()
jsonEncoder := json.NewEncoder(f)
jsonEncoder.SetIndent("", " ")
err = jsonEncoder.Encode(humidities)
if err != nil {
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
return
}
}
func (jl *jsonLogfile) writeTemperatures(temperatures []*types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
f, err := os.Create(jl.cnf.Device.TemperatureLogfile)
if err != nil {
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.TemperatureLogfile)
return
}
defer f.Close()
//writeCreationDate(temperatures)
jsonEncoder := json.NewEncoder(f)
jsonEncoder.SetIndent("", " ")
err = jsonEncoder.Encode(temperatures)
if err != nil {
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
return
}
}