Using one instance of NooliteAdapter in HTTP server

This commit is contained in:
2016-07-23 16:17:33 +05:00
parent d3b4fc3983
commit e3ca6417f3

View File

@@ -10,7 +10,7 @@ import (
"strconv"
)
func sendCommand(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")
}
@@ -19,14 +19,6 @@ func sendCommand(command string, channel, value, r, g, b int) error {
return errors.New("Command was not set")
}
n, err := noolite.DefaultNooliteAdapter()
if err != nil {
return err
}
defer n.Close()
if command == "set" {
if value != 0 {
return n.SetBrightnesValue(channel, value)
@@ -94,8 +86,16 @@ func main() {
flag.Parse()
n, err := noolite.DefaultNooliteAdapter()
if err != nil {
panic(err)
}
defer n.Close()
if *http_port < 0 {
err := sendCommand(*command, *channel, *value, *red, *green, *blue)
err := sendCommand(n, *command, *channel, *value, *red, *green, *blue)
if err != nil {
panic(err)
@@ -104,7 +104,7 @@ func main() {
http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
err := sendCommand(command, channel, value, red, green, blue)
err := sendCommand(n, command, channel, value, red, green, blue)
if err != nil {
fmt.Fprintf(w, "{\"error\": %q}", err)