PKGBUILD/pkg/temperature/temperature.go

226 lines
4.8 KiB
Go

package temperature
import (
"fmt"
"io"
"os"
"os/signal"
"syscall"
"text/tabwriter"
"time"
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"
)
var temperatureLog = "temperature.log"
// Get ...
func Get(sensorNames []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)
// 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)
}
}
}
} else {
sensors = append(sensors, cnf.Sensors...)
}
// 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)
}
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)
}
}
// 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 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 sensors []*stypes.Sensor
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
}
}
if numOfSensors > 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(sensors)
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(sensors []*stypes.Sensor) ([]*stypes.Temperature, error) {
temperatures := []*stypes.Temperature{}
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", sensor.SensorID, err)
}
temperature := &stypes.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: t,
SensorID: sensor.SensorID,
TemperatureDate: time.Now(),
}
temperatures = append(temperatures, temperature)
}
return temperatures, nil
}