fix: humidity file logger
This commit is contained in:
parent
f01b943024
commit
d988735817
@ -4,13 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/internal/errutils"
|
|
||||||
|
|
||||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
"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
|
return cachedTemperatures, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LogHumidities write humidities to all configured humidity logfiles
|
||||||
func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
|
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] {
|
for _, logfile := range fl.LogFiles[LogHumidity] {
|
||||||
go func(humidities []*types.Humidity, logfile string) {
|
logHumidities := []*types.Humidity{}
|
||||||
defer wg.Done()
|
// if logfile already exists, read humidities from logfile
|
||||||
|
if _, err := os.Stat(logfile); err == nil {
|
||||||
// read existing humidities as bytes
|
f, err := os.Open(logfile)
|
||||||
bytes, err := ioutil.ReadFile(logfile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
return fmt.Errorf("Can not open file %v: %v", logfile, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert bytes into humidities
|
decoder := json.NewDecoder(f)
|
||||||
logHumidities := []*types.Humidity{}
|
err = decoder.Decode(&logHumidities)
|
||||||
err = json.Unmarshal(bytes, &logHumidities)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
return fmt.Errorf("Can not decode humidities from reader")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append new humidities with exsisting humidities from logfile
|
f.Close()
|
||||||
logHumidities = append(logHumidities, humidities...)
|
}
|
||||||
|
|
||||||
// convert humidities into json file
|
// append new humidities with existing humidities from logfile
|
||||||
bytes, err = json.MarshalIndent(logHumidities, "", " ")
|
logHumidities = append(logHumidities, humidities...)
|
||||||
err = json.Unmarshal(bytes, &logHumidities)
|
|
||||||
if err != nil {
|
|
||||||
errChan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ioutil.WriteFile(logfile, bytes, os.ModePerm)
|
// overwrite lofgile with new entries
|
||||||
|
f, err := os.Create(logfile)
|
||||||
}(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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Can not open file %v: %v", logfile, err)
|
return fmt.Errorf("Can not open file %v: %v", logfile, err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
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 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)
|
decoder := json.NewDecoder(f)
|
||||||
err = decoder.Decode(&logTemperatures)
|
err = decoder.Decode(&logTemperatures)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Can not decode temperatures from reader")
|
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...)
|
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
|
// encode temperatures
|
||||||
encoder := json.NewEncoder(f)
|
encoder := json.NewEncoder(f)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error)
|
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
|
LogHumidities(humidities []*types.Humidity) error
|
||||||
LogTemperatures(temperatures []*types.Temperature) error
|
LogTemperatures(temperatures []*types.Temperature) error
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user