fix(logfile): Add pkg to operate with logfiles

This commit is contained in:
2019-06-13 23:21:41 +02:00
parent a8aa7a14c5
commit 5666830030
4 changed files with 127 additions and 8 deletions

107
pkg/logfile/logfile.go Normal file
View File

@ -0,0 +1,107 @@
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
}

View File

@ -4,9 +4,11 @@ import "time"
// Device ...
type Device struct {
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceLocation *string `json:"device_location"`
DeviceLastContact time.Time `json:"device_last_contact"`
CreationDate time.Time `json:"creation_date"`
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceLocation *string `json:"device_location"`
DeviceLastContact time.Time `json:"device_last_contact"`
HumidityLogfile string `json:"humidity_logfile"`
TemperatureLogfile string `json:"temperature_logfile"`
CreationDate time.Time `json:"creation_date"`
}