2019-06-19 17:18:01 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-06-25 20:22:34 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/internal/collect"
|
|
|
|
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
2019-06-19 17:18:01 +00:00
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonLogfile struct {
|
2019-06-25 20:22:34 +00:00
|
|
|
cnf *config.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
|
|
|
|
|
|
|
allMeasuredValues, err := jl.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
|
|
|
|
|
|
|
err = jl.Write(allMeasuredValues)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
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
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
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)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
humidities := make([]*types.Humidity, 0)
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
f, err := os.Open(jl.cnf.Device.HumidityLogfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.HumidityLogfile)
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
if err = json.NewDecoder(f).Decode(&humidities); 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
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
measuredValues := make([]types.MeasuredValue, 0)
|
|
|
|
|
|
|
|
for _, humidity := range humidities {
|
|
|
|
measuredValues = append(measuredValues, humidity)
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
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)
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
f, err := os.Open(jl.cnf.Device.TemperatureLogfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.TemperatureLogfile)
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&temperatures); 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
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
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
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
func (jl *jsonLogfile) writeHumidities(humidities []*types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
f, err := os.Create(jl.cnf.Device.HumidityLogfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.HumidityLogfile)
|
|
|
|
return
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
jsonEncoder := json.NewEncoder(f)
|
|
|
|
jsonEncoder.SetIndent("", " ")
|
|
|
|
err = jsonEncoder.Encode(humidities)
|
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
|
|
|
return
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
func (jl *jsonLogfile) writeTemperatures(temperatures []*types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
f, err := os.Create(jl.cnf.Device.TemperatureLogfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.TemperatureLogfile)
|
|
|
|
return
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
//writeCreationDate(temperatures)
|
2019-06-19 17:18:01 +00:00
|
|
|
|
|
|
|
jsonEncoder := json.NewEncoder(f)
|
|
|
|
jsonEncoder.SetIndent("", " ")
|
|
|
|
err = jsonEncoder.Encode(temperatures)
|
|
|
|
if err != nil {
|
2019-06-25 20:22:34 +00:00
|
|
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
|
|
|
return
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
}
|