fix(cmd/sensor): set tick duration for new sensors
This commit is contained in:
		| @@ -8,12 +8,15 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var enabled bool | ||||
| var gpioIn string | ||||
| var i2cAddress uint8 | ||||
| var i2cBus int | ||||
| var location string | ||||
| var wireID string | ||||
| var ( | ||||
| 	enabled      bool | ||||
| 	gpioIn       string | ||||
| 	i2cAddress   uint8 | ||||
| 	i2cBus       int | ||||
| 	location     string | ||||
| 	tickDuration string | ||||
| 	wireID       string | ||||
| ) | ||||
|  | ||||
| var addSensorCmd = &cobra.Command{ | ||||
| 	Use:     "add", | ||||
| @@ -42,6 +45,7 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`, | ||||
| 			Model:        sensorModel, | ||||
| 			Location:     location, | ||||
| 			Enabled:      enabled, | ||||
| 			TickDuration: tickDuration, | ||||
| 		} | ||||
|  | ||||
| 		// determine gpio port if set | ||||
| @@ -90,10 +94,11 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`, | ||||
| func init() { | ||||
| 	sensorCmd.AddCommand(addSensorCmd) | ||||
|  | ||||
| 	addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable Sensor") | ||||
| 	addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "GPIO") | ||||
| 	addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "I2C-Address") | ||||
| 	addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "I2C-Bus") | ||||
| 	addSensorCmd.Flags().StringVar(&location, "location", "", "Sensor location") | ||||
| 	addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Wire-ID") | ||||
| 	addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable new sensor") | ||||
| 	addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "Defines the GPIO port") | ||||
| 	addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "Defines the I2C address on the I2C bus") | ||||
| 	addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "Defines the I2C bus") | ||||
| 	addSensorCmd.Flags().StringVar(&location, "location", "", "Location of the sensor") | ||||
| 	addSensorCmd.Flags().StringVar(&tickDuration, "tick-duration", "1m", "Controls how often values should be read from the sensor when running flucky in daemon mode") | ||||
| 	addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Defines the Wire-ID") | ||||
| } | ||||
|   | ||||
| @@ -68,7 +68,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error { | ||||
| 	// declar tabwriter | ||||
| 	tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) | ||||
|  | ||||
| 	fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\tenabled\n") | ||||
| 	fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\ttick-duration\tenabled\n") | ||||
|  | ||||
| 	for _, sensor := range cnf.Sensors { | ||||
| 		fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.Name, sensor.Location, sensor.Model) | ||||
| @@ -97,7 +97,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error { | ||||
| 			fmt.Fprintf(tw, "\t") | ||||
| 		} | ||||
|  | ||||
| 		fmt.Fprintf(tw, "%v\n", sensor.Enabled) | ||||
| 		fmt.Fprintf(tw, "%v\t%v\n", sensor.TickDuration, sensor.Enabled) | ||||
| 	} | ||||
|  | ||||
| 	tw.Flush() | ||||
|   | ||||
| @@ -81,6 +81,11 @@ func (c *Configuration) AddSensor(sensor *types.Sensor) error { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// check if sensor has a valid tick time | ||||
| 	if _, err := time.ParseDuration(sensor.TickDuration); err != nil { | ||||
| 		return fmt.Errorf("Can not parse tick duration: %v", sensor.TickDuration) | ||||
| 	} | ||||
|  | ||||
| 	// check if sensor has a valid device id | ||||
| 	if sensor.DeviceID != c.Device.ID { | ||||
| 		sensor.DeviceID = c.Device.ID | ||||
|   | ||||
		Reference in New Issue
	
	Block a user