PKGBUILD/pkg/temperature/temperature.go
2018-11-28 18:07:20 +01:00

249 lines
5.6 KiB
Go

package temperature
import (
"fmt"
"io"
"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"
"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"
)
var temperatureLog = "temperature.log"
func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
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)
}
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
}
}
if numOfSensors > 1 {
fmt.Fprint(tw, "\n")
}
}
temperatures, err := getTemperatures(sensorNames, configDir)
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 push {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err
}
}
return nil
}
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
// }
// var temperatures []*stypes.Temperature
// // 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()
// }
// }
// }()
// 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
}
func Push(sensorName, configDir string) error {
// var temperatures []*stypes.Temperature
// temperature, err := getTemperature(sensorName, configDir)
// if err != nil {
// return err
// }
// temperatures = append(temperatures, &temperature)
// if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
// return err
// }
return nil
}
func getTemperatures(sensorNames []string, configDir string) ([]*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 []*types.WireSensor
var sensorFound bool
for _, configSensor := range cnf.TemperatureSensors {
for _, sensorName := range sensorNames {
if configSensor.ID == sensorName || configSensor.Name == 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.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,
}
temperatures = append(temperatures, temperature)
}
return temperatures, nil
}