45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
|
package logs
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||
|
)
|
||
|
|
||
|
// Temperature write a given array of temperatures to a logfile
|
||
|
func Temperature(temperatures []*stypes.Temperature, config *types.Config) error {
|
||
|
|
||
|
logPath := filepath.Dir(config.TemperatureLogfile)
|
||
|
|
||
|
// create log dir if not exist
|
||
|
if _, err := os.Stat(logPath); os.IsNotExist(err) {
|
||
|
err := os.MkdirAll(logPath, os.ModePerm)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not create directory: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
f, err := os.OpenFile(config.TemperatureLogfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
w := bufio.NewWriter(f)
|
||
|
|
||
|
for _, temperature := range temperatures {
|
||
|
fmt.Fprintf(w, "%v\t%v\t%v\t%v\n", temperature.TemperatureID, temperature.TemperatureValue, temperature.TemperatureDate, temperature.SensorID)
|
||
|
}
|
||
|
|
||
|
err = w.Flush()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Can not flush writer: %v", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|