From ba9f0c59f3e7ba778af153c6ebf897d9ad846220 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Fri, 30 Nov 2018 21:50:43 +0100 Subject: [PATCH] add: termperatures log --- cmd/temperature/log.go | 26 +++++ cmd/temperature/print.go | 23 ---- pkg/logs/temperature.go | 108 +++++++++++++++--- pkg/temperature/temperature.go | 7 +- .../go-flucky-server/pkg/types/types.go | 10 ++ 5 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 cmd/temperature/log.go delete mode 100644 cmd/temperature/print.go diff --git a/cmd/temperature/log.go b/cmd/temperature/log.go new file mode 100644 index 0000000..f1b3413 --- /dev/null +++ b/cmd/temperature/log.go @@ -0,0 +1,26 @@ +package temperature + +import ( + "log" + "os" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/logs" + "github.com/spf13/cobra" +) + +var writeLog bool + +var logTemperatureCmd = &cobra.Command{ + Use: "log", + Short: "Log print all temperatures saved from logfile on stdout", + Run: func(cmd *cobra.Command, args []string) { + + if err := logs.ListTemperatures(configDir, os.Stdout); err != nil { + log.Fatal(err) + } + }, +} + +func init() { + temperatureCmd.AddCommand(logTemperatureCmd) +} diff --git a/cmd/temperature/print.go b/cmd/temperature/print.go deleted file mode 100644 index c10033f..0000000 --- a/cmd/temperature/print.go +++ /dev/null @@ -1,23 +0,0 @@ -package temperature - -import ( - "github.com/spf13/cobra" -) - -var writeLog bool - -var printTemperatureCmd = &cobra.Command{ - Use: "print", - Short: "print all temperatures from all sensors secondly", - Run: func(cmd *cobra.Command, args []string) { - - // if err := temperature.Print(os.Stdout, configDir, writeLog); err != nil { - // log.Fatal(err) - // } - }, -} - -func init() { - temperatureCmd.AddCommand(printTemperatureCmd) - printTemperatureCmd.Flags().BoolVarP(&writeLog, "log", "l", true, "Append to logfiles") -} diff --git a/pkg/logs/temperature.go b/pkg/logs/temperature.go index 0a1b1f4..f5d2d19 100644 --- a/pkg/logs/temperature.go +++ b/pkg/logs/temperature.go @@ -3,25 +3,28 @@ package logs import ( "encoding/json" "fmt" + "io" "io/ioutil" "os" "path/filepath" + "text/tabwriter" stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) // AppendTemperature write a given array of temperatures to a logfile func AppendTemperature(temperatures []*stypes.Temperature, cnf *types.Config) error { - tLog, err := ReadTemperatures(cnf.TemperatureLogfile) + tLog, err := ReadTemperatures(cnf) if err != nil { return err } tLog = append(tLog, temperatures...) - err = WriteTemperatures(tLog, cnf.TemperatureLogfile) + err = WriteTemperatures(tLog, cnf) if err != nil { return err } @@ -30,42 +33,119 @@ func AppendTemperature(temperatures []*stypes.Temperature, cnf *types.Config) er } // FlushTemperatures ... -func FlushTemperatures(logFile string) error { - if err := ioutil.WriteFile(logFile, []byte(""), 0644); err != nil { +func FlushTemperatures(cnf *types.Config) error { + if err := ioutil.WriteFile(cnf.TemperatureLogfile, []byte(""), 0644); err != nil { return err } return nil } -// ReadTemperatures ... -func ReadTemperatures(logFile string) ([]*stypes.Temperature, error) { +// ListTemperatures ... +func ListTemperatures(configDir string, w io.Writer) error { - logPath := filepath.Dir(logFile) + cnf, err := config.Read(configDir) + if err != nil { + return err + } + + temperatures, err := ReadTemperatures(cnf) + if err != nil { + return err + } + + // tabwriter + tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0) + + sensors := []*stypes.Sensor{} + + // find all sensors which matched with one of the configuration file + for _, temperature := range temperatures { + for _, cnfSensor := range cnf.Sensors { + if temperature.SensorID == cnfSensor.SensorID { + var found bool + for _, sensor := range sensors { + if temperature.SensorID == sensor.SensorID { + found = true + } + } + + if !found { + sensors = append(sensors, cnfSensor) + } + } + } + } + + // sort temperature values to every sensor + orderedTemperatures := make(map[string][]*stypes.Temperature) + for _, temperature := range temperatures { + orderedTemperatures[temperature.SensorID] = append(orderedTemperatures[temperature.SensorID], temperature) + } + + // print headline + fmt.Fprint(tw, "\t") + for _, sensor := range sensors { + fmt.Fprintf(tw, "%v\t", sensor.HumanName()) + } + if len(sensors) > 0 { + fmt.Fprint(tw, "\n") + } + + // find max length + maxLength := 0 + for _, orderedTemperature := range orderedTemperatures { + if len(orderedTemperature) > maxLength { + maxLength = len(orderedTemperature) + } + } + + // print body + for i := 0; i < maxLength; i++ { + fmt.Fprintf(tw, "%v\t", i) + for _, sensor := range sensors { + if len(orderedTemperatures[sensor.SensorID]) > i { + fmt.Fprintf(tw, "%3.3f (%v)\t", orderedTemperatures[sensor.SensorID][i].TemperatureValue, orderedTemperatures[sensor.SensorID][i].TemperatureDate.Format("2006-01-02 15:04:05")) + } else { + fmt.Fprint(tw, "\t") + } + } + fmt.Fprint(tw, "\n") + } + + tw.Flush() + + return nil +} + +// ReadTemperatures ... +func ReadTemperatures(cnf *types.Config) ([]*stypes.Temperature, error) { + + logPath := filepath.Dir(cnf.TemperatureLogfile) // create log dir if not exist - if _, err := os.Stat(logFile); os.IsNotExist(err) { + if _, err := os.Stat(cnf.TemperatureLogfile); os.IsNotExist(err) { if _, err := os.Stat(logPath); os.IsNotExist(err) { err := os.MkdirAll(logPath, os.ModePerm) if err != nil { return nil, fmt.Errorf("Can not create directory: %v", err) } } - if err := FlushTemperatures(logFile); err != nil { + if err := FlushTemperatures(cnf); err != nil { return nil, err } } // open config file - jsonFile, err := os.Open(logFile) + jsonFile, err := os.Open(cnf.TemperatureLogfile) if err != nil { - return nil, fmt.Errorf("Can not open file %v: %v", logFile, err) + return nil, fmt.Errorf("Can not open file %v: %v", cnf.TemperatureLogfile, err) } defer jsonFile.Close() bytes, err := ioutil.ReadAll(jsonFile) if err != nil { - return nil, fmt.Errorf("Can not read jsonfile %v: %v", logFile, err) + return nil, fmt.Errorf("Can not read jsonfile %v: %v", cnf.TemperatureLogfile, err) } temperatures := []*stypes.Temperature{} @@ -75,14 +155,14 @@ func ReadTemperatures(logFile string) ([]*stypes.Temperature, error) { } // WriteTemperatures ... -func WriteTemperatures(temperatures []*stypes.Temperature, logFile string) error { +func WriteTemperatures(temperatures []*stypes.Temperature, cnf *types.Config) error { bytes, err := json.Marshal(temperatures) if err != nil { return err } - if err := ioutil.WriteFile(logFile, bytes, 0644); err != nil { + if err := ioutil.WriteFile(cnf.TemperatureLogfile, bytes, 0644); err != nil { return err } diff --git a/pkg/temperature/temperature.go b/pkg/temperature/temperature.go index 2ac5ef0..b6c9112 100644 --- a/pkg/temperature/temperature.go +++ b/pkg/temperature/temperature.go @@ -50,7 +50,10 @@ func Get(sensorNames []string, push bool, configDir string, w io.Writer) error { sensors = append(sensors, sensor) } } + } + if len(sensors) == 0 { + return fmt.Errorf("No matched Sensors. Check if your sensor is configured or enabled") } // check if sensor exists in cnf and has a wire id @@ -201,7 +204,7 @@ func Push(configDir string) error { return err } - temperatures, err := logs.ReadTemperatures(cnf.TemperatureLogfile) + temperatures, err := logs.ReadTemperatures(cnf) if err != nil { return err } @@ -210,7 +213,7 @@ func Push(configDir string) error { return err } - if err := logs.FlushTemperatures(cnf.TemperatureLogfile); err != nil { + if err := logs.FlushTemperatures(cnf); err != nil { return err } diff --git a/vendor/git.cryptic.systems/fh-trier/go-flucky-server/pkg/types/types.go b/vendor/git.cryptic.systems/fh-trier/go-flucky-server/pkg/types/types.go index 8aee04b..301f75c 100644 --- a/vendor/git.cryptic.systems/fh-trier/go-flucky-server/pkg/types/types.go +++ b/vendor/git.cryptic.systems/fh-trier/go-flucky-server/pkg/types/types.go @@ -99,6 +99,16 @@ func (s *Sensor) DecodeFromJSON(r io.Reader) error { return nil } +func (s *Sensor) HumanName() string { + if s.SensorName != nil { + return *s.SensorName + } + if s.WireID != nil { + return *s.WireID + } + return s.SensorID +} + // Temperature ... type Temperature struct { TemperatureID string `json:"temperature_id"`