From 002f3e9e2519792abf25c71272fb692a366576d6 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 11 Jan 2020 13:18:38 +0100 Subject: [PATCH] fix(cmd/sensor): set tick duration for new sensors --- cmd/sensor/add.go | 37 +++++++++++++++++++++---------------- pkg/cli/cli.go | 4 ++-- pkg/config/config.go | 5 +++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/cmd/sensor/add.go b/cmd/sensor/add.go index ba404d5..1a1cb55 100644 --- a/cmd/sensor/add.go +++ b/cmd/sensor/add.go @@ -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", @@ -38,10 +41,11 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`, // create new sensor struct sensor := &types.Sensor{ - Name: args[0], - Model: sensorModel, - Location: location, - Enabled: enabled, + Name: args[0], + 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") } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index f6bd434..d234e4a 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -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() diff --git a/pkg/config/config.go b/pkg/config/config.go index 1be2b0a..db5d380 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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