package main import ( "flag" "fmt" "strconv" "github.com/dedkovd/noolite" "net/http" ) 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") flag.Parse() if *channel == -1 { panic("Channel was not set") } if *command == "" { panic("Command was not set") } n, err := noolite.DefaultNooliteAdapter() if err != nil { panic(err) } defer n.Close() if *command == "set" { if *value != 0 { err = n.SetBrightnesValue(*channel, *value) } else if *red != 0 || *green != 0 || *blue != 0 { err = n.SetBrightnesValues(*channel, *red, *green, *blue) } else { panic("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] if !ok { panic("Command not found") } err = cmd(*channel) } if err != nil { panic(err) } if *http_port != -1 { http.HandleFunc("/switch", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%q\n", r.URL.Query()) q := r.URL.Query() c, ok := q["c"] if !ok { fmt.Fprintf(w, "Channel param required\n") } else { cn, _ := strconv.Atoi(c[0]) err = n.Switch(cn) } if err != nil { fmt.Fprintf(w, "%q\n", err) } }) http.ListenAndServe(":8080", nil) } }