PKGBUILD/cmd/rgbled/rgbled.go

348 lines
7.1 KiB
Go
Raw Normal View History

package rgbled
import (
"fmt"
"os"
"github.com/Masterminds/semver"
"github.com/volker-raschek/flucky/pkg/cli"
"github.com/volker-raschek/flucky/pkg/config"
"github.com/volker-raschek/flucky/pkg/rgbled"
"github.com/volker-raschek/flucky/pkg/types"
"github.com/spf13/cobra"
)
var (
enabled bool
location string
version *semver.Version
)
// InitCmd initialize all rgb-led subcommands
func InitCmd(cmd *cobra.Command) error {
rgbLedCmd := &cobra.Command{
Use: "rgb-led",
Short: "Manage RGB-LEDs",
}
addRGBLEDCmd := &cobra.Command{
Use: "add",
Short: "Add a RGB-LED",
Aliases: []string{"append"},
Args: cobra.ExactArgs(4),
Example: fmt.Sprintf(`flucky rgb-led add <name> <gpio-for-blue> <gpio-for-green> <gpio-for-red>
flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
RunE: addRGBLED,
}
addRGBLEDCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
addRGBLEDCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
disableRGBLEDCmd := &cobra.Command{
Use: "disable",
Short: "Disable a RGB-LED",
Args: cobra.ExactArgs(1),
Example: `flucky rgb-led disable <name/uuid>
flucky rgb-led disable my-led
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
RunE: disableRGBLED,
}
enableRGBLEDCmd := &cobra.Command{
Use: "enable",
Short: "Enable a RGB-LED",
Args: cobra.ExactArgs(1),
Example: `flucky rgb-led enable <name/uuid>
flucky rgb-led enable my-led
flucky rgb-led enable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
RunE: enableRGBLED,
}
listRGBLEDCmd := &cobra.Command{
Use: "list",
Short: "List RGB-LEDs",
Aliases: []string{"ls"},
RunE: listRGBLED,
}
turnOffRGBLEDCmd := &cobra.Command{
Use: "off",
Short: "Turn a RGB-LED color off",
Example: `flucky rgb-led off <name/uuid> <blue>
flucky rgb-led off my-led`,
RunE: turnOffRGBLED,
}
turnOnRGBLEDCmd := &cobra.Command{
Use: "on",
Short: "Turn a RGB-LED color on",
Example: `flucky rgb-led on <names/uuids> <blue/green/purple/red/turquoise/white/yellow>
flucky rgb-led on my-led blue
flucky rgb-led on my-led my-sweet-led white
flucky rgb-led on 1c5b9424-f6e9-4a37-be5c-77e531e94aab red`,
Args: cobra.MinimumNArgs(1),
RunE: turnOnRGBLED,
}
removeRGBLEDCmd := &cobra.Command{
Use: "remove",
Short: "Remove a RGB-LED",
Example: `flucky rgb-led remove <name/uuid>
flucky rgb-led remove my-led
flucky rgb-led remove 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
Aliases: []string{"rm"},
Args: cobra.ExactArgs(1),
RunE: removeRGBLED,
}
renameRGBLEDCmd := &cobra.Command{
Use: "rename",
Short: "Rename a RGB-LED",
Args: cobra.ExactArgs(2),
Example: `flucky rgb-led disable <name/uuid> <new-name>
flucky rgb-led disable my-led my-sweet-led
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d my-sweet-led`,
RunE: renameRGBLED,
}
rgbLedCmd.AddCommand(addRGBLEDCmd)
rgbLedCmd.AddCommand(disableRGBLEDCmd)
rgbLedCmd.AddCommand(enableRGBLEDCmd)
rgbLedCmd.AddCommand(listRGBLEDCmd)
rgbLedCmd.AddCommand(turnOffRGBLEDCmd)
rgbLedCmd.AddCommand(turnOnRGBLEDCmd)
rgbLedCmd.AddCommand(removeRGBLEDCmd)
rgbLedCmd.AddCommand(renameRGBLEDCmd)
cmd.AddCommand(rgbLedCmd)
return nil
}
func addRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
gpioBlue, err := types.StringToGPIO(args[1])
if err != nil {
return err
}
gpioGreen, err := types.StringToGPIO(args[2])
if err != nil {
return err
}
gpioRed, err := types.StringToGPIO(args[3])
if err != nil {
return err
}
rgbLED := &types.RGBLED{
RGBLEDName: args[0],
RGBLEDLocation: location,
RGBLEDEnabled: enabled,
ActionMapping: types.DefaultActionMapping,
BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{
types.BaseColorBlue: &gpioBlue,
types.BaseColorGreen: &gpioGreen,
types.BaseColorRed: &gpioRed,
},
}
err = cnf.AddRGBLED(rgbLED)
if err != nil {
return err
}
err = config.Write(cnf, configFile)
if err != nil {
return err
}
return nil
}
func disableRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
err = cnf.DisableRGBLED(args[0])
if err != nil {
return err
}
err = config.Write(cnf, configFile)
if err != nil {
return err
}
return nil
}
func enableRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
err = cnf.EnableRGBLED(args[0])
if err != nil {
return err
}
err = config.Write(cnf, configFile)
if err != nil {
return err
}
return nil
}
func listRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
// print sensors on stdout
cli.PrintRGBLEDs(cnf, os.Stdout)
return nil
}
func turnOffRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
rgbLEDs := make([]rgbled.RGBLED, 0)
if len(args) != 0 {
rgbLEDs = cnf.GetRGBLEDsByName(args)
} else {
rgbLEDs = cnf.GetRGBLEDs(config.ENABLED)
}
err = rgbled.Off(rgbLEDs)
if err != nil {
return err
}
return nil
}
func turnOnRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
rgbLEDs := make([]rgbled.RGBLED, 0)
if len(args) > 1 {
rgbLEDs = cnf.GetRGBLEDsByName(args[0 : len(args)-1])
} else {
rgbLEDs = cnf.GetRGBLEDs(config.ENABLED)
}
color, err := types.StringToLEDColor(args[len(args)-1])
if err != nil {
return err
}
err = rgbled.CustomColor(rgbLEDs, color)
if err != nil {
return err
}
return nil
}
func removeRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
err = cnf.RemoveRGBLED(args[0])
if err != nil {
return err
}
err = config.Write(cnf, configFile)
if err != nil {
return err
}
return nil
}
func renameRGBLED(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
}
cnf, err := config.Read(configFile)
if err != nil {
return err
}
err = cnf.RenameRGBLED(args[0], args[1])
if err != nil {
return err
}
err = config.Write(cnf, configFile)
if err != nil {
return err
}
return nil
}