2020-03-25 00:12:35 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
2021-01-21 05:09:37 +00:00
|
|
|
"errors"
|
2020-03-25 00:12:35 +00:00
|
|
|
"flag"
|
2020-03-27 00:05:50 +00:00
|
|
|
"fmt"
|
2020-03-25 00:12:35 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-02-15 01:02:26 +00:00
|
|
|
"git.kill0.net/chill9/lifx-go"
|
2020-03-25 00:12:35 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 03:08:13 +00:00
|
|
|
const (
|
|
|
|
ExitSuccess = iota
|
2021-01-31 17:08:24 +00:00
|
|
|
ExitFailure
|
2021-01-18 03:08:13 +00:00
|
|
|
)
|
|
|
|
|
2020-03-29 04:47:42 +00:00
|
|
|
type Config struct {
|
2021-01-16 03:38:51 +00:00
|
|
|
AccessToken string `toml:"access_token"`
|
2020-04-04 15:27:09 +00:00
|
|
|
Colors map[string][]float32 `toml:"colors"`
|
2020-03-29 04:47:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 00:12:35 +00:00
|
|
|
type CmdArgs struct {
|
2020-03-28 23:13:15 +00:00
|
|
|
Flags Flags
|
|
|
|
Client *lifx.Client
|
2020-03-29 04:47:42 +00:00
|
|
|
Config Config
|
2021-02-07 23:31:19 +00:00
|
|
|
Name string
|
2020-03-25 00:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Flags struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
}
|
|
|
|
|
2020-03-27 00:05:50 +00:00
|
|
|
type Command struct {
|
2020-03-28 00:31:41 +00:00
|
|
|
Name string
|
2020-03-29 05:08:17 +00:00
|
|
|
Func func(CmdArgs) (int, error)
|
2020-03-27 00:05:50 +00:00
|
|
|
Flags *flag.FlagSet
|
2020-08-07 23:11:42 +00:00
|
|
|
Use string
|
2020-08-08 03:09:40 +00:00
|
|
|
Short string
|
|
|
|
Long string
|
2020-03-27 00:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandRegistry = make(map[string]Command)
|
|
|
|
|
2020-03-28 20:22:34 +00:00
|
|
|
var (
|
|
|
|
defaultSelector string = "all"
|
|
|
|
defaultDuration float64 = 1.0
|
|
|
|
defaultPower string = ""
|
|
|
|
defaultColor string = ""
|
|
|
|
defaultBrightness string = ""
|
|
|
|
defaultInfrared string = ""
|
|
|
|
defaultFast bool = false
|
|
|
|
defaultWhiteKelvin string = ""
|
|
|
|
defaultWhiteName string = ""
|
2020-03-28 23:03:16 +00:00
|
|
|
defaultHue string = ""
|
|
|
|
defaultSaturation string = ""
|
|
|
|
defaultRGB string = ""
|
2020-04-04 15:27:09 +00:00
|
|
|
defaultName string = ""
|
2020-03-28 20:22:34 +00:00
|
|
|
)
|
|
|
|
|
2020-03-25 00:12:35 +00:00
|
|
|
func (f Flags) String(name string) string {
|
|
|
|
return f.FlagSet.Lookup(name).Value.String()
|
|
|
|
}
|
|
|
|
|
2020-03-28 23:03:16 +00:00
|
|
|
func (f Flags) Float32(name string) float32 {
|
|
|
|
val, _ := strconv.ParseFloat(f.String(name), 32)
|
|
|
|
return float32(val)
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:12:35 +00:00
|
|
|
func (f Flags) Float64(name string) float64 {
|
|
|
|
val, _ := strconv.ParseFloat(f.String(name), 64)
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-03-28 18:18:15 +00:00
|
|
|
func (f Flags) Int16(name string) int16 {
|
|
|
|
val, _ := strconv.ParseInt(f.String(name), 10, 16)
|
|
|
|
return int16(val)
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:12:35 +00:00
|
|
|
func (f Flags) Bool(name string) bool {
|
|
|
|
val, _ := strconv.ParseBool(f.String(name))
|
|
|
|
return val
|
|
|
|
}
|
2020-03-27 00:05:50 +00:00
|
|
|
|
|
|
|
func RegisterCommand(name string, cmd Command) error {
|
|
|
|
if _, ok := commandRegistry[name]; ok {
|
|
|
|
return fmt.Errorf("%s command is already registered")
|
|
|
|
}
|
2020-08-07 23:09:54 +00:00
|
|
|
cmd.Name = name
|
2020-03-27 00:05:50 +00:00
|
|
|
commandRegistry[name] = cmd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommand(name string) (Command, bool) {
|
|
|
|
cmd, ok := commandRegistry[name]
|
|
|
|
return cmd, ok
|
|
|
|
}
|
2021-01-21 05:09:37 +00:00
|
|
|
|
|
|
|
// Validate configuration struct
|
|
|
|
func (c *Config) Validate() error {
|
|
|
|
var err error
|
|
|
|
if c.AccessToken == "" {
|
|
|
|
err = errors.New("access_token is not set")
|
|
|
|
}
|
2021-01-25 01:02:48 +00:00
|
|
|
|
|
|
|
if err = c.validateColors(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateColors() (err error) {
|
|
|
|
if len(c.Colors) > 0 {
|
|
|
|
for name, hsb := range c.Colors {
|
|
|
|
if len(hsb) != 3 {
|
|
|
|
return fmt.Errorf("color '%s' needs three values", name)
|
|
|
|
}
|
|
|
|
h, s, b := hsb[0], hsb[1], hsb[2]
|
|
|
|
if h < 0 || h > 360 {
|
|
|
|
return fmt.Errorf("color '%s' hue value must be between 0.0-360.0", name)
|
|
|
|
}
|
|
|
|
if s < 0 || b > 1 {
|
|
|
|
return fmt.Errorf("color '%s' saturation value must be between 0.0-1.0", name)
|
|
|
|
}
|
|
|
|
if b < 0 || b > 1 {
|
|
|
|
return fmt.Errorf("color '%s' brightness value must be between 0.0-1.0", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 05:09:37 +00:00
|
|
|
return err
|
|
|
|
}
|