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/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.Sensors) for _, sensor := range cnf.Sensors { switch { case sensor.SensorName != nil && numOfSensors <= 1: sensorNames = append(sensorNames, *sensor.SensorName) case sensor.SensorName == nil && numOfSensors <= 1: sensorNames = append(sensorNames, sensor.SensorID) case sensor.SensorName != nil && numOfSensors > 1: fmt.Fprintf(tw, "%v\t", *sensor.SensorName) sensorNames = append(sensorNames, *sensor.SensorName) break case sensor.SensorName == nil && numOfSensors > 1: sensorNames = append(sensorNames, sensor.SensorID) fmt.Fprintf(tw, "%v\t", sensor.SensorID) 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 []*stypes.Sensor var sensorFound bool for _, configSensor := range cnf.Sensors { for _, sensorName := range sensorNames { if configSensor.SensorID == sensorName || *configSensor.SensorName == 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.WireID) if err != nil { return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.SensorID, err) } temperature := &stypes.Temperature{ TemperatureID: uuid.NewV4().String(), TemperatureValue: t, SensorID: foundSensor.SensorID, TemperatureDate: time.Now(), } temperatures = append(temperatures, temperature) } return temperatures, nil }