fix: humidity file logger
This commit is contained in:
parent
f01b943024
commit
d988735817
@ -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)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
// convert bytes into humidities
|
||||
logHumidities := []*types.Humidity{}
|
||||
err = json.Unmarshal(bytes, &logHumidities)
|
||||
// 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)
|
||||
}
|
||||
|
||||
// append new humidities with exsisting humidities from logfile
|
||||
decoder := json.NewDecoder(f)
|
||||
err = decoder.Decode(&logHumidities)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not decode humidities from reader")
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}
|
||||
|
||||
// append new humidities with existing humidities from logfile
|
||||
logHumidities = append(logHumidities, humidities...)
|
||||
|
||||
// convert humidities into json file
|
||||
bytes, err = json.MarshalIndent(logHumidities, "", " ")
|
||||
err = json.Unmarshal(bytes, &logHumidities)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
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("", " ")
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user