PKGBUILD/pkg/temperature/temperature.go

247 lines
5.5 KiB
Go
Raw Normal View History

2018-11-07 19:07:15 +00:00
package temperature
import (
"fmt"
2018-11-07 19:07:15 +00:00
"io"
"text/tabwriter"
"time"
2018-11-21 19:48:10 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
2018-11-28 17:07:20 +00:00
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/config"
uuid "github.com/satori/go.uuid"
"github.com/yryz/ds18b20"
2018-11-07 19:07:15 +00:00
)
2018-11-28 17:07:20 +00:00
var temperatureLog = "temperature.log"
func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
2018-11-07 19:07:15 +00:00
2018-11-28 17:07:20 +00:00
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
2018-11-28 17:07:20 +00:00
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
2018-11-28 17:07:20 +00:00
var sensorNames []string
2018-11-28 17:07:20 +00:00
// headlines
if len(argSensorNames) > 1 {
for _, argSensorName := range argSensorNames {
if len(argSensorNames) > 1 {
fmt.Fprintf(tw, "%v\t", argSensorName)
}
sensorNames = append(sensorNames, argSensorName)
}
fmt.Fprint(tw, "\n")
} else {
numOfSensors := len(cnf.Sensors)
2018-11-28 17:07:20 +00:00
for _, sensor := range cnf.Sensors {
2018-11-28 17:07:20 +00:00
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)
2018-11-28 17:07:20 +00:00
break
case sensor.SensorName == nil && numOfSensors > 1:
sensorNames = append(sensorNames, sensor.SensorID)
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
2018-11-28 17:07:20 +00:00
break
}
}
2018-11-28 17:07:20 +00:00
if numOfSensors > 1 {
fmt.Fprint(tw, "\n")
}
}
temperatures, err := getTemperatures(sensorNames, configDir)
if err != nil {
return err
}
2018-11-28 17:07:20 +00:00
for _, temperature := range temperatures {
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
}
2018-11-28 17:07:20 +00:00
fmt.Fprint(tw, "\n")
tw.Flush()
if writeLogfiles {
if err := logs.Temperature(temperatures, cnf); err != nil {
return err
}
2018-11-28 17:07:20 +00:00
}
2018-11-28 17:07:20 +00:00
if push {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err
}
}
2018-11-28 17:07:20 +00:00
return nil
2018-11-28 17:07:20 +00:00
}
2018-11-28 17:07:20 +00:00
func GetFollow(sensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
2018-11-28 17:07:20 +00:00
// // get cnf
// cnf, err := config.Read(configDir)
// if err != nil {
// return err
// }
2018-11-28 17:07:20 +00:00
// var temperatures []*stypes.Temperature
2018-11-28 17:07:20 +00:00
// // tabwriter
// tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
// // headlines
// var sensorNames []string
// cnt := len(cnf.TemperatureSensors)
// for _, sensor := range cnf.TemperatureSensors {
// switch {
// case sensor.Name == "" && cnt == 1:
// sensorNames = append(sensorNames, sensor.ID)
// break
// case sensor.Name != "" && cnt == 1:
// sensorNames = append(sensorNames, sensor.Name)
// break
// case sensor.Name == "" && cnt > 1:
// sensorNames = append(sensorNames, sensor.ID)
// fmt.Fprintf(tw, "%v\t", sensor.ID)
// break
// case sensor.Name != "" && cnt > 1:
// sensorNames = append(sensorNames, sensor.Name)
// fmt.Fprintf(tw, "%v\t", sensor.Name)
// break
// }
// }
// if cnt > 1 {
// fmt.Fprint(tw, "\n")
// }
// // body
// ticker := time.NewTicker(1 * time.Second)
// 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)
// }
// fmt.Fprint(tw, "\n")
// // flush writer
// tw.Flush()
// }
// }
// }()
2018-11-28 17:07:20 +00:00
// signalChannel := make(chan os.Signal)
// signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
2018-11-28 17:07:20 +00:00
// 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
}
func Push(sensorName, configDir string) error {
2018-11-28 17:07:20 +00:00
// var temperatures []*stypes.Temperature
2018-11-21 19:48:10 +00:00
2018-11-28 17:07:20 +00:00
// temperature, err := getTemperature(sensorName, configDir)
// if err != nil {
// return err
// }
2018-11-28 17:07:20 +00:00
// temperatures = append(temperatures, &temperature)
2018-11-21 19:48:10 +00:00
2018-11-28 17:07:20 +00:00
// if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
// return err
// }
return nil
}
2018-11-28 17:07:20 +00:00
func getTemperatures(sensorNames []string, configDir string) ([]*stypes.Temperature, error) {
temperatures := []*stypes.Temperature{}
// read cnf
cnf, err := config.Read(configDir)
if err != nil {
2018-11-28 17:07:20 +00:00
return []*stypes.Temperature{}, err
}
2018-11-28 17:07:20 +00:00
// search after sensor
var foundSensors []*stypes.Sensor
var sensorFound bool
for _, configSensor := range cnf.Sensors {
2018-11-28 17:07:20 +00:00
for _, sensorName := range sensorNames {
if configSensor.SensorID == sensorName || *configSensor.SensorName == sensorName {
2018-11-28 17:07:20 +00:00
foundSensors = append(foundSensors, configSensor)
sensorFound = true
}
}
}
if !sensorFound {
2018-11-28 17:07:20 +00:00
return []*stypes.Temperature{}, fmt.Errorf("Sensor not found any sensor in the list")
}
2018-11-28 17:07:20 +00:00
for _, foundSensor := range foundSensors {
t, err := ds18b20.Temperature(*foundSensor.WireID)
2018-11-28 17:07:20 +00:00
if err != nil {
return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.SensorID, err)
2018-11-28 17:07:20 +00:00
}
temperature := &stypes.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: t,
SensorID: foundSensor.SensorID,
2018-11-28 17:07:20 +00:00
TemperatureDate: time.Now(),
}
2018-11-28 17:07:20 +00:00
temperatures = append(temperatures, temperature)
}
2018-11-28 17:07:20 +00:00
return temperatures, nil
2018-11-07 19:07:15 +00:00
}