| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package main
- import (
- "flag"
- "fmt"
- "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", r.URL.RawQuery)
- n.Switch(7)
- })
- http.ListenAndServe(":8080", nil)
- }
- }
|