2020-03-28 23:05:35 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-01-25 02:50:10 +00:00
|
|
|
"os"
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-01-18 03:04:18 +00:00
|
|
|
lifx "git.kill0.net/chill9/lume"
|
2020-03-28 23:05:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-02-01 01:40:27 +00:00
|
|
|
RegisterCommand("set-color", Command{
|
|
|
|
Func: SetColorCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
selector := fs.String("selector", "all", "the selector")
|
|
|
|
fs.StringVar(selector, "s", "all", "the selector")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
power := fs.String("power", defaultPower, "power state")
|
|
|
|
fs.StringVar(power, "p", defaultPower, "power state")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
hue := fs.String("hue", defaultHue, "hue level")
|
|
|
|
fs.StringVar(hue, "H", defaultHue, "hue level")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
saturation := fs.String("saturation", defaultSaturation, "saturation level")
|
|
|
|
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
rgb := fs.String("rgb", defaultRGB, "RGB value")
|
|
|
|
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
name := fs.String("name", defaultName, "named color")
|
|
|
|
fs.StringVar(name, "n", defaultName, "named color")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
|
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
2020-04-04 15:27:09 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
|
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
|
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
return fs
|
|
|
|
}(),
|
2020-08-07 23:44:36 +00:00
|
|
|
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
|
2020-08-08 03:09:40 +00:00
|
|
|
Short: "Set the color",
|
2020-03-28 23:05:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-29 05:08:17 +00:00
|
|
|
func SetColorCmd(args CmdArgs) (int, error) {
|
2020-03-28 23:05:35 +00:00
|
|
|
c := args.Client
|
|
|
|
state := lifx.State{}
|
|
|
|
selector := args.Flags.String("selector")
|
|
|
|
|
|
|
|
power := args.Flags.String("power")
|
|
|
|
if power != "" {
|
|
|
|
state.Power = power
|
|
|
|
}
|
|
|
|
|
|
|
|
hueFlag := args.Flags.String("hue")
|
|
|
|
saturationFlag := args.Flags.String("saturation")
|
|
|
|
rgbFlag := args.Flags.String("rgb")
|
2020-04-04 15:27:09 +00:00
|
|
|
name := args.Flags.String("name")
|
2020-03-28 23:05:35 +00:00
|
|
|
|
2021-01-25 02:50:10 +00:00
|
|
|
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
|
|
|
|
printCmdHelp(os.Args[1])
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, nil
|
2021-01-25 02:50:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 23:05:35 +00:00
|
|
|
if hueFlag != "" || saturationFlag != "" {
|
|
|
|
color := lifx.HSBKColor{}
|
|
|
|
|
|
|
|
if hueFlag != "" {
|
|
|
|
hue := args.Flags.Float32("hue")
|
|
|
|
color.H = lifx.Float32Ptr(hue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if saturationFlag != "" {
|
|
|
|
saturation := args.Flags.Float32("saturation")
|
|
|
|
color.S = lifx.Float32Ptr(saturation)
|
|
|
|
}
|
|
|
|
state.Color = color
|
|
|
|
|
|
|
|
} else if rgbFlag != "" {
|
|
|
|
color, err := parseRGB(rgbFlag)
|
|
|
|
if err != nil {
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2020-03-28 23:05:35 +00:00
|
|
|
}
|
|
|
|
state.Color = color
|
2020-04-04 15:27:09 +00:00
|
|
|
} else if name != "" {
|
|
|
|
hsb, ok := args.Config.Colors[name]
|
|
|
|
if !ok {
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, fmt.Errorf("%s is not a defined color", name)
|
2020-04-04 15:27:09 +00:00
|
|
|
}
|
|
|
|
color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2])
|
|
|
|
if err != nil {
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2020-04-04 15:27:09 +00:00
|
|
|
}
|
|
|
|
state.Color = color
|
2020-03-28 23:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
brightnessFlag := args.Flags.String("brightness")
|
|
|
|
if brightnessFlag != "" {
|
|
|
|
brightness := args.Flags.Float64("brightness")
|
|
|
|
state.Brightness = brightness
|
|
|
|
}
|
|
|
|
|
|
|
|
duration := args.Flags.Float64("duration")
|
|
|
|
state.Duration = duration
|
|
|
|
|
|
|
|
fast := args.Flags.Bool("fast")
|
|
|
|
state.Fast = fast
|
|
|
|
|
|
|
|
r, err := c.SetState(selector, state)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fatal: %s\n", err)
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2020-03-28 23:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fast {
|
|
|
|
PrintResults(r.Results)
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:08:13 +00:00
|
|
|
return ExitSuccess, nil
|
2020-03-28 23:05:35 +00:00
|
|
|
}
|