fix: temperatures get

This commit is contained in:
Markus Pesch 2018-11-29 17:05:07 +01:00
parent f8e829d3d2
commit e9630f5a19
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -3,11 +3,12 @@ package temperature
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"os/signal"
"syscall"
"text/tabwriter" "text/tabwriter"
"time" "time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
"git.cryptic.systems/fh-trier/go-flucky/pkg/logs" "git.cryptic.systems/fh-trier/go-flucky/pkg/logs"
@ -19,7 +20,8 @@ import (
var temperatureLog = "temperature.log" var temperatureLog = "temperature.log"
func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error { // Get ...
func Get(sensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
// get cnf // get cnf
cnf, err := config.Read(configDir) cnf, err := config.Read(configDir)
@ -29,66 +31,71 @@ func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0) tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
var sensorNames []string // filter sensors
var sensors []*stypes.Sensor
// headlines if len(sensorNames) > 0 {
if len(argSensorNames) > 1 { for _, sensorName := range sensorNames {
for _, argSensorName := range argSensorNames { for _, sensor := range cnf.Sensors {
if len(argSensorNames) > 1 { if sensorName == *sensor.SensorName ||
fmt.Fprintf(tw, "%v\t", argSensorName) sensorName == sensor.SensorID {
sensors = append(sensors, sensor)
}
} }
sensorNames = append(sensorNames, argSensorName)
} }
fmt.Fprint(tw, "\n")
} else { } else {
numOfSensors := len(cnf.Sensors) sensors = append(sensors, cnf.Sensors...)
}
for _, sensor := range cnf.Sensors { // check if sensor exists in cnf and has a wire id
switch { for _, filterdSensor := range sensors {
case sensor.SensorName != nil && numOfSensors <= 1: if filterdSensor.WireID == nil || *filterdSensor.WireID == "" {
sensorNames = append(sensorNames, *sensor.SensorName) return fmt.Errorf("Sensor %v has no wire id", filterdSensor.SensorID)
case sensor.SensorName == nil && numOfSensors <= 1:
sensorNames = append(sensorNames, sensor.SensorID)
case sensor.SensorName != nil && numOfSensors > 1:
fmt.Fprintf(tw, "%v\t", *sensor.SensorName)
sensorNames = append(sensorNames, *sensor.SensorName)
break
case sensor.SensorName == nil && numOfSensors > 1:
sensorNames = append(sensorNames, sensor.SensorID)
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
break
}
} }
if numOfSensors > 1 { var found bool
fmt.Fprint(tw, "\n") for _, cnfSensor := range cnf.Sensors {
if filterdSensor.SensorID == cnfSensor.SensorID {
found = true
}
}
if !found {
return fmt.Errorf("Can not found sensor %v in config", filterdSensor.SensorID)
} }
} }
temperatures, err := getTemperatures(sensorNames, configDir) // headlines
for _, sensor := range sensors {
if sensor.SensorName != nil && *sensor.SensorName != "" {
fmt.Fprintf(tw, "%v\t", *sensor.SensorName)
} else {
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
}
}
fmt.Fprint(tw, "\n")
// body
temperatures, err := getTemperatures(sensors)
if err != nil { if err != nil {
return err return err
} }
for _, temperature := range temperatures { for _, temperature := range temperatures {
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue) fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
} }
fmt.Fprint(tw, "\n") fmt.Fprint(tw, "\n")
tw.Flush() tw.Flush()
if writeLogfiles { // if writeLogfiles {
if err := logs.Temperature(temperatures, cnf); err != nil { // if err := logs.Temperature(temperatures, cnf); err != nil {
return err // return err
} // }
} // }
if push { // if push {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil { // if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err // return err
} // }
} // }
return nil return nil
@ -96,87 +103,81 @@ func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w
func GetFollow(sensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error { func GetFollow(sensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
// // get cnf // get cnf
// cnf, err := config.Read(configDir) cnf, err := config.Read(configDir)
// if err != nil { if err != nil {
// return err return err
// } }
// var temperatures []*stypes.Temperature var temperatures []*stypes.Temperature
// // tabwriter // tabwriter
// tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0) tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
// // headlines // headlines
// var sensorNames []string var sensors []*stypes.Sensor
// cnt := len(cnf.TemperatureSensors)
// for _, sensor := range cnf.TemperatureSensors {
// switch { numOfSensors := len(cnf.Sensors)
// case sensor.Name == "" && cnt == 1: for _, sensor := range cnf.Sensors {
// sensorNames = append(sensorNames, sensor.ID) switch {
// break case sensor.SensorName != nil && numOfSensors <= 1:
sensors = append(sensors, sensor)
case sensor.SensorName == nil && numOfSensors <= 1:
sensors = append(sensors, sensor)
case sensor.SensorName != nil && numOfSensors > 1:
fmt.Fprintf(tw, "%v\t", *sensor.SensorName)
sensors = append(sensors, sensor)
break
case sensor.SensorName == nil && numOfSensors > 1:
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
sensors = append(sensors, sensor)
break
}
}
// case sensor.Name != "" && cnt == 1: if numOfSensors > 1 {
// sensorNames = append(sensorNames, sensor.Name) fmt.Fprint(tw, "\n")
// break }
// case sensor.Name == "" && cnt > 1: // body
// sensorNames = append(sensorNames, sensor.ID) ticker := time.NewTicker(1 * time.Second)
// fmt.Fprintf(tw, "%v\t", sensor.ID)
// break
// case sensor.Name != "" && cnt > 1: go func() {
// sensorNames = append(sensorNames, sensor.Name) for {
// fmt.Fprintf(tw, "%v\t", sensor.Name) select {
// break case _, more := <-ticker.C:
if !more {
return
}
// } // get temperatures from sensors and write them into writer
// } temperatures, err = getTemperatures(sensors)
for _, temperature := range temperatures {
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
}
// if cnt > 1 { fmt.Fprint(tw, "\n")
// fmt.Fprint(tw, "\n")
// }
// // body // flush writer
// ticker := time.NewTicker(1 * time.Second) tw.Flush()
// go func() { }
// for { }
// select { }()
// case _, more := <-ticker.C:
// if !more {
// return
// }
// // get temperatures from sensors and write them into writer signalChannel := make(chan os.Signal)
// temperatures, err = getTemperatures(sensorNames, configDir) signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
// for _, temperature := range temperatures {
// fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
// }
// fmt.Fprint(tw, "\n") sig := <-signalChannel
fmt.Printf("Got signal %s, initiating shutdown\n", sig)
ticker.Stop()
// // flush writer // write logfiles
// tw.Flush() if writeLogfiles {
if err := logs.Temperature(temperatures, cnf); err != nil {
// } return err
// } }
// }() }
// signalChannel := make(chan os.Signal)
// signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
// sig := <-signalChannel
// fmt.Printf("Got signal %s, initiating shutdown\n", sig)
// ticker.Stop()
// // write logfiles
// if writeLogfiles {
// if err := logs.Temperature(temperatures, cnf); err != nil {
// return err
// }
// }
return nil return nil
} }
@ -200,42 +201,20 @@ func Push(sensorName, configDir string) error {
} }
func getTemperatures(sensorNames []string, configDir string) ([]*stypes.Temperature, error) { func getTemperatures(sensors []*stypes.Sensor) ([]*stypes.Temperature, error) {
temperatures := []*stypes.Temperature{} temperatures := []*stypes.Temperature{}
// read cnf for _, sensor := range sensors {
cnf, err := config.Read(configDir) t, err := ds18b20.Temperature(*sensor.WireID)
if err != nil {
return []*stypes.Temperature{}, err
}
// search after sensor
var foundSensors []*stypes.Sensor
var sensorFound bool
for _, configSensor := range cnf.Sensors {
for _, sensorName := range sensorNames {
if configSensor.SensorID == sensorName || *configSensor.SensorName == sensorName {
foundSensors = append(foundSensors, configSensor)
sensorFound = true
}
}
}
if !sensorFound {
return []*stypes.Temperature{}, fmt.Errorf("Sensor not found any sensor in the list")
}
for _, foundSensor := range foundSensors {
t, err := ds18b20.Temperature(*foundSensor.WireID)
if err != nil { if err != nil {
return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.SensorID, err) return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", sensor.SensorID, err)
} }
temperature := &stypes.Temperature{ temperature := &stypes.Temperature{
TemperatureID: uuid.NewV4().String(), TemperatureID: uuid.NewV4().String(),
TemperatureValue: t, TemperatureValue: t,
SensorID: foundSensor.SensorID, SensorID: sensor.SensorID,
TemperatureDate: time.Now(), TemperatureDate: time.Now(),
} }