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/httpcall" "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, 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 && *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) } } } // 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() // 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 } func Follow(sensorNames []string, 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, "%3.3f\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 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 } // Push ... func Push(configDir string) error { // get cnf cnf, err := config.Read(configDir) if err != nil { return err } temperatures, err := logs.ReadTemperatures(cnf.TemperatureLogfile) if err != nil { return err } if err := httpcall.SendTemperatures(temperatures, configDir); err != nil { return err } if err := logs.FlushTemperatures(cnf.TemperatureLogfile); 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, TemperatureDate: time.Now(), SensorID: sensor.SensorID, CreationDate: time.Now(), } temperatures = append(temperatures, temperature) } return temperatures, nil }