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) {
|
|
||||||
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{}
|
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 {
|
if err != nil {
|
||||||
errChan <- err
|
return fmt.Errorf("Can not open file %v: %v", logfile, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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...)
|
logHumidities = append(logHumidities, humidities...)
|
||||||
|
|
||||||
// convert humidities into json file
|
// overwrite lofgile with new entries
|
||||||
bytes, err = json.MarshalIndent(logHumidities, "", " ")
|
f, err := os.Create(logfile)
|
||||||
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)
|
|
||||||
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