PKGBUILD/pkg/temperature/temperature.go

293 lines
6.0 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"
2018-11-29 16:05:07 +00:00
"os"
"os/signal"
"syscall"
"text/tabwriter"
"time"
2018-11-28 17:07:20 +00:00
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
2018-11-29 19:03:42 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
2018-11-28 17:07:20 +00:00
"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"
2018-11-07 19:07:15 +00:00
)
2018-11-28 17:07:20 +00:00
var temperatureLog = "temperature.log"
2018-11-29 16:05:07 +00:00
// Get ...
2018-11-29 19:03:42 +00:00
func Get(sensorNames []string, 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
}
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
2018-11-30 20:50:43 +00:00
}
2018-11-30 15:14:47 +00:00
// get temperatures
temperatures, err := getTemperatures(sensors)
if err != nil {
return err
2018-11-29 16:05:07 +00:00
}
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
2018-11-29 16:05:07 +00:00
// 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)
2018-11-28 17:07:20 +00:00
}
}
2018-11-29 16:05:07 +00:00
fmt.Fprint(tw, "\n")
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
// body
2018-11-28 17:07:20 +00:00
for _, temperature := range temperatures {
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
}
fmt.Fprint(tw, "\n")
tw.Flush()
2018-11-29 19:03:42 +00:00
// write logfiles or push
if !push {
if err := logs.AppendTemperature(temperatures, cnf); err != nil {
return err
}
} else {
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
}
// Fetch ...
func Fetch(sensorNames []string, push bool, configDir string) error {
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
}
// get temperatures
temperatures, err := getTemperatures(sensors)
if err != nil {
return err
}
// write logfiles or push
if !push {
if err := logs.AppendTemperature(temperatures, cnf); err != nil {
return err
}
} else {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err
}
}
return nil
}
// Follow ...
2018-11-29 19:03:42 +00:00
func Follow(sensorNames []string, push bool, configDir string, w io.Writer) error {
2018-11-29 16:05:07 +00:00
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
2018-11-28 17:07:20 +00:00
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
}
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
// headlines
for _, sensor := range sensors {
if sensor.SensorName != nil && *sensor.SensorName != "" {
2018-11-29 16:05:07 +00:00
fmt.Fprintf(tw, "%v\t", *sensor.SensorName)
} else {
2018-11-29 16:05:07 +00:00
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
}
}
fmt.Fprint(tw, "\n")
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
// body
temperatures := []*stypes.Temperature{}
2018-11-29 16:05:07 +00:00
ticker := time.NewTicker(1 * time.Second)
2018-11-28 17:07:20 +00:00
// TODO: create channel for own sensor
2018-11-29 16:05:07 +00:00
go func() {
for {
select {
case _, more := <-ticker.C:
if !more {
return
}
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
// get temperatures from sensors and write them into writer
t, err := getTemperatures(sensors)
// TODO: get error back over channel
if err != nil {
return
}
// append temperatures to temperature list
temperatures = append(temperatures, t...)
for _, temperature := range t {
2018-11-29 19:03:42 +00:00
fmt.Fprintf(tw, "%3.3f\t", temperature.TemperatureValue)
2018-11-29 16:05:07 +00:00
}
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
fmt.Fprint(tw, "\n")
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
// flush writer
tw.Flush()
2018-11-28 17:07:20 +00:00
2018-11-29 16:05:07 +00:00
}
}
}()
2018-11-29 16:05:07 +00:00
signalChannel := make(chan os.Signal)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
2018-11-29 16:05:07 +00:00
sig := <-signalChannel
fmt.Printf("Got signal %s, initiating shutdown\n", sig)
ticker.Stop()
2018-11-28 17:07:20 +00:00
2018-11-29 19:03:42 +00:00
// write logfiles or push
if !push {
if err := logs.AppendTemperature(temperatures, cnf); err != nil {
return err
}
} else {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
2018-11-29 16:05:07 +00:00
return err
}
}
return nil
}
2018-11-29 19:03:42 +00:00
// Push ...
func Push(configDir string) error {
2018-11-29 19:03:42 +00:00
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
2018-11-21 19:48:10 +00:00
2018-11-30 20:50:43 +00:00
temperatures, err := logs.ReadTemperatures(cnf)
2018-11-29 19:03:42 +00:00
if err != nil {
return err
}
2018-11-29 19:03:42 +00:00
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err
}
2018-11-21 19:48:10 +00:00
2018-11-30 20:50:43 +00:00
if err := logs.FlushTemperatures(cnf); err != nil {
2018-11-29 19:03:42 +00:00
return err
}
return nil
}
2018-11-29 16:05:07 +00:00
func getTemperatures(sensors []*stypes.Sensor) ([]*stypes.Temperature, error) {
2018-11-28 17:07:20 +00:00
temperatures := []*stypes.Temperature{}
2018-11-29 16:05:07 +00:00
for _, sensor := range sensors {
t, err := ds18b20.Temperature(*sensor.WireID)
2018-11-28 17:07:20 +00:00
if err != nil {
2018-11-29 16:05:07 +00:00
return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", sensor.SensorID, err)
2018-11-28 17:07:20 +00:00
}
temperature := &stypes.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: t,
TemperatureDate: time.Now(),
2018-11-29 19:03:42 +00:00
SensorID: sensor.SensorID,
CreationDate: time.Now(),
2018-11-28 17:07:20 +00:00
}
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
}
func getSensors(sensorNames []string, cnf *types.Config) ([]*stypes.Sensor, error) {
sensors := []*stypes.Sensor{}
if len(sensorNames) > 0 {
for _, sensorName := range sensorNames {
for _, sensor := range cnf.Sensors {
if sensorName == *sensor.SensorName && *sensor.SensorEnabled ||
sensorName == sensor.SensorID && *sensor.SensorEnabled ||
sensorName == *sensor.WireID && *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
} else {
for _, sensor := range cnf.Sensors {
if *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
if len(sensors) == 0 {
return nil, fmt.Errorf("No matched Sensors. Check if your sensor is configured or enabled")
}
// check if sensor exists in cnf and has a wire id
for _, filterdSensor := range sensors {
if filterdSensor.WireID == nil || *filterdSensor.WireID == "" {
return nil, 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 nil, fmt.Errorf("Can not found sensor %v in config", filterdSensor.SensorID)
}
}
return sensors, nil
}