PKGBUILD/pkg/sensor/temperature.go
Markus Pesch 6d9368e86c
fix: temperature, sensor
changes:
- sensor temperature add
- sensor temperature list
- sensor temperature rm
- temperature get
- temperature log
- temperature push
2018-11-21 19:30:14 +01:00

100 lines
1.8 KiB
Go

package sensor
import (
"fmt"
"io"
"path/filepath"
"text/tabwriter"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
func AddTemperature(sensorID, sensorName, configDir, wirePath string) error {
// read cnf file
cnf, err := config.Read(configDir)
if err != nil {
return err
}
// check if sensor exists
if !sensorExists(wirePath, sensorID) {
return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, sensorID))
}
temperatureSensor := &types.WireSensor{
ID: sensorID,
Name: sensorName,
WirePath: wirePath,
}
// append sensor to list
cnf.TemperatureSensors = append(cnf.TemperatureSensors, temperatureSensor)
// write cnf file
if err := config.Write(cnf, configDir); err != nil {
return err
}
return nil
}
func PrintTemperature(w io.Writer, configDir string, quiet bool) error {
// read cnf file
cnf, err := config.Read(configDir)
if err != nil {
return err
}
// declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
// headline
if !quiet {
fmt.Fprint(tw, "id\tname\tpath\n")
}
for _, sensor := range cnf.TemperatureSensors {
if quiet {
fmt.Fprintf(tw, "%v\n", sensor.ID)
} else {
fmt.Fprintf(tw, "%v\t%v\t%v\n", sensor.ID, sensor.Name, sensor.WirePath)
}
}
tw.Flush()
return nil
}
func RemoveTemperature(sensorID, configDir string) error {
// read cnf file
cnf, err := config.Read(configDir)
if err != nil {
return err
}
// remove matching sensor ids
var j int
for _, sensor := range cnf.TemperatureSensors {
if sensor.ID == sensorID {
cnf.TemperatureSensors = append(cnf.TemperatureSensors[:j], cnf.TemperatureSensors[j+1:]...)
if j > 0 {
j = j - 1
}
continue
}
j++
}
// write cnf file
if err := config.Write(cnf, configDir); err != nil {
return err
}
return nil
}