fix(logfile): Add pkg to operate with logfiles

This commit is contained in:
Markus Pesch 2019-06-13 23:21:41 +02:00
parent a8aa7a14c5
commit 5666830030
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
4 changed files with 127 additions and 8 deletions

View File

@ -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(),
},
}

View File

@ -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)
}
}
},
}

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"`
}