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