REST interface was added
This commit is contained in:
143
noolite-cli.go
143
noolite-cli.go
@@ -1,13 +1,86 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"github.com/dedkovd/noolite"
|
"github.com/dedkovd/noolite"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func sendCommand(command string, channel, value, r, g, b int) error {
|
||||||
|
if channel == -1 {
|
||||||
|
return errors.New("Channel was not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
if command == "" {
|
||||||
|
return errors.New("Command was not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := noolite.DefaultNooliteAdapter()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
if command == "set" {
|
||||||
|
if value != 0 {
|
||||||
|
return n.SetBrightnesValue(channel, value)
|
||||||
|
} else if r != 0 || g != 0 || b != 0 {
|
||||||
|
return n.SetBrightnesValues(channel, r, g, b)
|
||||||
|
} else {
|
||||||
|
return errors.New("Need some value")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
commands := map[string]func(int) error{
|
||||||
|
"on": n.On,
|
||||||
|
"off": n.Off,
|
||||||
|
"switch": n.Switch,
|
||||||
|
"decraseBrightnes": n.DecraseBrightnes,
|
||||||
|
"incraseBrightnes": n.IncraseBrightnes,
|
||||||
|
"invertBrightnes": n.InvertBrightnes,
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd, ok := commands[command]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return errors.New("Command not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd(channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseParams(path string) (string, int, int, int, int, int) {
|
||||||
|
params := strings.Split(path, "/")[1:]
|
||||||
|
|
||||||
|
command := ""
|
||||||
|
channel := -1
|
||||||
|
value := 0
|
||||||
|
r := 0
|
||||||
|
g := 0
|
||||||
|
b := 0
|
||||||
|
|
||||||
|
command = params[0]
|
||||||
|
if len(params) > 1 {
|
||||||
|
channel, _ = strconv.Atoi(params[1])
|
||||||
|
}
|
||||||
|
if len(params) > 2 {
|
||||||
|
value, _ = strconv.Atoi(params[2])
|
||||||
|
}
|
||||||
|
if len(params) == 5 {
|
||||||
|
r, _ = strconv.Atoi(params[2])
|
||||||
|
g, _ = strconv.Atoi(params[3])
|
||||||
|
b, _ = strconv.Atoi(params[4])
|
||||||
|
}
|
||||||
|
|
||||||
|
return command, channel, value, r, g, b
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
channel := flag.Int("channel", -1, "Noolite adapter channel")
|
channel := flag.Int("channel", -1, "Noolite adapter channel")
|
||||||
command := flag.String("command", "", "Command")
|
command := flag.String("command", "", "Command")
|
||||||
@@ -20,71 +93,25 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *channel == -1 {
|
if *http_port < 0 {
|
||||||
panic("Channel was not set")
|
err := sendCommand(*command, *channel, *value, *red, *green, *blue)
|
||||||
}
|
|
||||||
|
|
||||||
if *command == "" {
|
if err != nil {
|
||||||
panic("Command was not set")
|
panic(err)
|
||||||
}
|
|
||||||
|
|
||||||
n, err := noolite.DefaultNooliteAdapter()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer n.Close()
|
|
||||||
|
|
||||||
if *command == "set" {
|
|
||||||
if *value != 0 {
|
|
||||||
err = n.SetBrightnesValue(*channel, *value)
|
|
||||||
} else if *red != 0 || *green != 0 || *blue != 0 {
|
|
||||||
err = n.SetBrightnesValues(*channel, *red, *green, *blue)
|
|
||||||
} else {
|
|
||||||
panic("Need some value")
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commands := map[string]func(int) error{
|
http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
"on": n.On,
|
command, channel, _, _,_, _ := parseParams(r.URL.Path[1:])
|
||||||
"off": n.Off,
|
fmt.Fprintf(w, "Command: %q\n", command)
|
||||||
"switch": n.Switch,
|
fmt.Fprintf(w, "Channel: %d\n", channel)
|
||||||
"decraseBrightnes": n.DecraseBrightnes,
|
|
||||||
"incraseBrightnes": n.IncraseBrightnes,
|
|
||||||
"invertBrightnes": n.InvertBrightnes,
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd, ok := commands[*command]
|
err := sendCommand(command, channel, 0, 0, 0, 0)
|
||||||
|
|
||||||
if !ok {
|
|
||||||
panic("Command not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd(*channel)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *http_port != -1 {
|
|
||||||
http.HandleFunc("/switch", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprintf(w, "%q\n", r.URL.Query())
|
|
||||||
q := r.URL.Query()
|
|
||||||
c, ok := q["c"]
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
fmt.Fprintf(w, "Channel param required\n")
|
|
||||||
} else {
|
|
||||||
cn, _ := strconv.Atoi(c[0])
|
|
||||||
err = n.Switch(cn)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "%q\n", err)
|
fmt.Fprintf(w, "Error: %q\n", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user