Command line interface was removed
This commit is contained in:
@@ -4,15 +4,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"github.com/dedkovd/noolite"
|
"github.com/dedkovd/noolite"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int) error {
|
func sendCommand(n *noolite.NooliteAdapter, command string, channel, value, r, g, b int) error {
|
||||||
if channel == -1 {
|
if channel == -1 {
|
||||||
return errors.New("Channel was not set")
|
return errors.New("Channel was not set")
|
||||||
}
|
}
|
||||||
@@ -21,23 +19,6 @@ func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int)
|
|||||||
return errors.New("Command was not set")
|
return errors.New("Command was not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex.Lock() // Lock for sync call
|
|
||||||
|
|
||||||
timedUnlock := func(m *sync.Mutex) {
|
|
||||||
time.Sleep(time.Millisecond * 200) // Without timeout not worked
|
|
||||||
m.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
defer timedUnlock(mutex)
|
|
||||||
|
|
||||||
n, err := noolite.DefaultNooliteAdapter()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer n.Close()
|
|
||||||
|
|
||||||
if command == "set" {
|
if command == "set" {
|
||||||
if value != 0 {
|
if value != 0 {
|
||||||
return n.SetBrightnesValue(channel, value)
|
return n.SetBrightnesValue(channel, value)
|
||||||
@@ -47,16 +28,7 @@ func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int)
|
|||||||
return errors.New("Need some value")
|
return errors.New("Need some value")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commands := map[string]func(int) error{
|
cmd, ok := n.StringCommand(command)
|
||||||
"on": n.On,
|
|
||||||
"off": n.Off,
|
|
||||||
"switch": n.Switch,
|
|
||||||
"decraseBrightnes": n.DecraseBrightnes,
|
|
||||||
"incraseBrightnes": n.IncraseBrightnes,
|
|
||||||
"invertBrightnes": n.InvertBrightnes,
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd, ok := commands[command]
|
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("Command not found")
|
return errors.New("Command not found")
|
||||||
@@ -94,42 +66,34 @@ func parseParams(path string) (string, int, int, int, int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
channel := flag.Int("channel", -1, "Noolite adapter channel")
|
binding := *flag.String("bind", ":8080", "Address binding")
|
||||||
command := flag.String("command", "", "Command")
|
static_dir := *flag.String("static", "/var/www/static", "Static directory")
|
||||||
value := flag.Int("val", 0, "Set value")
|
|
||||||
red := flag.Int("r", 0, "Red channel")
|
|
||||||
green := flag.Int("g", 0, "Green channel")
|
|
||||||
blue := flag.Int("b", 0, "Blue channel")
|
|
||||||
|
|
||||||
http_port := flag.Int("p", -1, "Http port")
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
mutex := &sync.Mutex{}
|
n, err := noolite.DefaultNooliteAdapter()
|
||||||
|
|
||||||
if *http_port < 0 {
|
if err != nil {
|
||||||
err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
|
||||||
|
|
||||||
|
err := sendCommand(n, command, channel, value, red, green, blue)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Fprintf(w, "{\"error\": %q}", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
|
||||||
}
|
}
|
||||||
} else {
|
})
|
||||||
http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
|
|
||||||
|
|
||||||
err := sendCommand(mutex, command, channel, value, red, green, blue)
|
fs := http.FileServer(http.Dir(static_dir))
|
||||||
|
|
||||||
if err != nil {
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||||
fmt.Fprintf(w, "{\"error\": %q}", err)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
fs := http.FileServer(http.Dir("/var/www/static"))
|
panic(http.ListenAndServe(binding, nil))
|
||||||
|
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
|
||||||
|
|
||||||
panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user