2018-11-07 19:07:15 +00:00
|
|
|
package temperature
|
|
|
|
|
|
|
|
import (
|
2018-11-20 21:55:06 +00:00
|
|
|
"fmt"
|
2018-11-07 19:07:15 +00:00
|
|
|
"io"
|
2018-11-20 21:55:06 +00:00
|
|
|
"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"
|
2018-11-20 21:55:06 +00:00
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
|
|
|
|
|
|
|
"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)
|
2018-11-20 21:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
var sensorNames []string
|
2018-11-20 21:55:06 +00:00
|
|
|
|
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.TemperatureSensors)
|
|
|
|
|
|
|
|
for _, temperatureSensor := range cnf.TemperatureSensors {
|
|
|
|
switch {
|
|
|
|
case temperatureSensor.Name != "" && numOfSensors <= 1:
|
|
|
|
sensorNames = append(sensorNames, temperatureSensor.Name)
|
|
|
|
case temperatureSensor.Name == "" && numOfSensors <= 1:
|
|
|
|
sensorNames = append(sensorNames, temperatureSensor.ID)
|
|
|
|
case temperatureSensor.Name != "" && numOfSensors > 1:
|
|
|
|
fmt.Fprintf(tw, "%v\t", temperatureSensor.Name)
|
|
|
|
sensorNames = append(sensorNames, temperatureSensor.Name)
|
|
|
|
break
|
|
|
|
case temperatureSensor.Name == "" && numOfSensors > 1:
|
|
|
|
sensorNames = append(sensorNames, temperatureSensor.ID)
|
|
|
|
fmt.Fprintf(tw, "%v\t", temperatureSensor.ID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
if numOfSensors > 1 {
|
|
|
|
fmt.Fprint(tw, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatures, err := getTemperatures(sensorNames, configDir)
|
2018-11-20 21:55:06 +00:00
|
|
|
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-20 21:55:06 +00:00
|
|
|
|
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-20 21:55:06 +00:00
|
|
|
}
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
if push {
|
|
|
|
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-20 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
return nil
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
2018-11-20 21:55:06 +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-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// // get cnf
|
|
|
|
// cnf, err := config.Read(configDir)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// var temperatures []*stypes.Temperature
|
2018-11-20 21:55:06 +00:00
|
|
|
|
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-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// signalChannel := make(chan os.Signal)
|
|
|
|
// signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
|
2018-11-20 21:55:06 +00:00
|
|
|
|
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
|
|
|
|
// }
|
|
|
|
// }
|
2018-11-20 21:55:06 +00:00
|
|
|
|
|
|
|
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-20 21:55:06 +00:00
|
|
|
|
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
|
|
|
|
// }
|
2018-11-20 21:55:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
func getTemperatures(sensorNames []string, configDir string) ([]*stypes.Temperature, error) {
|
|
|
|
|
|
|
|
temperatures := []*stypes.Temperature{}
|
|
|
|
|
|
|
|
// read cnf
|
2018-11-20 21:55:06 +00:00
|
|
|
cnf, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
2018-11-28 17:07:20 +00:00
|
|
|
return []*stypes.Temperature{}, err
|
2018-11-20 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// search after sensor
|
|
|
|
var foundSensors []*types.WireSensor
|
2018-11-20 21:55:06 +00:00
|
|
|
var sensorFound bool
|
2018-11-28 17:07:20 +00:00
|
|
|
for _, configSensor := range cnf.TemperatureSensors {
|
|
|
|
for _, sensorName := range sensorNames {
|
|
|
|
if configSensor.ID == sensorName || configSensor.Name == sensorName {
|
|
|
|
foundSensors = append(foundSensors, configSensor)
|
|
|
|
sensorFound = true
|
|
|
|
}
|
2018-11-20 21:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-20 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
for _, foundSensor := range foundSensors {
|
|
|
|
t, err := ds18b20.Temperature(foundSensor.ID)
|
|
|
|
if err != nil {
|
|
|
|
return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
temperature := &stypes.Temperature{
|
|
|
|
TemperatureID: uuid.NewV4().String(),
|
|
|
|
TemperatureValue: t,
|
|
|
|
SensorID: foundSensor.ID,
|
|
|
|
TemperatureDate: time.Now(),
|
|
|
|
DeviceID: cnf.DeviceID,
|
|
|
|
}
|
2018-11-20 21:55:06 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
temperatures = append(temperatures, temperature)
|
2018-11-20 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
return temperatures, nil
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|