108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
|
package logfile
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/volker-raschek/flucky/pkg/types"
|
||
|
)
|
||
|
|
||
|
// Define the entry size for each logfile
|
||
|
var humiditySplitBy = 10000
|
||
|
var templeratureSplitBy = 10000
|
||
|
|
||
|
func WriteTemperatures(temperatureLogfile string, temperatures []*types.Temperature) error {
|
||
|
|
||
|
allTemperatures := make([]*types.Temperature, 0)
|
||
|
|
||
|
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
||
|
err := os.MkdirAll(filepath.Dir(temperatureLogfile), 0755)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not create directory %v to write temperatures into the logfile", filepath.Dir(temperatureLogfile))
|
||
|
}
|
||
|
|
||
|
f, err := os.Create(temperatureLogfile)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
} else {
|
||
|
f, err := os.Open(temperatureLogfile)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not open file %v: %v", temperatureLogfile, err)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
savedTemperatures, err := ReadTemperaturesCustom(f)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not read temperatures from logfile %v: %v", temperatureLogfile, err)
|
||
|
}
|
||
|
|
||
|
allTemperatures = append(allTemperatures, savedTemperatures...)
|
||
|
}
|
||
|
|
||
|
f, err := os.Create(temperatureLogfile)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
allTemperatures = append(allTemperatures, temperatures...)
|
||
|
|
||
|
err = WriteTemperaturesCustom(f, allTemperatures)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not write temperatures to logfile %v: %v", temperatureLogfile, err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func WriteTemperaturesCustom(w io.Writer, temperatures []*types.Temperature) error {
|
||
|
jsonEncoder := json.NewEncoder(w)
|
||
|
jsonEncoder.SetIndent("", " ")
|
||
|
err := jsonEncoder.Encode(temperatures)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not encode temperatures: %v", err)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ReadTemperatures(temperatureLogfile string) ([]*types.Temperature, error) {
|
||
|
|
||
|
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
||
|
return nil, fmt.Errorf("Can not find temperature logfile %v", temperatureLogfile)
|
||
|
}
|
||
|
|
||
|
temperatures := make([]*types.Temperature, 0)
|
||
|
|
||
|
f, err := os.Open(temperatureLogfile)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Can not open temperature logfile %v", temperatureLogfile)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
temperatures, err = ReadTemperaturesCustom(f)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Can not read temperatures from logfile %v", temperatureLogfile)
|
||
|
}
|
||
|
|
||
|
return temperatures, nil
|
||
|
}
|
||
|
|
||
|
func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) {
|
||
|
|
||
|
temperatures := make([]*types.Temperature, 0)
|
||
|
|
||
|
decoder := json.NewDecoder(r)
|
||
|
err := decoder.Decode(&temperatures)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Can not decode temperatures from reader: %v", err)
|
||
|
}
|
||
|
|
||
|
return temperatures, nil
|
||
|
}
|