noolite-cli.go 2.6 KB

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