noolite-cli.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "sync"
  7. "github.com/dedkovd/noolite"
  8. "net/http"
  9. "strings"
  10. "strconv"
  11. "time"
  12. )
  13. func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int) error {
  14. if channel == -1 {
  15. return errors.New("Channel was not set")
  16. }
  17. if command == "" {
  18. return errors.New("Command was not set")
  19. }
  20. mutex.Lock() // Lock for sync call
  21. timedUnlock := func(m *sync.Mutex) {
  22. time.Sleep(time.Millisecond * 200) // Without timeout not worked
  23. m.Unlock()
  24. }
  25. defer timedUnlock(mutex)
  26. n, err := noolite.DefaultNooliteAdapter()
  27. if err != nil {
  28. return err
  29. }
  30. defer n.Close()
  31. if command == "set" {
  32. if value != 0 {
  33. return n.SetBrightnesValue(channel, value)
  34. } else if r != 0 || g != 0 || b != 0 {
  35. return n.SetBrightnesValues(channel, r, g, b)
  36. } else {
  37. return errors.New("Need some value")
  38. }
  39. } else {
  40. commands := map[string]func(int) error{
  41. "on": n.On,
  42. "off": n.Off,
  43. "switch": n.Switch,
  44. "decraseBrightnes": n.DecraseBrightnes,
  45. "incraseBrightnes": n.IncraseBrightnes,
  46. "invertBrightnes": n.InvertBrightnes,
  47. }
  48. cmd, ok := commands[command]
  49. if !ok {
  50. return errors.New("Command not found")
  51. }
  52. return cmd(channel)
  53. }
  54. }
  55. func parseParams(path string) (string, int, int, int, int, int) {
  56. params := strings.Split(path, "/")[1:]
  57. command := ""
  58. channel := -1
  59. value := 0
  60. r := 0
  61. g := 0
  62. b := 0
  63. command = params[0]
  64. if len(params) > 1 {
  65. channel, _ = strconv.Atoi(params[1])
  66. }
  67. if len(params) > 2 {
  68. value, _ = strconv.Atoi(params[2])
  69. }
  70. if len(params) == 5 {
  71. value = 0
  72. r, _ = strconv.Atoi(params[2])
  73. g, _ = strconv.Atoi(params[3])
  74. b, _ = strconv.Atoi(params[4])
  75. }
  76. return command, channel, value, r, g, b
  77. }
  78. func main() {
  79. channel := flag.Int("channel", -1, "Noolite adapter channel")
  80. command := flag.String("command", "", "Command")
  81. value := flag.Int("val", 0, "Set value")
  82. red := flag.Int("r", 0, "Red channel")
  83. green := flag.Int("g", 0, "Green channel")
  84. blue := flag.Int("b", 0, "Blue channel")
  85. http_port := flag.Int("p", -1, "Http port")
  86. flag.Parse()
  87. mutex := &sync.Mutex{}
  88. if *http_port < 0 {
  89. err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
  90. if err != nil {
  91. panic(err)
  92. }
  93. } else {
  94. http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
  95. command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
  96. err := sendCommand(mutex, command, channel, value, red, green, blue)
  97. if err != nil {
  98. fmt.Fprintf(w, "{\"error\": %q}", err)
  99. } else {
  100. fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
  101. }
  102. })
  103. fs := http.FileServer(http.Dir("/var/www/static"))
  104. http.Handle("/static/", http.StripPrefix("/static/", fs))
  105. panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
  106. }
  107. }