From 5666830030812779d6a32d84886cfb889ea84056 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 13 Jun 2019 23:21:41 +0200 Subject: [PATCH] fix(logfile): Add pkg to operate with logfiles --- cmd/cmd.go | 8 +-- cmd/temperature/read.go | 8 +++ pkg/logfile/logfile.go | 107 ++++++++++++++++++++++++++++++++++++++++ pkg/types/device.go | 12 +++-- 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 pkg/logfile/logfile.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 8feec6d..6102c7f 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -30,9 +30,11 @@ var rootCmd = &cobra.Command{ cnf := config.Configuration{ Device: &types.Device{ - DeviceID: uuid.NewV4().String(), - DeviceName: hostname, - CreationDate: time.Now(), + DeviceID: uuid.NewV4().String(), + DeviceName: hostname, + HumidityLogfile: "/var/log/flucky/humidity.json", + TemperatureLogfile: "/var/log/flucky/temperature.json", + CreationDate: time.Now(), }, } diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 4c2ef5d..e991938 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/volker-raschek/flucky/pkg/cli" "github.com/volker-raschek/flucky/pkg/config" + "github.com/volker-raschek/flucky/pkg/logfile" "github.com/volker-raschek/flucky/pkg/sensor" ) @@ -36,6 +37,13 @@ var readTemperatureCmd = &cobra.Command{ // print temperatures on stdout cli.PrintTemperatures(temperatures, cnf, os.Stdout) + + if logs { + err := logfile.WriteTemperatures(cnf.Device.TemperatureLogfile, temperatures) + if err != nil { + log.Fatalln(err) + } + } }, } diff --git a/pkg/logfile/logfile.go b/pkg/logfile/logfile.go new file mode 100644 index 0000000..8ed5841 --- /dev/null +++ b/pkg/logfile/logfile.go @@ -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 +} diff --git a/pkg/types/device.go b/pkg/types/device.go index d7b791e..924d66e 100644 --- a/pkg/types/device.go +++ b/pkg/types/device.go @@ -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"` }