fix(cmd/sensor): set tick duration for new sensors

This commit is contained in:
Markus Pesch 2020-01-11 13:18:38 +01:00
parent 40857249c4
commit 002f3e9e25
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 28 additions and 18 deletions

View File

@ -8,12 +8,15 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var enabled bool var (
var gpioIn string enabled bool
var i2cAddress uint8 gpioIn string
var i2cBus int i2cAddress uint8
var location string i2cBus int
var wireID string location string
tickDuration string
wireID string
)
var addSensorCmd = &cobra.Command{ var addSensorCmd = &cobra.Command{
Use: "add", Use: "add",
@ -42,6 +45,7 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
Model: sensorModel, Model: sensorModel,
Location: location, Location: location,
Enabled: enabled, Enabled: enabled,
TickDuration: tickDuration,
} }
// determine gpio port if set // determine gpio port if set
@ -90,10 +94,11 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
func init() { func init() {
sensorCmd.AddCommand(addSensorCmd) sensorCmd.AddCommand(addSensorCmd)
addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable Sensor") addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable new sensor")
addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "GPIO") addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "Defines the GPIO port")
addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "I2C-Address") addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "Defines the I2C address on the I2C bus")
addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "I2C-Bus") addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "Defines the I2C bus")
addSensorCmd.Flags().StringVar(&location, "location", "", "Sensor location") addSensorCmd.Flags().StringVar(&location, "location", "", "Location of the sensor")
addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Wire-ID") 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")
} }

View File

@ -68,7 +68,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
// declar tabwriter // declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) 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 { for _, sensor := range cnf.Sensors {
fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.Name, sensor.Location, sensor.Model) 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, "\t")
} }
fmt.Fprintf(tw, "%v\n", sensor.Enabled) fmt.Fprintf(tw, "%v\t%v\n", sensor.TickDuration, sensor.Enabled)
} }
tw.Flush() tw.Flush()

View File

@ -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 // check if sensor has a valid device id
if sensor.DeviceID != c.Device.ID { if sensor.DeviceID != c.Device.ID {
sensor.DeviceID = c.Device.ID sensor.DeviceID = c.Device.ID