Command line interface was removed

This commit is contained in:
2016-07-24 11:41:17 +05:00
parent b4e748a6ac
commit 0fe0289299

View File

@@ -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,30 +66,23 @@ 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 {
err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} else {
defer n.Close()
http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
command, channel, value, red, green, blue := parseParams(r.URL.Path[1:]) 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 { if err != nil {
fmt.Fprintf(w, "{\"error\": %q}", err) 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)) http.Handle("/static/", http.StripPrefix("/static/", fs))
panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil)) panic(http.ListenAndServe(binding, nil))
}
} }