lume/cmd/setwhite.go

123 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-03-28 18:18:30 +00:00
package lumecmd
import (
2021-02-17 05:24:32 +00:00
"flag"
"fmt"
2021-02-17 05:24:32 +00:00
"git.kill0.net/chill9/lifx-go"
2020-03-28 18:18:30 +00:00
)
2021-02-17 05:24:32 +00:00
func NewCmdSetWhite() Command {
return Command{
Name: "set-white",
Func: SetWhiteCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
name := fs.String("name", defaultWhiteName, "named white level")
fs.StringVar(name, "n", defaultWhiteName, "named white level")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
Short: "Set the white level",
}
}
2021-03-17 00:23:50 +00:00
func SetWhiteCmd(ctx Context) (int, error) {
2021-03-13 22:20:18 +00:00
var p Printer
2021-03-17 00:23:50 +00:00
c := ctx.Client
2020-03-28 18:18:30 +00:00
state := lifx.State{}
2021-03-17 00:23:50 +00:00
selector := ctx.Flags.String("selector")
2021-04-11 02:05:05 +00:00
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
2021-03-11 02:57:05 +00:00
2021-03-17 00:23:50 +00:00
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
2021-03-11 02:57:05 +00:00
}
2020-03-28 18:18:30 +00:00
2021-03-17 00:23:50 +00:00
power := ctx.Flags.String("power")
2020-03-28 18:18:30 +00:00
if power != "" {
state.Power = power
}
2021-03-17 00:23:50 +00:00
kelvinFlag := ctx.Flags.String("kelvin")
2020-03-28 18:18:30 +00:00
if kelvinFlag != "" {
2021-03-17 00:23:50 +00:00
kelvin := ctx.Flags.Int16("kelvin")
2020-03-28 18:18:30 +00:00
color, err := lifx.NewWhite(kelvin)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 18:18:30 +00:00
}
state.Color = color
}
2021-03-17 00:23:50 +00:00
name := ctx.Flags.String("name")
2020-03-28 18:18:30 +00:00
if name != "" {
2021-03-17 00:23:50 +00:00
name := ctx.Flags.String("name")
2020-03-28 18:18:30 +00:00
color, err := lifx.NewWhiteString(name)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 18:18:30 +00:00
}
state.Color = color
}
2021-03-17 00:23:50 +00:00
brightnessFlag := ctx.Flags.String("brightness")
2020-03-28 18:18:30 +00:00
if brightnessFlag != "" {
2021-03-17 00:23:50 +00:00
brightness := ctx.Flags.Float64("brightness")
2020-03-28 18:18:30 +00:00
state.Brightness = brightness
}
2021-03-17 00:23:50 +00:00
duration := ctx.Flags.Float64("duration")
2020-03-28 18:18:30 +00:00
state.Duration = duration
2021-03-17 00:23:50 +00:00
infraredFlag := ctx.Flags.String("infrared")
2020-03-28 18:18:30 +00:00
if infraredFlag != "" {
2021-03-17 00:23:50 +00:00
infrared := ctx.Flags.Float64("infrared")
2020-03-28 18:18:30 +00:00
state.Infrared = infrared
}
2021-03-17 00:23:50 +00:00
fast := ctx.Flags.Bool("fast")
2020-03-28 18:18:30 +00:00
state.Fast = fast
if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" {
2021-03-17 00:23:50 +00:00
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
2020-03-28 18:18:30 +00:00
r, err := c.SetState(selector, state)
if err != nil {
2021-01-31 17:08:24 +00:00
return ExitFailure, err
2020-03-28 18:18:30 +00:00
}
if !fast {
2021-03-13 22:20:18 +00:00
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
2020-03-28 18:18:30 +00:00
}
return ExitSuccess, nil
2020-03-28 18:18:30 +00:00
}