noolite-cli.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "github.com/dedkovd/noolite"
  7. "net/http"
  8. "strings"
  9. "strconv"
  10. )
  11. func sendCommand(command string, channel, value, r, g, b int) error {
  12. if channel == -1 {
  13. return errors.New("Channel was not set")
  14. }
  15. if command == "" {
  16. return errors.New("Command was not set")
  17. }
  18. n, err := noolite.DefaultNooliteAdapter()
  19. if err != nil {
  20. if n != nil {
  21. n.Close()
  22. }
  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. if *http_port < 0 {
  83. err := sendCommand(*command, *channel, *value, *red, *green, *blue)
  84. if err != nil {
  85. panic(err)
  86. }
  87. } else {
  88. http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
  89. command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
  90. err := sendCommand(command, channel, value, red, green, blue)
  91. if err != nil {
  92. fmt.Fprintf(w, "{\"error\": %q}", err)
  93. } else {
  94. fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
  95. }
  96. })
  97. fs := http.FileServer(http.Dir("/var/www/static"))
  98. http.Handle("/static/", http.StripPrefix("/static/", fs))
  99. panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
  100. }
  101. }