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