Command line interface was removed
This commit is contained in:
@@ -4,15 +4,13 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"sync"
|
||||
"github.com/dedkovd/noolite"
|
||||
"net/http"
|
||||
"strings"
|
||||
"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 {
|
||||
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")
|
||||
}
|
||||
|
||||
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 value != 0 {
|
||||
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")
|
||||
}
|
||||
} 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]
|
||||
cmd, ok := n.StringCommand(command)
|
||||
|
||||
if !ok {
|
||||
return errors.New("Command not found")
|
||||
@@ -94,30 +66,23 @@ func parseParams(path string) (string, int, int, int, int, int) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
channel := flag.Int("channel", -1, "Noolite adapter channel")
|
||||
command := flag.String("command", "", "Command")
|
||||
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")
|
||||
binding := *flag.String("bind", ":8080", "Address binding")
|
||||
static_dir := *flag.String("static", "/var/www/static", "Static directory")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
mutex := &sync.Mutex{}
|
||||
|
||||
if *http_port < 0 {
|
||||
err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
|
||||
n, err := noolite.DefaultNooliteAdapter()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
|
||||
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(mutex, command, channel, value, red, green, blue)
|
||||
err := sendCommand(n, command, channel, value, red, green, blue)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\": %q}", err)
|
||||
@@ -126,10 +91,9 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
fs := http.FileServer(http.Dir("/var/www/static"))
|
||||
fs := http.FileServer(http.Dir(static_dir))
|
||||
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
|
||||
panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
|
||||
}
|
||||
panic(http.ListenAndServe(binding, nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user