Mutex was added

This commit is contained in:
2016-07-23 19:09:18 +05:00
parent 964aa84a15
commit bbd9eb5c77

View File

@@ -4,13 +4,14 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"sync"
"github.com/dedkovd/noolite" "github.com/dedkovd/noolite"
"net/http" "net/http"
"strings" "strings"
"strconv" "strconv"
) )
func sendCommand(command string, channel, value, r, g, b int) error { func sendCommand(mutex *sync.Mutex, 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")
} }
@@ -19,6 +20,8 @@ func sendCommand(command string, channel, value, r, g, b int) error {
return errors.New("Command was not set") return errors.New("Command was not set")
} }
mutex.Lock()
defer mutex.Unlock()
n, err := noolite.DefaultNooliteAdapter() n, err := noolite.DefaultNooliteAdapter()
if err != nil { if err != nil {
@@ -94,8 +97,10 @@ func main() {
flag.Parse() flag.Parse()
mutex := &sync.Mutex{}
if *http_port < 0 { if *http_port < 0 {
err := sendCommand(*command, *channel, *value, *red, *green, *blue) err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -104,7 +109,7 @@ func main() {
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(command, channel, value, red, green, blue) err := sendCommand(mutex, command, channel, value, red, green, blue)
if err != nil { if err != nil {
fmt.Fprintf(w, "{\"error\": %q}", err) fmt.Fprintf(w, "{\"error\": %q}", err)