package sensor import ( "fmt" "io" "os" "path/filepath" "text/tabwriter" "time" "github.com/satori/go.uuid" stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) // Add ... func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled bool) error { // read cnf file cnf, err := config.Read(configDir) if err != nil { return err } s := &types.Sensor{ Enabled: enabled, RemoteSensor: &stypes.Sensor{ SensorID: uuid.NewV4().String(), SensorName: sensorName, SensorLocation: sensorLocation, GPIONumber: gpioNumber, SensorType: sensorType, DeviceID: cnf.DeviceID, CreationDate: time.Now(), }, } // If the new sensor is a wire sensor if wireID != nil { // check if sensor exists if !exists(wirePath, *wireID) { return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, *wireID)) } // check if sensor is redundant if isWireIDRedundant(*wireID, cnf.Sensors) { return fmt.Errorf("Sensor %v already exists", *wireID) } s.RemoteSensor.WireID = wireID } // check if sensor is redundant if isSensorNameRedundant(*sensorName, cnf.Sensors) { return fmt.Errorf("Sensor %v already exists", *sensorName) } // append sensor to list cnf.Sensors = append(cnf.Sensors, s) // write cnf file if err := config.Write(cnf, configDir); err != nil { return err } return nil } // Enable a remote link func Enable(sensorName string, configDir string) error { cnf, err := config.Read(configDir) if err != nil { return err } // search after duplicate remote_names var found bool for _, sensor := range cnf.Sensors { if *sensor.RemoteSensor.SensorName == sensorName || sensor.RemoteSensor.SensorID == sensorName { sensor.Enabled = true found = true break } } if !found { return fmt.Errorf("Can not find sensor %v", sensorName) } if err := config.Write(cnf, configDir); err != nil { return err } return nil } // Disable a remote link func Disable(sensorName string, configDir string) error { cnf, err := config.Read(configDir) if err != nil { return err } // search after duplicate remote_names var found bool for _, sensor := range cnf.Sensors { if *sensor.RemoteSensor.SensorName == sensorName || sensor.RemoteSensor.SensorID == sensorName { sensor.Enabled = false found = true break } } if !found { return fmt.Errorf("Can not find sensor %v", sensorName) } if err := config.Write(cnf, configDir); err != nil { return err } return nil } // Print a list with the given writer w over all temperature sensors func Print(w io.Writer, configDir string, quiet bool) error { // read cnf file cnf, err := config.Read(configDir) if err != nil { return err } // declar tabwriter tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) // headline if !quiet { fmt.Fprint(tw, "id\tname\tlocation\ttype\twire-id\tgpio\tenabled\n") } for _, sensor := range cnf.Sensors { if quiet { fmt.Fprintf(tw, "%v\n", sensor.RemoteSensor.SensorID) } else { fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", sensor.RemoteSensor.SensorID, *sensor.RemoteSensor.SensorName, *sensor.RemoteSensor.SensorLocation, *sensor.RemoteSensor.SensorType, *sensor.RemoteSensor.WireID, *sensor.RemoteSensor.GPIONumber, sensor.Enabled) } } tw.Flush() return nil } // Remove a sensor func Remove(sensorName, configDir string) error { cnf, err := config.Read(configDir) if err != nil { return err } var j int for _, sensor := range cnf.Sensors { if *sensor.RemoteSensor.SensorName == sensorName || sensor.RemoteSensor.SensorID == sensorName { cnf.Sensors = append(cnf.Sensors[:j], cnf.Sensors[j+1:]...) if j > 0 { j = j - 1 } continue } j++ } if err := config.Write(cnf, configDir); err != nil { return err } return nil } func exists(wirePath, sensorID string) bool { sensorPath := filepath.Join(wirePath, sensorID) if _, err := os.Stat(sensorPath); os.IsNotExist(err) { return false } return true } // isWireIDRedundant returns a boolean if the sensor id redundant func isWireIDRedundant(wireID string, sensors []*types.Sensor) bool { for _, sensor := range sensors { if *sensor.RemoteSensor.WireID == wireID { return true } } return false } // isSensorNameRedundant returns a boolean if the sensor name redundant func isSensorNameRedundant(sensorName string, sensors []*types.Sensor) bool { for _, sensor := range sensors { if *sensor.RemoteSensor.SensorName == sensorName { return true } } return false }