PKGBUILD/pkg/logs/temperature.go

171 lines
3.8 KiB
Go
Raw Normal View History

2018-11-28 17:07:20 +00:00
package logs
import (
2018-11-29 19:03:42 +00:00
"encoding/json"
2018-11-28 17:07:20 +00:00
"fmt"
2018-11-30 20:50:43 +00:00
"io"
2018-11-29 19:03:42 +00:00
"io/ioutil"
2018-11-28 17:07:20 +00:00
"os"
"path/filepath"
2018-11-30 20:50:43 +00:00
"text/tabwriter"
2018-11-28 17:07:20 +00:00
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
2018-11-30 20:50:43 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
2018-11-28 17:07:20 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
2018-11-29 19:03:42 +00:00
// AppendTemperature write a given array of temperatures to a logfile
func AppendTemperature(temperatures []*stypes.Temperature, cnf *types.Config) error {
2018-11-28 17:07:20 +00:00
2018-11-30 20:50:43 +00:00
tLog, err := ReadTemperatures(cnf)
2018-11-29 19:03:42 +00:00
if err != nil {
return err
}
tLog = append(tLog, temperatures...)
2018-11-30 20:50:43 +00:00
err = WriteTemperatures(tLog, cnf)
2018-11-29 19:03:42 +00:00
if err != nil {
return err
}
return nil
}
// FlushTemperatures ...
2018-11-30 20:50:43 +00:00
func FlushTemperatures(cnf *types.Config) error {
if err := ioutil.WriteFile(cnf.TemperatureLogfile, []byte(""), 0644); err != nil {
2018-11-29 19:03:42 +00:00
return err
}
return nil
}
2018-11-30 20:50:43 +00:00
// ListTemperatures ...
func ListTemperatures(configDir string, w io.Writer) error {
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 {
2018-12-04 18:11:50 +00:00
fmt.Fprintf(tw, "%v\t", sensor.ReadableSensorName())
2018-11-30 20:50:43 +00:00
}
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
}
2018-11-29 19:03:42 +00:00
// ReadTemperatures ...
2018-11-30 20:50:43 +00:00
func ReadTemperatures(cnf *types.Config) ([]*stypes.Temperature, error) {
2018-11-29 19:03:42 +00:00
2018-11-30 20:50:43 +00:00
logPath := filepath.Dir(cnf.TemperatureLogfile)
2018-11-28 17:07:20 +00:00
// create log dir if not exist
2018-11-30 20:50:43 +00:00
if _, err := os.Stat(cnf.TemperatureLogfile); os.IsNotExist(err) {
2018-11-29 19:03:42 +00:00
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)
}
2018-11-28 17:07:20 +00:00
}
2018-11-30 20:50:43 +00:00
if err := FlushTemperatures(cnf); err != nil {
2018-11-29 19:03:42 +00:00
return nil, err
}
2018-11-28 17:07:20 +00:00
}
2018-11-29 19:03:42 +00:00
// open config file
2018-11-30 20:50:43 +00:00
jsonFile, err := os.Open(cnf.TemperatureLogfile)
2018-11-28 17:07:20 +00:00
if err != nil {
2018-11-30 20:50:43 +00:00
return nil, fmt.Errorf("Can not open file %v: %v", cnf.TemperatureLogfile, err)
2018-11-28 17:07:20 +00:00
}
2018-11-29 19:03:42 +00:00
defer jsonFile.Close()
2018-11-28 17:07:20 +00:00
2018-11-29 19:03:42 +00:00
bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
2018-11-30 20:50:43 +00:00
return nil, fmt.Errorf("Can not read jsonfile %v: %v", cnf.TemperatureLogfile, err)
2018-11-28 17:07:20 +00:00
}
2018-11-29 19:03:42 +00:00
temperatures := []*stypes.Temperature{}
json.Unmarshal(bytes, &temperatures)
return temperatures, nil
}
// WriteTemperatures ...
2018-11-30 20:50:43 +00:00
func WriteTemperatures(temperatures []*stypes.Temperature, cnf *types.Config) error {
2018-11-29 19:03:42 +00:00
bytes, err := json.Marshal(temperatures)
2018-11-28 17:07:20 +00:00
if err != nil {
2018-11-29 19:03:42 +00:00
return err
}
2018-11-30 20:50:43 +00:00
if err := ioutil.WriteFile(cnf.TemperatureLogfile, bytes, 0644); err != nil {
2018-11-29 19:03:42 +00:00
return err
2018-11-28 17:07:20 +00:00
}
return nil
}