add: termperatures log

This commit is contained in:
Markus Pesch 2018-11-30 21:50:43 +01:00
parent 7b18b115c8
commit ba9f0c59f3
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
5 changed files with 135 additions and 39 deletions

26
cmd/temperature/log.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

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