fix: humidity file logger

This commit is contained in:
Markus Pesch 2019-03-04 17:56:49 +01:00
parent f01b943024
commit d988735817
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 48 additions and 56 deletions

View File

@ -4,13 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sync"
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/internal/errutils"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
@ -101,82 +97,78 @@ func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorID
return cachedTemperatures, nil
}
// LogHumidities write humidities to all configured humidity logfiles
func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
countLogfiles := len(fl.LogFiles[LogHumidity])
wg := sync.WaitGroup{}
wg.Add(countLogfiles)
errChan := make(chan error)
for _, logfile := range fl.LogFiles[LogHumidity] {
go func(humidities []*types.Humidity, logfile string) {
defer wg.Done()
// read existing humidities as bytes
bytes, err := ioutil.ReadFile(logfile)
logHumidities := []*types.Humidity{}
// if logfile already exists, read humidities from logfile
if _, err := os.Stat(logfile); err == nil {
f, err := os.Open(logfile)
if err != nil {
errChan <- err
return
return fmt.Errorf("Can not open file %v: %v", logfile, err)
}
// convert bytes into humidities
logHumidities := []*types.Humidity{}
err = json.Unmarshal(bytes, &logHumidities)
decoder := json.NewDecoder(f)
err = decoder.Decode(&logHumidities)
if err != nil {
errChan <- err
return
return fmt.Errorf("Can not decode humidities from reader")
}
// append new humidities with exsisting humidities from logfile
logHumidities = append(logHumidities, humidities...)
f.Close()
}
// convert humidities into json file
bytes, err = json.MarshalIndent(logHumidities, "", " ")
err = json.Unmarshal(bytes, &logHumidities)
if err != nil {
errChan <- err
return
}
// append new humidities with existing humidities from logfile
logHumidities = append(logHumidities, humidities...)
ioutil.WriteFile(logfile, bytes, os.ModePerm)
}(humidities, logfile)
}
wg.Wait()
errList := errutils.CollectErrors(errChan)
err := errutils.FormatErrors(errList)
if err != nil {
return err
}
return nil
}
func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) error {
for _, logfile := range fl.LogFiles[LogTemperature] {
logTemperatures := []*types.Temperature{}
// open logfile
f, err := os.OpenFile(logfile, os.O_RDWR, os.ModePerm)
// overwrite lofgile with new entries
f, err := os.Create(logfile)
if err != nil {
return fmt.Errorf("Can not open file %v: %v", logfile, err)
}
defer f.Close()
// encode humidities
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
err = encoder.Encode(logHumidities)
if err != nil {
return fmt.Errorf("Can not encode humidities for the writer")
}
}
return nil
}
// LogTemperatures write temperatures to all configured temperature logfiles
func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) error {
for _, logfile := range fl.LogFiles[LogTemperature] {
logTemperatures := []*types.Temperature{}
// if logfile already exists, read temperatures from logfile
if _, err := os.Stat(logfile); os.IsExist(err) {
if _, err := os.Stat(logfile); err == nil {
f, err := os.Open(logfile)
if err != nil {
return fmt.Errorf("Can not open file %v: %v", logfile, err)
}
decoder := json.NewDecoder(f)
err = decoder.Decode(&logTemperatures)
if err != nil {
return fmt.Errorf("Can not decode temperatures from reader")
}
f.Close()
}
// append new temperatures with exsisting humidities from logfile
// append new temperatures with existing temperatures from logfile
logTemperatures = append(logTemperatures, temperatures...)
// overwrite lofgile with new entries
f, err := os.Create(logfile)
if err != nil {
return fmt.Errorf("Can not open file %v: %v", logfile, err)
}
defer f.Close()
// encode temperatures
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")

View File

@ -8,7 +8,7 @@ import (
type Logger interface {
GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error)
GetTemperatures(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Temperature, error)
GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error)
LogHumidities(humidities []*types.Humidity) error
LogTemperatures(temperatures []*types.Temperature) error
}