From 49d66cfcbb54cae24ff743daa2657330dd77db8a Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 16 Jun 2019 13:00:50 +0200 Subject: [PATCH] feat(cmd/rgb-led): add subcommands to operate with rgb-leds --- cmd/cmd.go | 17 ++-- cmd/daemon/daemon.go | 8 +- cmd/humidity/humidity.go | 6 +- cmd/rgbled/add.go | 78 ++++++++++++++ cmd/rgbled/disable.go | 42 ++++++++ cmd/rgbled/enable.go | 41 ++++++++ cmd/rgbled/list.go | 30 ++++++ cmd/rgbled/remove.go | 41 ++++++++ cmd/rgbled/rename.go | 40 ++++++++ cmd/rgbled/rgbled.go | 20 ++++ cmd/sensor/add.go | 6 +- cmd/sensor/disable.go | 6 +- cmd/sensor/enable.go | 6 +- cmd/sensor/list.go | 6 +- cmd/sensor/remove.go | 6 +- cmd/sensor/rename.go | 6 +- cmd/sensor/sensor.go | 6 +- cmd/temperature/list.go | 2 +- cmd/temperature/read.go | 2 +- cmd/temperature/temperature.go | 6 +- go.mod | 1 + go.sum | 2 + pkg/cli/cli.go | 15 +++ pkg/config/flucky.go | 180 +++++++++++++++++++++++++++++++++ pkg/led/led.go | 71 +++++++++++++ pkg/types/gpio.go | 57 +++++++++++ pkg/types/led.go | 52 ++++++++++ 27 files changed, 713 insertions(+), 40 deletions(-) create mode 100644 cmd/rgbled/add.go create mode 100644 cmd/rgbled/disable.go create mode 100644 cmd/rgbled/enable.go create mode 100644 cmd/rgbled/list.go create mode 100644 cmd/rgbled/remove.go create mode 100644 cmd/rgbled/rename.go create mode 100644 cmd/rgbled/rgbled.go create mode 100644 pkg/led/led.go create mode 100644 pkg/types/led.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 89ae1af..dbf4079 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,6 +6,7 @@ import ( "time" "github.com/go-flucky/flucky/cmd/daemon" + "github.com/go-flucky/flucky/cmd/rgbled" "github.com/go-flucky/flucky/cmd/sensor" "github.com/go-flucky/flucky/cmd/temperature" "github.com/go-flucky/flucky/pkg/types" @@ -15,7 +16,7 @@ import ( "github.com/spf13/cobra" ) -var configPath string +var configFile string var rootCmd = &cobra.Command{ Use: "flucky", @@ -23,7 +24,7 @@ var rootCmd = &cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // check if config file exists - if _, err := os.Stat(configPath); os.IsNotExist(err) { + if _, err := os.Stat(configFile); os.IsNotExist(err) { hostname, err := os.Hostname() if err != nil { return fmt.Errorf("Can not locate the hostname: %v", err) @@ -39,7 +40,7 @@ var rootCmd = &cobra.Command{ }, } - err = config.Write(&cnf, configPath) + err = config.Write(&cnf, configFile) if err != nil { return err } @@ -53,9 +54,11 @@ var rootCmd = &cobra.Command{ func Execute(version string) { rootCmd.Version = version - rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file") - daemon.InitCmd(rootCmd, configPath) - sensor.InitCmd(rootCmd, configPath) - temperature.InitCmd(rootCmd, configPath) + rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file") + + daemon.InitCmd(rootCmd, configFile) + rgbled.InitCmd(rootCmd, configFile) + sensor.InitCmd(rootCmd, configFile) + temperature.InitCmd(rootCmd, configFile) rootCmd.Execute() } diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 9347ed0..57f516f 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -9,14 +9,14 @@ import ( ) var compression bool -var configPath string +var configFile string var daemonCmd = &cobra.Command{ Use: "daemon", Short: "Read continuously data from all enabled sensors", Run: func(cmd *cobra.Command, args []string) { // read configuration - cnf, err := config.Read(configPath) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } @@ -29,8 +29,8 @@ var daemonCmd = &cobra.Command{ }, } -func InitCmd(cmd *cobra.Command, cnfPath string) { - configPath = cnfPath +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile cmd.AddCommand(daemonCmd) daemonCmd.Flags().BoolVarP(&compression, "compression", "c", true, "Compress measured values") diff --git a/cmd/humidity/humidity.go b/cmd/humidity/humidity.go index 6bb5076..308ee64 100644 --- a/cmd/humidity/humidity.go +++ b/cmd/humidity/humidity.go @@ -4,7 +4,7 @@ import ( "github.com/spf13/cobra" ) -var configPath string +var configFile string var humidityCmd = &cobra.Command{ Use: "humidity", @@ -12,8 +12,8 @@ var humidityCmd = &cobra.Command{ } // Execute a -func InitCmd(cmd *cobra.Command, cnfPath string) { - configPath = cnfPath +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile cmd.AddCommand(humidityCmd) diff --git a/cmd/rgbled/add.go b/cmd/rgbled/add.go new file mode 100644 index 0000000..1f3ae09 --- /dev/null +++ b/cmd/rgbled/add.go @@ -0,0 +1,78 @@ +package rgbled + +import ( + "fmt" + "log" + + "github.com/go-flucky/flucky/pkg/config" + "github.com/go-flucky/flucky/pkg/types" + "github.com/spf13/cobra" +) + +var enabled bool +var location, wireID, wirePath string + +var addRgbLedCmd = &cobra.Command{ + Use: "add", + Short: "Add a RGB-LED", + Aliases: []string{"append"}, + Args: cobra.ExactArgs(4), + Example: fmt.Sprintf(`flucky rgb-led add +flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`), + Run: func(cmd *cobra.Command, args []string) { + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // determine gpio port + gpioRed, err := types.StringToGPIO(args[1]) + if err != nil { + log.Fatalln(err) + } + + gpioGreen, err := types.StringToGPIO(args[2]) + if err != nil { + log.Fatalln(err) + } + + gpioBlue, err := types.StringToGPIO(args[3]) + if err != nil { + log.Fatalln(err) + } + + // create new sensor struct + rgbLED := &types.RGBLED{ + RGBLEDName: args[0], + RGBLEDLocation: location, + RGBLEDEnabled: enabled, + RGBLEDColorToGPIO: map[types.RGBColor]*types.GPIO{ + types.RGBLEDBlue: &gpioBlue, + types.RGBLEDGreen: &gpioGreen, + types.RGBLEDRed: &gpioRed, + }, + } + + // // add sensor entry to list + err = cnf.AddRGBLED(rgbLED) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(cnf, configFile) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + rgbLedCmd.AddCommand(addRgbLedCmd) + + addRgbLedCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor") + addRgbLedCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location") + addRgbLedCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID") + addRgbLedCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path") +} diff --git a/cmd/rgbled/disable.go b/cmd/rgbled/disable.go new file mode 100644 index 0000000..75ad8f7 --- /dev/null +++ b/cmd/rgbled/disable.go @@ -0,0 +1,42 @@ +package rgbled + +import ( + "log" + + "github.com/go-flucky/flucky/pkg/config" + "github.com/spf13/cobra" +) + +var disableRgbLedCmd = &cobra.Command{ + Use: "disable", + Short: "Disable a RGB-LED", + Args: cobra.ExactArgs(1), + Example: `flucky rgb-led disable +flucky rgb-led disable my-led +flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`, + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // disable sensor entry to list + err = cnf.DisableRGBLED(args[0]) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(cnf, configFile) + if err != nil { + log.Fatalln(err) + } + + }, +} + +func init() { + rgbLedCmd.AddCommand(disableRgbLedCmd) +} diff --git a/cmd/rgbled/enable.go b/cmd/rgbled/enable.go new file mode 100644 index 0000000..8c60d8c --- /dev/null +++ b/cmd/rgbled/enable.go @@ -0,0 +1,41 @@ +package rgbled + +import ( + "log" + + "github.com/go-flucky/flucky/pkg/config" + "github.com/spf13/cobra" +) + +var enableRgbLedCmd = &cobra.Command{ + Use: "enable", + Short: "Enable a RGB-LED", + Example: `flucky rgb-led enable +flucky rgb-led enable my-led +flucky rgb-led enable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // disable sensor entry to list + err = cnf.EnableRGBLED(args[0]) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(cnf, configFile) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + rgbLedCmd.AddCommand(enableRgbLedCmd) +} diff --git a/cmd/rgbled/list.go b/cmd/rgbled/list.go new file mode 100644 index 0000000..c7fa1f9 --- /dev/null +++ b/cmd/rgbled/list.go @@ -0,0 +1,30 @@ +package rgbled + +import ( + "log" + "os" + + "github.com/go-flucky/flucky/pkg/cli" + "github.com/go-flucky/flucky/pkg/config" + "github.com/spf13/cobra" +) + +var listRgbLedCmd = &cobra.Command{ + Use: "list", + Short: "List RGB-LEDs", + Aliases: []string{"ls"}, + Run: func(cmd *cobra.Command, args []string) { + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // print sensors on stdout + cli.PrintRGBLEDs(cnf, os.Stdout) + }, +} + +func init() { + rgbLedCmd.AddCommand(listRgbLedCmd) +} diff --git a/cmd/rgbled/remove.go b/cmd/rgbled/remove.go new file mode 100644 index 0000000..6afd513 --- /dev/null +++ b/cmd/rgbled/remove.go @@ -0,0 +1,41 @@ +package rgbled + +import ( + "log" + + "github.com/go-flucky/flucky/pkg/config" + "github.com/spf13/cobra" +) + +var removeRgbLedCmd = &cobra.Command{ + Use: "remove", + Short: "Remove a RGB-LED", + Example: `flucky rgb-led remove +flucky rgb-led remove my-led +flucky rgb-led remove 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`, + Aliases: []string{"rm"}, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // // add remote entry to list + err = cnf.RemoveRGBLED(args[0]) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(cnf, configFile) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + rgbLedCmd.AddCommand(removeRgbLedCmd) +} diff --git a/cmd/rgbled/rename.go b/cmd/rgbled/rename.go new file mode 100644 index 0000000..3dfa29d --- /dev/null +++ b/cmd/rgbled/rename.go @@ -0,0 +1,40 @@ +package rgbled + +import ( + "log" + + "github.com/go-flucky/flucky/pkg/config" + "github.com/spf13/cobra" +) + +var renameRgbLedCmd = &cobra.Command{ + Use: "rename", + Short: "Rename a RGB-LED", + Args: cobra.ExactArgs(2), + Example: `flucky rgb-led disable +flucky rgb-led disable my-led my-sweet-led +flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d my-sweet-led`, + Run: func(cmd *cobra.Command, args []string) { + // read configuration + cnf, err := config.Read(configFile) + if err != nil { + log.Fatalln(err) + } + + // rename sensor + err = cnf.RenameRGBLED(args[0], args[1]) + if err != nil { + log.Println(err) + } + + // save new configuration + err = config.Write(cnf, configFile) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + rgbLedCmd.AddCommand(renameRgbLedCmd) +} diff --git a/cmd/rgbled/rgbled.go b/cmd/rgbled/rgbled.go new file mode 100644 index 0000000..b5bb5be --- /dev/null +++ b/cmd/rgbled/rgbled.go @@ -0,0 +1,20 @@ +package rgbled + +import ( + "github.com/spf13/cobra" +) + +var configFile string + +var rgbLedCmd = &cobra.Command{ + Use: "rgb-led", + Short: "Manage RGB-LEDs", +} + +// InitCmd da +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile + + cmd.AddCommand(rgbLedCmd) + +} diff --git a/cmd/sensor/add.go b/cmd/sensor/add.go index 5078307..a0bfac7 100644 --- a/cmd/sensor/add.go +++ b/cmd/sensor/add.go @@ -20,7 +20,7 @@ var addSensorCmd = &cobra.Command{ Example: fmt.Sprintf("flucky sensor add indoor DHT11 GPIO14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20 GPIO14"), Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } @@ -48,13 +48,13 @@ var addSensorCmd = &cobra.Command{ } // // add sensor entry to list - err = fc.AddSensor(sensor) + err = cnf.AddSensor(sensor) if err != nil { log.Fatalln(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/disable.go b/cmd/sensor/disable.go index 6d58870..7076ee2 100644 --- a/cmd/sensor/disable.go +++ b/cmd/sensor/disable.go @@ -15,19 +15,19 @@ var disableSensorCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } // disable sensor entry to list - err = fc.DisableSensor(args[0]) + err = cnf.DisableSensor(args[0]) if err != nil { log.Fatalln(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/enable.go b/cmd/sensor/enable.go index 61c8f2f..27ee865 100644 --- a/cmd/sensor/enable.go +++ b/cmd/sensor/enable.go @@ -15,19 +15,19 @@ var enableSensorCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } // disable sensor entry to list - err = fc.EnableSensor(args[0]) + err = cnf.EnableSensor(args[0]) if err != nil { log.Fatalln(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/list.go b/cmd/sensor/list.go index 3dce877..5bab7f8 100644 --- a/cmd/sensor/list.go +++ b/cmd/sensor/list.go @@ -15,19 +15,19 @@ var listSensorCmd = &cobra.Command{ Aliases: []string{"ls"}, Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } // print sensors on stdout - err = cli.PrintSensors(fc, os.Stdout) + err = cli.PrintSensors(cnf, os.Stdout) if err != nil { log.Fatalln(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/remove.go b/cmd/sensor/remove.go index f97aea5..f249563 100644 --- a/cmd/sensor/remove.go +++ b/cmd/sensor/remove.go @@ -15,19 +15,19 @@ var rmSensorCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } // // add remote entry to list - err = fc.RemoveSensor(args[0]) + err = cnf.RemoveSensor(args[0]) if err != nil { log.Fatalln(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/rename.go b/cmd/sensor/rename.go index c567911..1dc0a30 100644 --- a/cmd/sensor/rename.go +++ b/cmd/sensor/rename.go @@ -15,19 +15,19 @@ var renameSensorCmd = &cobra.Command{ Example: fmt.Sprintf("flucky sensor rename indoor outdoor\nflucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor"), Run: func(cmd *cobra.Command, args []string) { // read configuration - fc, err := config.Read(cfg) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } // rename sensor - err = fc.RenameSensor(args[0], args[1]) + err = cnf.RenameSensor(args[0], args[1]) if err != nil { log.Println(err) } // save new configuration - err = config.Write(fc, cfg) + err = config.Write(cnf, configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/sensor.go b/cmd/sensor/sensor.go index c6912b7..3c22105 100644 --- a/cmd/sensor/sensor.go +++ b/cmd/sensor/sensor.go @@ -4,7 +4,7 @@ import ( "github.com/spf13/cobra" ) -var cfg string +var configFile string var sensorCmd = &cobra.Command{ Use: "sensor", @@ -12,8 +12,8 @@ var sensorCmd = &cobra.Command{ } // InitCmd da -func InitCmd(cmd *cobra.Command, config string) { - cfg = config +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile cmd.AddCommand(sensorCmd) diff --git a/cmd/temperature/list.go b/cmd/temperature/list.go index aa6b4a8..d447fa7 100644 --- a/cmd/temperature/list.go +++ b/cmd/temperature/list.go @@ -18,7 +18,7 @@ var listTemperatureCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // read configuration - cnf, err := config.Read(configPath) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 4e145a4..5dbe24d 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -22,7 +22,7 @@ var readTemperatureCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // read configuration - cnf, err := config.Read(configPath) + cnf, err := config.Read(configFile) if err != nil { log.Fatalln(err) } diff --git a/cmd/temperature/temperature.go b/cmd/temperature/temperature.go index deaf3cd..6be121a 100644 --- a/cmd/temperature/temperature.go +++ b/cmd/temperature/temperature.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -var configPath string +var configFile string var temperatureCmd = &cobra.Command{ Use: "temperature", @@ -15,8 +15,8 @@ var temperatureCmd = &cobra.Command{ } // Execute a -func InitCmd(cmd *cobra.Command, cnfPath string) { - configPath = cnfPath +func InitCmd(cmd *cobra.Command, cnfFile string) { + configFile = cnfFile cmd.AddCommand(temperatureCmd) } diff --git a/go.mod b/go.mod index 8bd4be1..6c1e4a0 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/satori/go.uuid v1.2.0 github.com/spf13/cobra v0.0.3 github.com/spf13/pflag v1.0.3 // indirect + github.com/stianeikeland/go-rpio v4.2.0+incompatible github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index 72085a7..dbf4b3f 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo= +github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8= github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624 h1:bePzgtpuLSl+F9aacwuaquuoOyKfMKuJORq2CvPPJK4= github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624/go.mod h1:MqFju5qeLDFh+S9PqxYT7TEla8xeW7bgGr/69q3oki0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 77b238d..e1ead6e 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -68,6 +68,21 @@ func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w tw.Flush() } +func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) { + + // declare tabwriter + tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) + + // headline + fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled") + + for _, rgbled := range cnf.RGBLEDs { + fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", rgbled.RGBLEDName, rgbled.RGBLEDLocation, *rgbled.RGBLEDColorToGPIO[types.RGBLEDBlue], *rgbled.RGBLEDColorToGPIO[types.RGBLEDGreen], *rgbled.RGBLEDColorToGPIO[types.RGBLEDRed], rgbled.RGBLEDEnabled) + } + + tw.Flush() +} + // PrintSensors displays a list with all configured sensors func PrintSensors(cnf *config.Configuration, w io.Writer) error { diff --git a/pkg/config/flucky.go b/pkg/config/flucky.go index 1bd9d8d..85aa4a5 100644 --- a/pkg/config/flucky.go +++ b/pkg/config/flucky.go @@ -25,9 +25,79 @@ var temperatureSensorModels = map[types.SensorModel]types.SensorModel{ // Configuration of flucky type Configuration struct { Device *types.Device `json:"device"` + LEDs []*types.LED `json:"leds"` + RGBLEDs []*types.RGBLED `json:"rgb_leds"` Sensors []*types.Sensor `json:"sensors"` } +// AddLED add a new LED +func (c *Configuration) AddLED(led *types.LED) error { + + // check if LEDID is a valid UUID string + if !validUUID.MatchString(led.LEDID) { + led.LEDID = uuid.NewV4().String() + } + + // check if sensor name and sensor uuid already exists + for _, l := range c.LEDs { + if l.LEDName == led.LEDName { + return fmt.Errorf("LED %v already exists", led.LEDName) + } + + if l.LEDID == led.LEDID { + return fmt.Errorf("LED %v with UUID %v already exists", led.LEDName, led.LEDID) + } + + } + + // check if sensor has a valid device id + if led.DeviceID != c.Device.DeviceID { + led.DeviceID = c.Device.DeviceID + } + + // overwrite creation date + led.CreationDate = time.Now() + + // check + c.LEDs = append(c.LEDs, led) + + return nil +} + +// AddRGBLED add a new RGBLED +func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error { + + // check if RGBLEDID is a valid UUID string + if !validUUID.MatchString(rgbLED.RGBLEDID) { + rgbLED.RGBLEDID = uuid.NewV4().String() + } + + // check if sensor name and sensor uuid already exists + for _, l := range c.RGBLEDs { + if l.RGBLEDName == rgbLED.RGBLEDName { + return fmt.Errorf("RGBLED %v already exists", rgbLED.RGBLEDName) + } + + if l.RGBLEDID == rgbLED.RGBLEDID { + return fmt.Errorf("RGBLED %v with UUID %v already exists", rgbLED.RGBLEDName, rgbLED.RGBLEDID) + } + + } + + // check if sensor has a valid device id + if rgbLED.DeviceID != c.Device.DeviceID { + rgbLED.DeviceID = c.Device.DeviceID + } + + // overwrite creation date + rgbLED.CreationDate = time.Now() + + // check + c.RGBLEDs = append(c.RGBLEDs, rgbLED) + + return nil +} + // AddSensor add a new sensor func (c *Configuration) AddSensor(sensor *types.Sensor) error { @@ -68,6 +138,36 @@ func (c *Configuration) AddSensor(sensor *types.Sensor) error { return nil } +// DisableRGBLED enables a rgb led by its name or its unique UUID +func (c *Configuration) DisableRGBLED(name string) error { + found := false + + for _, rgbled := range c.RGBLEDs { + + // disable sensor matched after name + if !validUUID.MatchString(name) && + rgbled.RGBLEDName == name { + rgbled.RGBLEDEnabled = false + found = true + break + } + + // disable sensor matched by uuid + if validUUID.MatchString(name) && + rgbled.RGBLEDID == name { + rgbled.RGBLEDEnabled = false + found = true + break + } + } + + if !found { + return fmt.Errorf("Can not found RGB-LED %v", name) + } + + return nil +} + // DisableSensor disables a sensor by its name or its unique UUID func (c *Configuration) DisableSensor(name string) error { found := false @@ -98,6 +198,36 @@ func (c *Configuration) DisableSensor(name string) error { return nil } +// EnableRGBLED enables a rgb led by its name or its unique UUID +func (c *Configuration) EnableRGBLED(name string) error { + found := false + + for _, rgbled := range c.RGBLEDs { + + // disable sensor matched after name + if !validUUID.MatchString(name) && + rgbled.RGBLEDName == name { + rgbled.RGBLEDEnabled = true + found = true + break + } + + // disable sensor matched by uuid + if validUUID.MatchString(name) && + rgbled.RGBLEDID == name { + rgbled.RGBLEDEnabled = true + found = true + break + } + } + + if !found { + return fmt.Errorf("Can not found RGB-LED %v", name) + } + + return nil +} + // EnableSensor enables a sensor by its name or its unique UUID func (c *Configuration) EnableSensor(name string) error { found := false @@ -228,6 +358,44 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Tem return c.convertTemperatureSensors(temperatureSensors) } +// RemoveLED deletes a LED by its name or its unique UUID +func (c *Configuration) RemoveLED(name string) error { + for i, led := range c.LEDs { + // remove machted name + if !validUUID.MatchString(name) && + led.LEDName == name { + c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) + return nil + } + // remove machted uuid + if validUUID.MatchString(name) && + led.LEDID == name { + c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) + return nil + } + } + return fmt.Errorf("Can not find LED %v", name) +} + +// RemoveRGBLED deletes a LED by its name or its unique UUID +func (c *Configuration) RemoveRGBLED(name string) error { + for i, rgbLED := range c.RGBLEDs { + // remove machted name + if !validUUID.MatchString(name) && + rgbLED.RGBLEDName == name { + c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...) + return nil + } + // remove machted uuid + if validUUID.MatchString(name) && + rgbLED.RGBLEDID == name { + c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) + return nil + } + } + return fmt.Errorf("Can not find RGBLED %v", name) +} + // RemoveSensor deletes a sensor by its name or its unique UUID func (c *Configuration) RemoveSensor(name string) error { for i, sensor := range c.Sensors { @@ -247,6 +415,18 @@ func (c *Configuration) RemoveSensor(name string) error { return fmt.Errorf("Can not find sensor %v", name) } +// RenameRGBLED renames a sensor identified by the name or the UUID +func (c *Configuration) RenameRGBLED(oldName, newName string) error { + for _, rgbled := range c.RGBLEDs { + if rgbled.RGBLEDName == oldName || + rgbled.RGBLEDID == oldName { + rgbled.RGBLEDName = newName + return nil + } + } + return fmt.Errorf("Could not find rgb-led %v to replace into with %v", oldName, newName) +} + // RenameSensor renames a sensor identified by the name or the UUID func (c *Configuration) RenameSensor(oldName, newName string) error { for _, sensor := range c.Sensors { diff --git a/pkg/led/led.go b/pkg/led/led.go new file mode 100644 index 0000000..c489181 --- /dev/null +++ b/pkg/led/led.go @@ -0,0 +1,71 @@ +package led + +import ( + "fmt" + + "github.com/go-flucky/flucky/pkg/types" + "github.com/stianeikeland/go-rpio" +) + +type LED interface { + On() error + Off() error + Toggel() error +} + +type RGBLED struct { + *types.LED +} + +func (l *RGBLED) On() error { + if err := rpio.Open(); err != nil { + return fmt.Errorf("Can not open rpio: %v", err) + } + defer rpio.Close() + + gpio, err := types.GPIOToInt(*l.GPIONumber) + if err != nil { + return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err) + } + + pin := rpio.Pin(gpio) + pin.High() + + return nil +} + +func (l *RGBLED) Off() error { + + if err := rpio.Open(); err != nil { + return fmt.Errorf("Can not open rpio: %v", err) + } + defer rpio.Close() + + gpio, err := types.GPIOToInt(*l.GPIONumber) + if err != nil { + return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err) + } + + pin := rpio.Pin(gpio) + pin.Low() + + return nil +} + +func (l *RGBLED) Toggel() error { + + if err := rpio.Open(); err != nil { + return fmt.Errorf("Can not open rpio: %v", err) + } + defer rpio.Close() + + gpio, err := types.GPIOToInt(*l.GPIONumber) + if err != nil { + return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err) + } + + pin := rpio.Pin(gpio) + pin.Toggle() + + return nil +} diff --git a/pkg/types/gpio.go b/pkg/types/gpio.go index 9bf41ac..7f0e875 100644 --- a/pkg/types/gpio.go +++ b/pkg/types/gpio.go @@ -91,6 +91,63 @@ func GPIOToString(gpio GPIO) (string, error) { } } +func GPIOToInt(gpio GPIO) (int, error) { + switch gpio { + case GPIO02: + return 2, nil + case GPIO03: + return 3, nil + case GPIO04: + return 4, nil + case GPIO05: + return 5, nil + case GPIO06: + return 6, nil + case GPIO07: + return 7, nil + case GPIO08: + return 8, nil + case GPIO10: + return 10, nil + case GPIO11: + return 11, nil + case GPIO12: + return 12, nil + case GPIO13: + return 13, nil + case GPIO14: + return 14, nil + case GPIO15: + return 15, nil + case GPIO16: + return 16, nil + case GPIO17: + return 17, nil + case GPIO18: + return 18, nil + case GPIO19: + return 19, nil + case GPIO20: + return 20, nil + case GPIO21: + return 21, nil + case GPIO22: + return 22, nil + case GPIO23: + return 23, nil + case GPIO24: + return 24, nil + case GPIO25: + return 25, nil + case GPIO26: + return 26, nil + case GPIO27: + return 27, nil + default: + return 0, fmt.Errorf("Can not determine gpio %v", gpio) + } +} + func StringToGPIO(gpio string) (GPIO, error) { switch gpio { case "GPIO02": diff --git a/pkg/types/led.go b/pkg/types/led.go new file mode 100644 index 0000000..daf5708 --- /dev/null +++ b/pkg/types/led.go @@ -0,0 +1,52 @@ +package types + +import "time" + +type LED struct { + LEDID string `json:"led_id"` + LEDName string `json:"led_name"` + LEDLocation string `json:"led_location"` + GPIONumber *GPIO `json:"gpio_number"` + LEDEnabled bool `json:"led_enabled"` + LEDColor *LEDColor `json:"led_color"` + DeviceID string `json:"device_id"` + CreationDate time.Time `json:"creation_date"` +} + +type RGBLED struct { + RGBLEDID string `json:"rgbled_id"` + RGBLEDName string `json:"rgbled_name"` + RGBLEDLocation string `json:"rgbled_location"` + RGBLEDColorToGPIO map[RGBColor]*GPIO `json:"color_to_gpio"` + RGBLEDEnabled bool `json:"rgbled_enabled"` + DeviceID string `json:"device_id"` + CreationDate time.Time `json:"creation_date"` +} + +type RGBColor string + +const ( + RGBLEDBlue RGBColor = "blue" + RGBLEDRed = "red" + RGBLEDGreen = "green" +) + +type LEDColor string + +const ( + LEDBlue LEDColor = "blue" + LEDRed = "red" + LEDGreen = "green" + LEDPurple = "purple" + LEDTurquoiseGravel = "turquoise gravel" + LEDYellow = "yellow" + LEDWhite = "white" +) + +type LEDOption string + +const ( + LEDError LEDOption = "error" + LEDWarn = "warn" + LEDOk = "ok" +)